From 312e6fb949fc3926c6a22356b0722601a2be862e Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 17 Feb 2021 18:30:36 +0100 Subject: [PATCH] Can get current user email address easily --- src/api_data/mod.rs | 1 + src/api_data/user_mail_address.rs | 20 ++++++++++++++++++++ src/controllers/account_controller.rs | 7 +++++++ src/routes.rs | 1 + 4 files changed, 29 insertions(+) create mode 100644 src/api_data/user_mail_address.rs diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index 8b7b5c4..a885c98 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -11,6 +11,7 @@ pub mod server_config; pub mod http_error; pub mod login_success; pub mod current_user_id; +pub mod user_mail_address; pub mod user_info; pub mod custom_emoji; pub mod res_find_user_by_virtual_directory; diff --git a/src/api_data/user_mail_address.rs b/src/api_data/user_mail_address.rs new file mode 100644 index 0000000..29cc002 --- /dev/null +++ b/src/api_data/user_mail_address.rs @@ -0,0 +1,20 @@ +//! # Get current user email address result +//! +//! @author Pierre Hubert + +use serde::Serialize; + +use crate::data::user::User; + +#[derive(Serialize)] +pub struct UserMailAddressAPI { + mail: String, +} + +impl UserMailAddressAPI { + pub fn new(user: &User) -> Self { + Self { + mail: user.email.to_string() + } + } +} \ No newline at end of file diff --git a/src/controllers/account_controller.rs b/src/controllers/account_controller.rs index 6590b66..c288099 100644 --- a/src/controllers/account_controller.rs +++ b/src/controllers/account_controller.rs @@ -7,6 +7,7 @@ use crate::api_data::res_check_email_exists::ResCheckEmailExists; use crate::api_data::res_check_security_answers::ResCheckSecurityAnswers; use crate::api_data::res_check_security_questions_exists::ResCheckSecurityQuestionsExists; use crate::api_data::res_get_security_questions::ResGetSecurityQuestions; +use crate::api_data::user_mail_address::UserMailAddressAPI; use crate::constants::PASSWORD_RESET_TOKEN_LENGTH; use crate::data::base_request_handler::BaseRequestHandler; use crate::data::error::ResultBoxError; @@ -110,6 +111,12 @@ pub fn user_id(request: &mut HttpRequestHandler) -> RequestResult { request.set_response(CurrentUserID::new(&request.user_id()?)) } +/// Get current user email address +pub fn get_mail(r: &mut HttpRequestHandler) -> RequestResult { + let user = user_helper::find_user_by_id(r.user_id_ref()?)?; + r.set_response(UserMailAddressAPI::new(&user)) +} + /// Check out whether an email address exists or not pub fn exists_mail(r: &mut HttpRequestHandler) -> RequestResult { let email = r.post_email("email")?; diff --git a/src/routes.rs b/src/routes.rs index 08ab83c..71c40fe 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -140,6 +140,7 @@ pub fn get_routes() -> Vec { Route::post("/user/disconnectUSER", Box::new(account_controller::logout_user)), // TODO : remove Route::post("/account/disconnect_all_devices", Box::new(account_controller::disconnect_all_devices)), Route::post("/account/id", Box::new(account_controller::user_id)), + Route::post("/account/mail", Box::new(account_controller::get_mail)), Route::post("/user/getCurrentUserID", Box::new(account_controller::user_id)), // TODO : remove Route::limited_post_without_login("/account/exists_email", Box::new(account_controller::exists_mail), LimitPolicy::ANY(30)), Route::limited_post_without_login("/account/has_security_questions", Box::new(account_controller::has_security_questions), LimitPolicy::FAILURE(10)),