fix: add MoveChannels permission and check permissions when moving channels
This commit is contained in:
@@ -96,8 +96,10 @@ pub enum ChannelPermission {
|
|||||||
/// Mention roles
|
/// Mention roles
|
||||||
MentionRoles = 1 << 38,
|
MentionRoles = 1 << 38,
|
||||||
|
|
||||||
|
MoveChannels = 1 << 39,
|
||||||
|
|
||||||
// * Misc. permissions
|
// * Misc. permissions
|
||||||
// % Bits 38 to 52: free area
|
// % Bits 40 to 52: free area
|
||||||
// % Bits 53 to 64: do not use
|
// % Bits 53 to 64: do not use
|
||||||
|
|
||||||
// * Grant all permissions
|
// * Grant all permissions
|
||||||
|
|||||||
@@ -34,10 +34,11 @@ pub async fn edit(
|
|||||||
.ok_or(create_error!(UnknownCategory))?
|
.ok_or(create_error!(UnknownCategory))?
|
||||||
.clone();
|
.clone();
|
||||||
|
|
||||||
let mut query = DatabasePermissionQuery::new(db, &user).server(&server);
|
let mut query = DatabasePermissionQuery::new(db, &user).server(&server).category(&category);
|
||||||
calculate_server_permissions(&mut query)
|
let permissions = calculate_server_permissions(&mut query)
|
||||||
.await
|
.await;
|
||||||
.throw_if_lacking_channel_permission(ChannelPermission::ManageChannel)?;
|
|
||||||
|
permissions.throw_if_lacking_channel_permission(ChannelPermission::ManageChannel)?;
|
||||||
|
|
||||||
let DataEditCategory {
|
let DataEditCategory {
|
||||||
title,
|
title,
|
||||||
@@ -45,9 +46,24 @@ pub async fn edit(
|
|||||||
remove
|
remove
|
||||||
} = data;
|
} = data;
|
||||||
|
|
||||||
|
if channels.is_some() {
|
||||||
|
permissions.throw_if_lacking_channel_permission(ChannelPermission::MoveChannels)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if any channel we are adding to this category are in another category we need to make sure we have MoveChannels in the category before moving it over.
|
||||||
|
for category in server.categories.values() {
|
||||||
|
if category.channels.iter().any(|c| channels.as_ref().is_some_and(|cs| cs.contains(c))) {
|
||||||
|
let mut category_query = query.clone().category(category);
|
||||||
|
|
||||||
|
calculate_server_permissions(&mut category_query)
|
||||||
|
.await
|
||||||
|
.throw_if_lacking_channel_permission(ChannelPermission::MoveChannels)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// remove the channels from any existing categories to avoid it having two
|
// remove the channels from any existing categories to avoid it having two
|
||||||
for category in server.categories.values_mut() {
|
for category in server.categories.values_mut() {
|
||||||
category.channels.retain(|c| channels.as_ref().map_or(false, |cs| cs.contains(c)));
|
category.channels.retain(|c| channels.as_ref().is_some_and(|cs| cs.contains(c)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// only keep channels which exist in the server
|
// only keep channels which exist in the server
|
||||||
|
|||||||
Reference in New Issue
Block a user