mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-07-11 10:02:48 +00:00
Start to implement admin roles logic
This commit is contained in:
@ -6,7 +6,7 @@ use crate::constants::{ADMIN_RESET_TOKEN_LENGTH, ADMIN_RESET_TOKEN_LIFETIME};
|
||||
use crate::constants::database_tables_names::ADMIN_LIST_TABLE;
|
||||
use crate::data::admin::{Admin, AdminID, AdminResetToken, NewAdmin, NewAdminGeneralSettings};
|
||||
use crate::data::error::{ExecError, Res};
|
||||
use crate::helpers::database;
|
||||
use crate::helpers::{admin_roles_helper, database};
|
||||
use crate::utils::crypt_utils::rand_str;
|
||||
use crate::utils::date_utils::time;
|
||||
|
||||
@ -85,12 +85,14 @@ fn db_to_admin(row: &database::RowResult) -> Res<Admin> {
|
||||
})
|
||||
};
|
||||
|
||||
let admin_id = row.get_admin_id("id")?;
|
||||
|
||||
Ok(Admin {
|
||||
id: row.get_admin_id("id")?,
|
||||
id: admin_id,
|
||||
time_create: row.get_u64("time_create")?,
|
||||
name: row.get_str("name")?,
|
||||
email: row.get_str("email")?,
|
||||
reset_token,
|
||||
roles: admin_roles_helper::get_roles(admin_id)?,
|
||||
})
|
||||
}
|
37
src/helpers/admin_roles_helper.rs
Normal file
37
src/helpers/admin_roles_helper.rs
Normal file
@ -0,0 +1,37 @@
|
||||
//! # Admin roles helper
|
||||
//!
|
||||
//! @author Pierre Hubert
|
||||
|
||||
use crate::constants::admin::AdminRole;
|
||||
use crate::constants::database_tables_names::ADMIN_ROLES_TABLE;
|
||||
use crate::data::admin::AdminID;
|
||||
use crate::data::error::{ExecError, Res};
|
||||
use crate::helpers::database;
|
||||
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)
|
||||
.exec(db_to_role)
|
||||
}
|
||||
|
||||
/// Add a new role to a user
|
||||
pub fn add_role(id: AdminID, role: AdminRole) -> Res {
|
||||
database::InsertQuery::new(ADMIN_ROLES_TABLE)
|
||||
.add_admin_id("admin_id", id)
|
||||
.add_str("role_id", role.to_id())
|
||||
.add_u64("time_insert", time())
|
||||
.insert_drop_result()
|
||||
}
|
||||
|
||||
fn db_to_role(row: &database::RowResult) -> Res<AdminRole> {
|
||||
let role_id = row.get_str("role_id")?;
|
||||
|
||||
let role = AdminRole::from_id(&role_id);
|
||||
|
||||
match role {
|
||||
None => Err(ExecError::boxed_string(format!("Role {} from database not found in the list!", role_id))),
|
||||
Some(r) => Ok(r)
|
||||
}
|
||||
}
|
@ -26,4 +26,5 @@ pub mod admin_account_helper;
|
||||
pub mod admin_account_key_helper;
|
||||
pub mod admin_access_token_helper;
|
||||
pub mod admin_key_registration_challenges_helper;
|
||||
pub mod admin_key_authentication_challenges_helper;
|
||||
pub mod admin_key_authentication_challenges_helper;
|
||||
pub mod admin_roles_helper;
|
Reference in New Issue
Block a user