1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-07-10 01:22:48 +00:00

Can update admin roles

This commit is contained in:
2021-05-15 09:10:39 +02:00
parent e619d71601
commit d933dadf62
5 changed files with 65 additions and 6 deletions

View File

@ -12,11 +12,11 @@ use crate::utils::date_utils::time;
/// Get the list of roles of a given administrator
pub fn get_roles(id: AdminID) -> Res<Vec<AdminRole>> {
database::QueryInfo::new(ADMIN_ROLES_TABLE)
.cond_admin_id("id", id)
.cond_admin_id("admin_id", id)
.exec(db_to_role)
}
/// Add a new role to a user
/// Add a new role to an admin
pub fn add_role(id: AdminID, role: AdminRole) -> Res {
database::InsertQuery::new(ADMIN_ROLES_TABLE)
.add_admin_id("admin_id", id)
@ -25,6 +25,21 @@ pub fn add_role(id: AdminID, role: AdminRole) -> Res {
.insert_drop_result()
}
/// Remove a role to an admin
pub fn remove_role(id: AdminID, role: AdminRole) -> Res {
database::DeleteQuery::new(ADMIN_ROLES_TABLE)
.cond_admin_id("admin_id", id)
.cond_str("role_id", role.to_id())
.exec()
}
/// Check out whether an admin has a role or not
pub fn has_role(id: AdminID, role: AdminRole) -> Res<bool> {
database::QueryInfo::new(ADMIN_ROLES_TABLE)
.cond_admin_id("admin_id", id)
.cond("role_id", role.to_id())
.exec_count_has_at_least_one_result()
}
fn db_to_role(row: &database::RowResult) -> Res<AdminRole> {
let role_id = row.get_str("role_id")?;

View File

@ -11,7 +11,7 @@ use mysql::prelude::Queryable;
use crate::data::admin::AdminID;
use crate::data::config::{conf, DatabaseConfig};
use crate::data::conversation::ConvID;
use crate::data::error::{ExecError, ResultBoxError};
use crate::data::error::{ExecError, Res, ResultBoxError};
use crate::data::group_id::GroupID;
use crate::data::user::UserID;
@ -272,6 +272,11 @@ impl QueryInfo {
pub fn exec_count(self) -> ResultBoxError<usize> {
count(self)
}
/// Execute count query, checking that there is at least one result
pub fn exec_count_has_at_least_one_result(self) -> Res<bool> {
self.exec_count().map(|r| r > 0)
}
}
/// Struct used to read the result of a request
@ -841,6 +846,11 @@ impl DeleteQuery {
self
}
pub fn cond_admin_id(mut self, key: &str, value: AdminID) -> DeleteQuery {
self.conditions.insert(key.to_string(), Value::from(value.id()));
self
}
pub fn cond_group_id(mut self, key: &str, value: &GroupID) -> DeleteQuery {
self.conditions.insert(key.to_string(), Value::from(value.id()));
self