mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-06-20 16:35:17 +00:00
Can create new admin accounts from command line
This commit is contained in:
73
src/helpers/admin_account_helper.rs
Normal file
73
src/helpers/admin_account_helper.rs
Normal file
@ -0,0 +1,73 @@
|
||||
//! # Administration accounts helper
|
||||
//!
|
||||
//! @author Pierre Hubert
|
||||
|
||||
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};
|
||||
use crate::data::error::{ExecError, Res};
|
||||
use crate::helpers::database;
|
||||
use crate::utils::crypt_utils::rand_str;
|
||||
use crate::utils::date_utils::time;
|
||||
|
||||
/// Create a new admin account
|
||||
pub fn create(new_admin: &NewAdmin) -> Res<AdminID> {
|
||||
if find_admin_by_email(&new_admin.email).is_ok() {
|
||||
return Err(ExecError::boxed_new("An other admin account already holds the same email address!"));
|
||||
}
|
||||
|
||||
database::InsertQuery::new(ADMIN_LIST_TABLE)
|
||||
.add_u64("time_create", time())
|
||||
.add_str("name", &new_admin.name)
|
||||
.add_str("email", &new_admin.email)
|
||||
.insert_expect_result()
|
||||
.map(|i| AdminID::new(i))
|
||||
}
|
||||
|
||||
|
||||
/// Get admin information by admin email address
|
||||
pub fn find_admin_by_email(email: &str) -> Res<Admin> {
|
||||
database::QueryInfo::new(ADMIN_LIST_TABLE)
|
||||
.cond("email", email)
|
||||
.query_row(db_to_admin)
|
||||
}
|
||||
|
||||
/// Create a new reset token for an admin
|
||||
pub fn create_new_reset_token(id: AdminID) -> Res<AdminResetToken> {
|
||||
let token = AdminResetToken {
|
||||
token: rand_str(ADMIN_RESET_TOKEN_LENGTH),
|
||||
expire: time() + ADMIN_RESET_TOKEN_LIFETIME,
|
||||
};
|
||||
|
||||
database::UpdateInfo::new(ADMIN_LIST_TABLE)
|
||||
.cond_admin_id("id", id)
|
||||
.set_str("reset_token", &token.token)
|
||||
.set_u64("reset_token_expire", token.expire)
|
||||
.exec()?;
|
||||
|
||||
Ok(token)
|
||||
}
|
||||
|
||||
/// Turn a database entry into an admin structure
|
||||
fn db_to_admin(row: &database::RowResult) -> Res<Admin> {
|
||||
let reset_token_expire = row.get_optional_u64("reset_token_expire")?
|
||||
.unwrap_or(0);
|
||||
|
||||
let reset_token = if reset_token_expire < time() {
|
||||
None
|
||||
} else {
|
||||
Some(AdminResetToken {
|
||||
token: row.get_str("reset_token")?,
|
||||
expire: row.get_u64("reset_token_expire")?,
|
||||
})
|
||||
};
|
||||
|
||||
|
||||
Ok(Admin {
|
||||
id: row.get_admin_id("id")?,
|
||||
time_create: row.get_u64("time_create")?,
|
||||
name: row.get_str("name")?,
|
||||
email: row.get_str("email")?,
|
||||
reset_token,
|
||||
})
|
||||
}
|
@ -8,6 +8,7 @@ use chrono::{TimeZone, Utc};
|
||||
use mysql::{Binary, Pool, ResultSet, Value};
|
||||
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};
|
||||
@ -376,6 +377,11 @@ impl<'a> RowResult<'a> {
|
||||
Ok(UserID::new(self.get_u64(name)?))
|
||||
}
|
||||
|
||||
/// Get the ID of an admin included in the request
|
||||
pub fn get_admin_id(&self, name: &str) -> ResultBoxError<AdminID> {
|
||||
Ok(AdminID::new(self.get_u64(name)?))
|
||||
}
|
||||
|
||||
/// Get the ID of a group included in the response
|
||||
pub fn get_group_id(&self, name: &str) -> ResultBoxError<GroupID> {
|
||||
Ok(GroupID::new(self.get_u64(name)?))
|
||||
@ -903,6 +909,12 @@ impl UpdateInfo {
|
||||
self
|
||||
}
|
||||
|
||||
/// Filter with an admin id
|
||||
pub fn cond_admin_id(mut self, name: &str, val: AdminID) -> Self {
|
||||
self.cond.insert(name.to_string(), Value::UInt(val.id()));
|
||||
self
|
||||
}
|
||||
|
||||
/// Filter with a group id
|
||||
pub fn cond_group_id(mut self, name: &str, val: &GroupID) -> UpdateInfo {
|
||||
self.cond.insert(name.to_string(), Value::UInt(val.id()));
|
||||
|
@ -21,4 +21,5 @@ pub mod calls_helper;
|
||||
pub mod push_notifications_helper;
|
||||
pub mod independent_push_notifications_service_helper;
|
||||
pub mod firebase_notifications_helper;
|
||||
pub mod forez_presence_helper;
|
||||
pub mod forez_presence_helper;
|
||||
pub mod admin_account_helper;
|
Reference in New Issue
Block a user