From e76b3180e7a5e3cf78d8316a735729f410729add Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 13 Jul 2020 13:15:26 +0200 Subject: [PATCH] Can check if security questions has been defined --- src/api_data/mod.rs | 3 ++- src/api_data/res_check_email_exists.rs | 1 - .../res_check_security_questions_exists.rs | 15 +++++++++++++++ src/controllers/account_controller.rs | 5 ++--- src/data/user.rs | 12 ++++++++++++ src/helpers/user_helper.rs | 4 ++++ 6 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 src/api_data/res_check_security_questions_exists.rs diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index f229695..8c8313d 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -43,4 +43,5 @@ pub mod res_count_all_unreads; pub mod notification_api; pub mod user_membership_api; mod type_container_api; -pub mod res_check_email_exists; \ No newline at end of file +pub mod res_check_email_exists; +pub mod res_check_security_questions_exists; \ No newline at end of file diff --git a/src/api_data/res_check_email_exists.rs b/src/api_data/res_check_email_exists.rs index ba73a6a..e2bf613 100644 --- a/src/api_data/res_check_email_exists.rs +++ b/src/api_data/res_check_email_exists.rs @@ -4,7 +4,6 @@ use serde::Serialize; #[derive(Serialize)] -#[allow(non_snake_case)] pub struct ResCheckEmailExists { exists: bool } diff --git a/src/api_data/res_check_security_questions_exists.rs b/src/api_data/res_check_security_questions_exists.rs new file mode 100644 index 0000000..3ed2aff --- /dev/null +++ b/src/api_data/res_check_security_questions_exists.rs @@ -0,0 +1,15 @@ +//! # Check if security questions are set for a user +//! +//! @author Pierre Hubert +use serde::Serialize; + +#[derive(Serialize)] +pub struct ResCheckSecurityQuestionsExists { + defined: bool +} + +impl ResCheckSecurityQuestionsExists { + pub fn new(defined: bool) -> ResCheckSecurityQuestionsExists { + ResCheckSecurityQuestionsExists { defined } + } +} \ No newline at end of file diff --git a/src/controllers/account_controller.rs b/src/controllers/account_controller.rs index 8b17641..a112ac5 100644 --- a/src/controllers/account_controller.rs +++ b/src/controllers/account_controller.rs @@ -1,6 +1,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::controllers::routes::RequestResult; use crate::data::error::ResultBoxError; use crate::data::http_request_handler::HttpRequestHandler; @@ -79,7 +80,5 @@ pub fn exists_mail(r: &mut HttpRequestHandler) -> RequestResult { pub fn has_security_questions(r: &mut HttpRequestHandler) -> RequestResult { let user = r.post_user_info_from_email("email")?; - // TODO : continue implementation - println!("{:#?}", user); - r.success("implement me") + r.set_response(ResCheckSecurityQuestionsExists::new(user.has_security_questions())) } \ No newline at end of file diff --git a/src/data/user.rs b/src/data/user.rs index 884228d..9be0145 100644 --- a/src/data/user.rs +++ b/src/data/user.rs @@ -68,6 +68,10 @@ pub struct User { pub block_comments_on_his_page: bool, pub allow_posts_from_friends: bool, pub account_creation_time: u64, + pub security_question_1: Option, + pub security_answer_1: Option, + pub security_question_2: Option, + pub security_answer_2: Option, } impl User { @@ -86,4 +90,12 @@ impl User { pub fn has_account_image(&self) -> bool { self.account_image_path.is_some() } + + /// Check out whether security questions have been defined for this user or not + pub fn has_security_questions(&self) -> bool { + self.security_question_1.is_some() && + self.security_answer_1.is_some() && + self.security_question_2.is_some() && + self.security_answer_2.is_some() + } } \ No newline at end of file diff --git a/src/helpers/user_helper.rs b/src/helpers/user_helper.rs index 488c9b4..ba3b266 100644 --- a/src/helpers/user_helper.rs +++ b/src/helpers/user_helper.rs @@ -64,6 +64,10 @@ fn exec_get_user_query(query: database::QueryInfo) -> ResultBoxError { block_comments_on_his_page: res.get_legacy_bool("bloquecommentaire")?, allow_posts_from_friends: res.get_legacy_bool("autoriser_post_amis")?, account_creation_time: res.get_date_as_time("date_creation")?, + security_question_1: res.get_optional_str("question1")?, + security_answer_1: res.get_optional_str("reponse1")?, + security_question_2: res.get_optional_str("question2")?, + security_answer_2: res.get_optional_str("reponse2")?, }) }) }