1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-25 23:09:22 +00:00

Administrators can change user email address

This commit is contained in:
Pierre HUBERT 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
@ -36,3 +36,30 @@ pub fn get_single(r: &mut HttpRequestHandler) -> RequestResult {
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()
}

View File

@ -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}" }
}

View File

@ -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 {

View File

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

View File

@ -397,6 +397,7 @@ pub fn get_routes() -> Vec<Route> {
// 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)),
]
}