init
This commit is contained in:
@@ -24,6 +24,7 @@ pub struct Model {
|
||||
/// L'utilisateur à qui appartiennent ces permissions
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
pub user_id: Uuid,
|
||||
pub server_id: Uuid,
|
||||
|
||||
pub scope_type: PermissionScopeType,
|
||||
|
||||
|
||||
@@ -4,10 +4,12 @@ use crate::models::{
|
||||
};
|
||||
use crate::repositories::{AnyResult, RepositoryContext};
|
||||
// use sea_orm::prelude::*;
|
||||
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};
|
||||
use sea_orm::{ActiveModelTrait, ColumnTrait, EntityTrait, QueryFilter, Set, TransactionTrait};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::permissions::ServerPermission;
|
||||
use crate::models::channel::ChannelType;
|
||||
use crate::models::computed_permission::PermissionScopeType;
|
||||
use crate::permissions::{ChannelPermission, ServerPermission, VoicePermission};
|
||||
use std::sync::Arc;
|
||||
use uuid::Uuid;
|
||||
|
||||
@@ -112,16 +114,25 @@ impl ComputedPermissionRepository {
|
||||
});
|
||||
|
||||
// Compute permissions
|
||||
let mut computed_permissions: Vec<computed_permission::ActiveModel> = Vec::new();
|
||||
// server
|
||||
let mut server_permissions = ServerPermission::empty();
|
||||
match all_permissions.get(&server_id) {
|
||||
Some(perm) => {
|
||||
let mut server_permissions = ServerPermission::empty();
|
||||
for permission in perm {
|
||||
let current =
|
||||
server_permissions |=
|
||||
ServerPermission::from_bits_retain(permission.server_permissions as u64);
|
||||
|
||||
server_permissions |= current;
|
||||
}
|
||||
|
||||
computed_permissions.push(computed_permission::ActiveModel {
|
||||
user_id: Set(user_id),
|
||||
server_id: Set(server_id),
|
||||
scope_type: Set(PermissionScopeType::Server),
|
||||
resource_id: Set(server_id),
|
||||
server_permissions: Set(server_permissions.bits() as i64),
|
||||
channel_permissions: Set(ChannelPermission::empty().bits() as i64),
|
||||
voice_permissions: Set(ChannelPermission::empty().bits() as i64),
|
||||
});
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
@@ -130,6 +141,54 @@ impl ComputedPermissionRepository {
|
||||
// todo : à faire
|
||||
|
||||
// channels
|
||||
channels.into_iter().for_each(|channel| {
|
||||
let mut channel_permissions = ChannelPermission::empty();
|
||||
let mut voice_permissions = VoicePermission::empty();
|
||||
match all_permissions.get(&channel.id) {
|
||||
Some(perm) => {
|
||||
for permission in perm {
|
||||
channel_permissions |= ChannelPermission::from_bits_retain(
|
||||
permission.channel_permissions as u64,
|
||||
);
|
||||
if channel.channel_type == ChannelType::Voice {
|
||||
voice_permissions |= VoicePermission::from_bits_retain(
|
||||
permission.voice_permissions as u64,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
computed_permissions.push(computed_permission::ActiveModel {
|
||||
user_id: Set(user_id),
|
||||
server_id: Set(server_id),
|
||||
scope_type: Set(PermissionScopeType::Channel),
|
||||
resource_id: Set(channel.id),
|
||||
server_permissions: Set(ServerPermission::empty().bits() as i64),
|
||||
channel_permissions: Set(channel_permissions.bits() as i64),
|
||||
voice_permissions: Set(voice_permissions.bits() as i64),
|
||||
});
|
||||
});
|
||||
|
||||
// apply in the DB
|
||||
self.context
|
||||
.db
|
||||
.transaction::<_, (), anyhow::Error>(|txn| {
|
||||
Box::pin(async move {
|
||||
computed_permission::Entity::delete_many()
|
||||
.filter(computed_permission::Column::UserId.eq(user_id))
|
||||
.filter(computed_permission::Column::ServerId.eq(server_id))
|
||||
.exec(txn)
|
||||
.await?;
|
||||
if !computed_permissions.is_empty() {
|
||||
computed_permission::Entity::insert_many(computed_permissions)
|
||||
.exec(txn)
|
||||
.await?;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user