diff --git a/src/controllers/admin/admin_users_controller.rs b/src/controllers/admin/admin_users_controller.rs index 621c911..f0d7531 100644 --- a/src/controllers/admin/admin_users_controller.rs +++ b/src/controllers/admin/admin_users_controller.rs @@ -8,8 +8,8 @@ use crate::constants::admin::AdminRole; use crate::data::admin_action_log::AdminAction; use crate::data::base_request_handler::BaseRequestHandler; use crate::data::http_request_handler::HttpRequestHandler; +use crate::helpers::{account_helper, user_helper}; use crate::helpers::admin_log_helper::log_admin_action; -use crate::helpers::user_helper; use crate::routes::RequestResult; /// Search for user @@ -35,4 +35,31 @@ pub fn get_single(r: &mut HttpRequestHandler) -> RequestResult { AdminAction::AccessUserPage { user_id, user_name: user.full_name() })?; r.set_response(AdminUserInfoAPI::new(user)) +} + +/// Change the email address of a Comunic user +pub fn change_email_address(r: &mut HttpRequestHandler) -> RequestResult { + r.check_admin_has_role(AdminRole::MANAGE_USERS)?; + + let user_id = r.post_user_id("user_id")?; + let user = user_helper::find_user_by_id(&user_id)?; + let new_mail = r.post_email("new_mail")?; + + // We check if the email address is already used + if user_helper::find_user_by_email(&new_mail).is_ok() { + r.bad_request(format!("The email address {} is already attributed!", new_mail))?; + } + + // Do the update + account_helper::set_email(user_id, &new_mail)?; + + log_admin_action(r.admin_id()?, &r.remote_ip(), + AdminAction::ChangedEmailAddress { + user_id, + user_name: user.full_name(), + old_mail: user.email, + new_mail, + })?; + + r.ok() } \ No newline at end of file diff --git a/src/data/admin_action_log.rs b/src/data/admin_action_log.rs index 3008460..24ac974 100644 --- a/src/data/admin_action_log.rs +++ b/src/data/admin_action_log.rs @@ -15,6 +15,7 @@ pub enum AdminAction { AddAdminRole { target: AdminID, role: String }, RemoveAdminRole { target: AdminID, role: String }, AccessUserPage { user_id: UserID, user_name: String }, + ChangedEmailAddress { user_id: UserID, user_name: String, old_mail: String, new_mail: String }, UnsupportedAction { raw_data: String }, } @@ -47,7 +48,11 @@ impl AdminAction { AdminAction::RemoveAdminRole { .. } => { "Removed a role from [admin]{target}[/admin] => {role}" } - AdminAction::AccessUserPage { .. } => { "Administrator accessed information of user #{user_id} '{user_name}'." } + AdminAction::AccessUserPage { .. } => + { "Accessed information of user #{user_id} '{user_name}'." } + + AdminAction::ChangedEmailAddress { .. } => + { "Changed email address of user #{user_id} '{user_name}' from {old_mail} to {new_mail}" } AdminAction::UnsupportedAction { .. } => { "Unsupported action. Raw data: {raw_data}" } } diff --git a/src/data/user.rs b/src/data/user.rs index 361596c..ac0435f 100644 --- a/src/data/user.rs +++ b/src/data/user.rs @@ -8,7 +8,7 @@ use crate::utils::user_data_utils::user_data_url; ///! User information ///! ///! @author Pierre Hubert -#[derive(Clone, PartialEq, Eq, Debug)] +#[derive(Clone, PartialEq, Eq, Debug, Copy)] pub struct UserID(u64); impl UserID { diff --git a/src/helpers/account_helper.rs b/src/helpers/account_helper.rs index 6eada9a..2c20c1f 100644 --- a/src/helpers/account_helper.rs +++ b/src/helpers/account_helper.rs @@ -239,6 +239,14 @@ pub fn set_general(settings: &GeneralSettings) -> ResultBoxError { .exec() } +/// Set the email address of a user +pub fn set_email(user_id: UserID, new_mail: &str) -> Res { + database::UpdateInfo::new(USERS_TABLE) + .cond_user_id("ID", &user_id) + .set_str("mail", new_mail) + .exec() +} + /// Set new language settings pub fn set_language_settings(settings: &LangSettings) -> ResultBoxError { database::UpdateInfo::new(USERS_TABLE) diff --git a/src/routes.rs b/src/routes.rs index 0f1e6e1..9e282be 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -397,6 +397,7 @@ pub fn get_routes() -> Vec { // Admin users management controller Route::admin_post("/admin/users/search", Box::new(admin_users_controller::search)), - Route::admin_post("/admin/users/info", Box::new(admin_users_controller::get_single)) + Route::admin_post("/admin/users/info", Box::new(admin_users_controller::get_single)), + Route::admin_post("/admin/users/change_email_address", Box::new(admin_users_controller::change_email_address)), ] } \ No newline at end of file