1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-07-06 15:52:48 +00:00

Administrators can change user email address

This commit is contained in:
2021-07-13 16:54:38 +02:00
parent d0f87467ec
commit 71254c47f1
5 changed files with 45 additions and 4 deletions

View File

@ -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()
}