Add option to limit community creation to admins only (fixes #1586)

This commit is contained in:
Felix Ableitner 2021-04-20 17:48:07 +02:00
parent 65a11a7239
commit 92561568e5
10 changed files with 27 additions and 7 deletions

View file

@ -72,6 +72,7 @@ pub struct CreateSite {
pub open_registration: bool,
pub enable_nsfw: bool,
pub auth: String,
pub community_creation_admin_only: bool,
}
#[derive(Deserialize)]
@ -85,6 +86,7 @@ pub struct EditSite {
pub open_registration: bool,
pub enable_nsfw: bool,
pub auth: String,
pub community_creation_admin_only: Option<bool>,
}
#[derive(Deserialize)]

View 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)?;

View 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);

View 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);

View 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);

View 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();

View file

@ -431,6 +431,7 @@ table! {
icon -> Nullable<Varchar>,
banner -> Nullable<Varchar>,
description -> Nullable<Text>,
community_creation_admin_only -> Bool,
}
}

View 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>,
}

View file

@ -0,0 +1 @@
ALTER TABLE site DROP COLUMN community_creation_admin_only;

View file

@ -0,0 +1 @@
ALTER TABLE site ADD COLUMN community_creation_admin_only bool NOT NULL;