]> Untitled Git - lemmy.git/commitdiff
Add option to limit community creation to admins only (fixes #1586) (#1587)
authorNutomic <me@nutomic.com>
Thu, 22 Apr 2021 23:42:58 +0000 (23:42 +0000)
committerGitHub <noreply@github.com>
Thu, 22 Apr 2021 23:42:58 +0000 (19:42 -0400)
* Add option to limit community creation to admins only (fixes #1586)

* address review

crates/api_common/src/site.rs
crates/api_crud/src/community/create.rs
crates/api_crud/src/site/create.rs
crates/api_crud/src/site/read.rs
crates/api_crud/src/site/update.rs
crates/db_queries/src/aggregates/site_aggregates.rs
crates/db_schema/src/schema.rs
crates/db_schema/src/source/site.rs
migrations/2021-04-20-155001_limit-admins-create-community/down.sql [new file with mode: 0644]
migrations/2021-04-20-155001_limit-admins-create-community/up.sql [new file with mode: 0644]

index 397b5a72ba3ded1795ea5d793e27c3cfe7e6957f..ffee7ba8685df0fc56a192313c499722ae8fab2f 100644 (file)
@@ -71,6 +71,7 @@ pub struct CreateSite {
   pub enable_downvotes: bool,
   pub open_registration: bool,
   pub enable_nsfw: bool,
+  pub community_creation_admin_only: bool,
   pub auth: String,
 }
 
@@ -84,6 +85,7 @@ pub struct EditSite {
   pub enable_downvotes: bool,
   pub open_registration: bool,
   pub enable_nsfw: bool,
+  pub community_creation_admin_only: Option<bool>,
   pub auth: String,
 }
 
index d733966ca8e1818c8fd71c31b2cfa3f9650648c5..88f4415bfcc97d91149a5bcf37dcf9490d07c644 100644 (file)
@@ -4,6 +4,7 @@ use lemmy_api_common::{
   blocking,
   community::{CommunityResponse, CreateCommunity},
   get_local_user_view_from_jwt,
+  is_admin,
 };
 use lemmy_apub::{
   generate_apub_endpoint,
@@ -13,13 +14,16 @@ use lemmy_apub::{
   EndpointType,
 };
 use lemmy_db_queries::{diesel_option_overwrite_to_url, ApubObject, Crud, Followable, Joinable};
-use lemmy_db_schema::source::community::{
-  Community,
-  CommunityFollower,
-  CommunityFollowerForm,
-  CommunityForm,
-  CommunityModerator,
-  CommunityModeratorForm,
+use lemmy_db_schema::source::{
+  community::{
+    Community,
+    CommunityFollower,
+    CommunityFollowerForm,
+    CommunityForm,
+    CommunityModerator,
+    CommunityModeratorForm,
+  },
+  site::Site,
 };
 use lemmy_db_views_actor::community_view::CommunityView;
 use lemmy_utils::{
@@ -43,6 +47,11 @@ impl PerformCrud for CreateCommunity {
     let data: &CreateCommunity = &self;
     let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
 
+    let site = blocking(context.pool(), move |conn| Site::read(conn, 0)).await??;
+    if site.community_creation_admin_only && is_admin(&local_user_view).is_err() {
+      return Err(ApiError::err("only_admins_can_create_communities").into());
+    }
+
     check_slurs(&data.name)?;
     check_slurs(&data.title)?;
     check_slurs_opt(&data.description)?;
index b9889bf1eda1b1c0e81bfb61697828ffbf00a32f..dc6e649d6a096a5816e08c19c8af93ed024d24dc 100644 (file)
@@ -67,6 +67,7 @@ impl PerformCrud for CreateSite {
       open_registration: data.open_registration,
       enable_nsfw: data.enable_nsfw,
       updated: None,
+      community_creation_admin_only: Some(data.community_creation_admin_only),
     };
 
     let create_site = move |conn: &'_ _| Site::create(conn, &site_form);
index d3bf0d2d1b6d12e4f2f52fc008b9324288344910..e378b26b0a4f01f7b193e976c99f4e63cc0e29ec 100644 (file)
@@ -51,6 +51,7 @@ impl PerformCrud for GetSite {
             open_registration: true,
             enable_nsfw: true,
             auth: login_response.jwt,
+            community_creation_admin_only: false,
           };
           create_site.perform(context, websocket_id).await?;
           info!("Site {} created", setup.site_name);
index e9c5828dfef0277829b5ee7f51cde81af9bcc906..58f4084925d0080c53487cf4e2c65c4eb5360a3f 100644 (file)
@@ -65,6 +65,7 @@ impl PerformCrud for EditSite {
       enable_downvotes: data.enable_downvotes,
       open_registration: data.open_registration,
       enable_nsfw: data.enable_nsfw,
+      community_creation_admin_only: data.community_creation_admin_only,
     };
 
     let update_site = move |conn: &'_ _| Site::update(conn, 1, &site_form);
index 995462e1a8b8f72fc5d8ae81177949e933fbfea7..934a730510b06ef557544ccd910a1da1952db97f 100644 (file)
@@ -58,6 +58,7 @@ mod tests {
       open_registration: true,
       enable_nsfw: true,
       updated: None,
+      community_creation_admin_only: Some(false),
     };
 
     Site::create(&conn, &site_form).unwrap();
index f92e43f72d5efba3f09040e1c1e277650ea37218..a700839818a8b3ae406e0c0334b73c0094a5ad67 100644 (file)
@@ -433,6 +433,7 @@ table! {
         icon -> Nullable<Varchar>,
         banner -> Nullable<Varchar>,
         description -> Nullable<Text>,
+        community_creation_admin_only -> Bool,
     }
 }
 
index 41042bc5cb2fc177b977b1ad3c19a493ec61dd3d..67bba99331034614f5acc8ba89c8d65048bb4dc9 100644 (file)
@@ -16,6 +16,7 @@ pub struct Site {
   pub icon: Option<DbUrl>,
   pub banner: Option<DbUrl>,
   pub description: Option<String>,
+  pub community_creation_admin_only: bool,
 }
 
 #[derive(Insertable, AsChangeset)]
@@ -32,4 +33,5 @@ pub struct SiteForm {
   pub icon: Option<Option<DbUrl>>,
   pub banner: Option<Option<DbUrl>>,
   pub description: Option<Option<String>>,
+  pub community_creation_admin_only: Option<bool>,
 }
diff --git a/migrations/2021-04-20-155001_limit-admins-create-community/down.sql b/migrations/2021-04-20-155001_limit-admins-create-community/down.sql
new file mode 100644 (file)
index 0000000..a04036f
--- /dev/null
@@ -0,0 +1 @@
+ALTER TABLE site DROP COLUMN community_creation_admin_only;
diff --git a/migrations/2021-04-20-155001_limit-admins-create-community/up.sql b/migrations/2021-04-20-155001_limit-admins-create-community/up.sql
new file mode 100644 (file)
index 0000000..f302f88
--- /dev/null
@@ -0,0 +1 @@
+ALTER TABLE site ADD COLUMN community_creation_admin_only bool NOT NULL DEFAULT false;