fix: don't allow communities to be renamed to in-use names

This commit is contained in:
trisua 2025-04-12 10:15:47 -04:00
parent 5995aaf31c
commit e092d46586
7 changed files with 50 additions and 11 deletions

View file

@ -211,7 +211,7 @@
{% endif %} {% endif %} {% if can_manage_community or {% endif %} {% endif %} {% if can_manage_community or
is_manager %} is_manager %}
<a <a
href="/community/{{ community.title }}/manage" href="/community/{{ community.id }}/manage"
class="button primary" class="button primary"
> >
{{ icon "settings" }} {{ icon "settings" }}

View file

@ -30,7 +30,7 @@
{% if can_manage_roles %} {% if can_manage_roles %}
<a <a
href="/community/{{ community.title }}/manage?uid={{ item[1].id }}#/members" href="/community/{{ community.id }}/manage?uid={{ item[1].id }}#/members"
class="button small quaternary" class="button small quaternary"
> >
{{ icon "pencil" }} {{ icon "pencil" }}

View file

@ -94,7 +94,7 @@ pub async fn update_title_request(
None => return Json(Error::NotAllowed.into()), None => return Json(Error::NotAllowed.into()),
}; };
match data.update_community_title(id, user, req.title).await { match data.update_community_title(id, user, &req.title).await {
Ok(_) => Json(ApiReturn { Ok(_) => Json(ApiReturn {
ok: true, ok: true,
message: "Community updated".to_string(), message: "Community updated".to_string(),

View file

@ -369,10 +369,10 @@ pub async fn feed_request(
)) ))
} }
/// `/community/{title}/manage` /// `/community/{id}/manage`
pub async fn settings_request( pub async fn settings_request(
jar: CookieJar, jar: CookieJar,
Path(title): Path<String>, Path(id): Path<usize>,
Extension(data): Extension<State>, Extension(data): Extension<State>,
) -> impl IntoResponse { ) -> impl IntoResponse {
let data = data.read().await; let data = data.read().await;
@ -385,7 +385,7 @@ pub async fn settings_request(
} }
}; };
let community = match data.0.get_community_by_title(&title.to_lowercase()).await { let community = match data.0.get_community_by_id_no_void(id).await {
Ok(ua) => ua, Ok(ua) => ua,
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)), Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
}; };

View file

@ -55,10 +55,7 @@ pub fn routes() -> Router {
get(communities::create_post_request), get(communities::create_post_request),
) )
.route("/community/{title}", get(communities::feed_request)) .route("/community/{title}", get(communities::feed_request))
.route( .route("/community/{id}/manage", get(communities::settings_request))
"/community/{title}/manage",
get(communities::settings_request),
)
.route( .route(
"/community/{title}/members", "/community/{title}/members",
get(communities::members_request), get(communities::members_request),

View file

@ -355,7 +355,47 @@ impl DataManager {
Ok(()) Ok(())
} }
auto_method!(update_community_title(String)@get_community_by_id_no_void:MANAGE_COMMUNITIES -> "UPDATE communities SET title = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_community); pub async fn update_community_title(&self, id: usize, user: User, title: &str) -> Result<()> {
let y = self.get_community_by_id(id).await?;
if user.id != y.owner {
if !user.permissions.check(FinePermission::MANAGE_COMMUNITIES) {
return Err(Error::NotAllowed);
} else {
self.create_audit_log_entry(crate::model::moderation::AuditLogEntry::new(
user.id,
format!("invoked `update_community_title` with x value `{id}`"),
))
.await?
}
}
// check for existing community
if self.get_community_by_title_no_void(title).await.is_ok() {
return Err(Error::TitleInUse);
}
// ...
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = execute!(
&conn,
"UPDATE communities SET title = $1 WHERE id = $2",
params![&title, &(id as i64)]
);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
self.cache_clear_community(&y).await;
Ok(())
}
auto_method!(update_community_context(CommunityContext)@get_community_by_id_no_void:MANAGE_COMMUNITIES -> "UPDATE communities SET context = $1 WHERE id = $2" --serde --cache-key-tmpl=cache_clear_community); auto_method!(update_community_context(CommunityContext)@get_community_by_id_no_void:MANAGE_COMMUNITIES -> "UPDATE communities SET context = $1 WHERE id = $2" --serde --cache-key-tmpl=cache_clear_community);
auto_method!(update_community_read_access(CommunityReadAccess)@get_community_by_id_no_void:MANAGE_COMMUNITIES -> "UPDATE communities SET read_access = $1 WHERE id = $2" --serde --cache-key-tmpl=cache_clear_community); auto_method!(update_community_read_access(CommunityReadAccess)@get_community_by_id_no_void:MANAGE_COMMUNITIES -> "UPDATE communities SET read_access = $1 WHERE id = $2" --serde --cache-key-tmpl=cache_clear_community);
auto_method!(update_community_write_access(CommunityWriteAccess)@get_community_by_id_no_void:MANAGE_COMMUNITIES -> "UPDATE communities SET write_access = $1 WHERE id = $2" --serde --cache-key-tmpl=cache_clear_community); auto_method!(update_community_write_access(CommunityWriteAccess)@get_community_by_id_no_void:MANAGE_COMMUNITIES -> "UPDATE communities SET write_access = $1 WHERE id = $2" --serde --cache-key-tmpl=cache_clear_community);

View file

@ -31,6 +31,7 @@ pub enum Error {
DataTooLong(String), DataTooLong(String),
DataTooShort(String), DataTooShort(String),
UsernameInUse, UsernameInUse,
TitleInUse,
Unknown, Unknown,
} }
@ -49,6 +50,7 @@ impl ToString for Error {
Self::DataTooLong(name) => format!("Given {name} is too long!"), Self::DataTooLong(name) => format!("Given {name} is too long!"),
Self::DataTooShort(name) => format!("Given {name} is too short!"), Self::DataTooShort(name) => format!("Given {name} is too short!"),
Self::UsernameInUse => "Username in use".to_string(), Self::UsernameInUse => "Username in use".to_string(),
Self::TitleInUse => "Title in use".to_string(),
_ => format!("An unknown error as occurred: ({:?})", self), _ => format!("An unknown error as occurred: ({:?})", self),
} }
} }