From 77d11a82105eea459d9dbdb3c5cd46ed5a2b6c7c Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 19 Jan 2021 17:47:48 +0100 Subject: [PATCH] Can set security settings --- src/controllers/routes.rs | 1 + src/controllers/settings_controller.rs | 23 ++++++++++++++++++ src/data/mod.rs | 3 ++- src/data/security_settings.rs | 32 ++++++++++++++++++++++++++ src/helpers/account_helper.rs | 12 ++++++++++ 5 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/data/security_settings.rs diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index e5df7da..5990429 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -99,6 +99,7 @@ pub fn get_routes() -> Vec { Route::post("/settings/get_language", Box::new(settings_controller::get_language)), Route::post("/settings/set_language", Box::new(settings_controller::set_language)), Route::post("/settings/get_security", Box::new(settings_controller::get_security)), + Route::post("/settings/set_security", Box::new(settings_controller::set_security)), // Friends controller Route::post("/friends/getList", Box::new(friends_controller::get_list)), diff --git a/src/controllers/settings_controller.rs b/src/controllers/settings_controller.rs index c7bb7b3..03f137e 100644 --- a/src/controllers/settings_controller.rs +++ b/src/controllers/settings_controller.rs @@ -10,6 +10,7 @@ use crate::controllers::routes::RequestResult; use crate::data::general_settings::GeneralSettings; use crate::data::http_request_handler::HttpRequestHandler; use crate::data::lang_settings::LangSettings; +use crate::data::security_settings::{SecurityQuestion, SecuritySettings}; use crate::data::user::UserPageStatus; use crate::helpers::{account_helper, user_helper}; use crate::helpers::virtual_directory_helper::VirtualDirType; @@ -93,4 +94,26 @@ pub fn get_security(r: &mut HttpRequestHandler) -> RequestResult { let user = user_helper::find_user_by_id(r.user_id_ref()?)?; r.set_response(SecuritySettingsAPI::new(&user)) +} + +/// Set security settings +pub fn set_security(r: &mut HttpRequestHandler) -> RequestResult { + r.need_user_password("password")?; + + let new_settings = SecuritySettings { + id: r.user_id()?, + question1: SecurityQuestion::new( + &Option::Some(r.post_content("security_question_1", 0, false)?), + &Option::Some(r.post_content("security_answer_1", 0, false)?), + ), + + question2: SecurityQuestion::new( + &Option::Some(r.post_content("security_question_2", 0, false)?), + &Option::Some(r.post_content("security_answer_2", 0, false)?), + ), + }; + + account_helper::set_security_settings(&new_settings)?; + + r.success("Security settings update.") } \ No newline at end of file diff --git a/src/data/mod.rs b/src/data/mod.rs index 00bf773..d9d0862 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -31,4 +31,5 @@ pub mod account_export; pub mod user_like; pub mod survey_response; pub mod general_settings; -pub mod lang_settings; \ No newline at end of file +pub mod lang_settings; +pub mod security_settings; \ No newline at end of file diff --git a/src/data/security_settings.rs b/src/data/security_settings.rs new file mode 100644 index 0000000..4d8e289 --- /dev/null +++ b/src/data/security_settings.rs @@ -0,0 +1,32 @@ +//! # Security settings +//! +//! Security settings of a user + +use crate::data::user::UserID; + +pub struct SecurityQuestion(Option, Option); + +impl SecurityQuestion +{ + pub fn new(question: &Option, answer: &Option) -> SecurityQuestion + { + SecurityQuestion(question.clone(), answer.clone()) + } + + pub fn question(&self) -> Option + { + self.0.clone() + } + + pub fn answer(&self) -> Option + { + self.1.clone() + } +} + +pub struct SecuritySettings +{ + pub id: UserID, + pub question1: SecurityQuestion, + pub question2: SecurityQuestion, +} \ No newline at end of file diff --git a/src/helpers/account_helper.rs b/src/helpers/account_helper.rs index 7c9e749..32b78b6 100644 --- a/src/helpers/account_helper.rs +++ b/src/helpers/account_helper.rs @@ -12,6 +12,7 @@ use crate::helpers::{comments_helper, conversations_helper, database, friends_he use crate::helpers::database::{DeleteQuery, InsertQuery, QueryInfo}; use crate::utils::crypt_utils::{crypt_pass, rand_str}; use crate::utils::date_utils::{mysql_date, time}; +use crate::data::security_settings::SecuritySettings; /// Account helper /// @@ -220,6 +221,17 @@ pub fn set_language_settings(settings: &LangSettings) -> ResultBoxError { .exec() } +/// Set new security settings +pub fn set_security_settings(settings: &SecuritySettings) -> ResultBoxError { + database::UpdateInfo::new(USERS_TABLE) + .cond_user_id("ID", &settings.id) + .set_opt_str("question1", settings.question1.question()) + .set_opt_str("reponse1", settings.question1.answer()) + .set_opt_str("question2", settings.question2.question()) + .set_opt_str("reponse2", settings.question2.answer()) + .exec() +} + /// Export an account's data pub fn export(user_id: &UserID) -> ResultBoxError { let mut data = AccountExport {