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

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

* address review
This commit is contained in:
Nutomic 2021-04-22 23:42:58 +00:00 committed by GitHub
parent efee2062dd
commit db1abff857
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 27 additions and 7 deletions

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

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

@ -433,6 +433,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 DEFAULT false;