From 1974c782b57e0cde60b22400b4bfba4a3d9596b0 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 13 Jul 2020 13:23:37 +0200 Subject: [PATCH] Can get security questions --- src/api_data/mod.rs | 3 ++- src/api_data/res_get_security_questions.rs | 21 +++++++++++++++++++++ src/controllers/account_controller.rs | 12 ++++++++++++ src/controllers/routes.rs | 1 + 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/api_data/res_get_security_questions.rs diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index 8c8313d..0ac2cb9 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -44,4 +44,5 @@ pub mod notification_api; pub mod user_membership_api; mod type_container_api; pub mod res_check_email_exists; -pub mod res_check_security_questions_exists; \ No newline at end of file +pub mod res_check_security_questions_exists; +pub mod res_get_security_questions; \ No newline at end of file diff --git a/src/api_data/res_get_security_questions.rs b/src/api_data/res_get_security_questions.rs new file mode 100644 index 0000000..3ab5685 --- /dev/null +++ b/src/api_data/res_get_security_questions.rs @@ -0,0 +1,21 @@ +//! # Get security questions results +//! +//! @author Pierre Hubert +use serde::Serialize; + +use crate::data::user::User; + +#[derive(Serialize)] +pub struct ResGetSecurityQuestions { + questions: Vec +} + +impl ResGetSecurityQuestions { + pub fn new(user: &User) -> ResGetSecurityQuestions { + let mut questions = Vec::new(); + questions.push(user.security_question_1.clone().unwrap_or(String::new())); + questions.push(user.security_question_2.clone().unwrap_or(String::new())); + + ResGetSecurityQuestions { questions } + } +} \ No newline at end of file diff --git a/src/controllers/account_controller.rs b/src/controllers/account_controller.rs index a112ac5..8fde8c7 100644 --- a/src/controllers/account_controller.rs +++ b/src/controllers/account_controller.rs @@ -2,6 +2,7 @@ use crate::api_data::current_user_id::CurrentUserID; use crate::api_data::login_success::LoginSuccess; use crate::api_data::res_check_email_exists::ResCheckEmailExists; use crate::api_data::res_check_security_questions_exists::ResCheckSecurityQuestionsExists; +use crate::api_data::res_get_security_questions::ResGetSecurityQuestions; use crate::controllers::routes::RequestResult; use crate::data::error::ResultBoxError; use crate::data::http_request_handler::HttpRequestHandler; @@ -81,4 +82,15 @@ pub fn has_security_questions(r: &mut HttpRequestHandler) -> RequestResult { let user = r.post_user_info_from_email("email")?; r.set_response(ResCheckSecurityQuestionsExists::new(user.has_security_questions())) +} + +/// Get the security questions of a user +pub fn get_security_questions(r: &mut HttpRequestHandler) -> RequestResult { + let user = r.post_user_info_from_email("email")?; + + if !user.has_security_questions() { + r.forbidden("Specified user has not defined security questions!".to_string())?; + } + + r.set_response(ResGetSecurityQuestions::new(&user)) } \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index f3b96f5..1309374 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -76,6 +76,7 @@ pub fn get_routes() -> Vec { Route::post("/user/getCurrentUserID", Box::new(account_controller::user_id)), Route::post_without_login("/account/exists_email", Box::new(account_controller::exists_mail)), Route::post_without_login("/account/has_security_questions", Box::new(account_controller::has_security_questions)), + Route::post_without_login("/account/get_security_questions", Box::new(account_controller::get_security_questions)), // User controller Route::post_without_login("/user/getInfo", Box::new(user_controller::get_single)),