pub mod groupe;
pub mod groupes_droits;
pub mod users_groupes;
use sea_orm::{ColumnTrait, ConnectionTrait, EntityTrait, QueryFilter};
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize)]
pub struct Permission {
pub resource_key: String,
pub can_create: bool,
pub can_read: bool,
pub can_update: bool,
pub can_delete: bool,
pub can_update_own: bool,
pub can_delete_own: bool,
}
impl Permission {
pub fn zeroed(resource_key: String) -> Self {
Self {
resource_key,
can_create: false,
can_read: false,
can_update: false,
can_delete: false,
can_update_own: false,
can_delete_own: false,
}
}
pub fn merge_from(&mut self, other: &Self) {
self.can_create |= other.can_create;
self.can_read |= other.can_read;
self.can_update |= other.can_update;
self.can_delete |= other.can_delete;
self.can_update_own |= other.can_update_own;
self.can_delete_own |= other.can_delete_own;
}
}
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct Groupe {
pub id: i32,
pub nom: String,
pub permissions: Vec<Permission>,
}
pub async fn prune_orphan_droits<C: ConnectionTrait>(
db: &C,
valid_keys: &[&str],
) -> Result<u64, sea_orm::DbErr> {
if valid_keys.is_empty() {
return Ok(0);
}
let res = groupes_droits::Entity::delete_many()
.filter(
groupes_droits::Column::ResourceKey.is_not_in(valid_keys.iter().map(|k| k.to_string())),
)
.exec(db)
.await?;
Ok(res.rows_affected)
}
pub async fn pull_groupes_db<C: ConnectionTrait>(
db: &C,
user_id: crate::utils::pk::Pk,
) -> Vec<Groupe> {
let trace_err = |what: &str, e: &sea_orm::DbErr| {
tracing::error!(user_id = %user_id, error = %e, "{what} failed — no rights granted for this request");
};
let rows = match users_groupes::Entity::find()
.filter(users_groupes::Column::UserId.eq(user_id))
.find_also_related(groupe::Entity)
.all(db)
.await
{
Ok(rows) => rows,
Err(e) => {
trace_err("loading groups", &e);
return Vec::new();
}
};
let mut groupes: Vec<Groupe> = rows
.into_iter()
.filter_map(|(_, g)| g)
.map(|g| Groupe {
id: g.id,
nom: g.nom,
permissions: Vec::new(),
})
.collect();
if groupes.is_empty() {
return groupes;
}
let droits = match groupes_droits::Entity::find()
.filter(groupes_droits::Column::GroupeId.is_in(groupes.iter().map(|g| g.id)))
.all(db)
.await
{
Ok(droits) => droits,
Err(e) => {
trace_err("loading rights", &e);
return Vec::new();
}
};
for m in droits {
if let Some(g) = groupes.iter_mut().find(|g| g.id == m.groupe_id) {
g.permissions.push(Permission {
resource_key: m.resource_key,
can_create: m.can_create,
can_read: m.can_read,
can_update: m.can_update,
can_delete: m.can_delete,
can_update_own: m.can_update_own,
can_delete_own: m.can_delete_own,
});
}
}
groupes
}