//! # Settings controller //! //! @author Pierre Hubert use crate::api_data::general_settings_api::GeneralSettingsAPI; use crate::api_data::language_settings_api::LanguageSettingsAPI; use crate::api_data::security_settings_api::SecuritySettingsAPI; use crate::constants::SUPPORTED_LANGUAGES; 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; /// Get the general settings of the user pub fn get_general(r: &mut HttpRequestHandler) -> RequestResult { let user = user_helper::find_user_by_id(r.user_id_ref()?)?; r.set_response(GeneralSettingsAPI::new(&user)) } /// Set the general settings of the user pub fn set_general(r: &mut HttpRequestHandler) -> RequestResult { let page_status = match (r.post_bool("isPublic")?, r.post_bool("isOpen")?) { (true, true) => UserPageStatus::OPEN, (true, false) => UserPageStatus::PUBLIC, (_, _) => UserPageStatus::PRIVATE, }; let personal_website = r.post_url_opt("personnalWebsite", false)?; let virtual_directory = r.post_checked_virtual_directory_opt( "virtualDirectory", r.user_id_ref()?.id(), VirtualDirType::USER)?; let new_settings = GeneralSettings { id: r.user_id()?, first_name: r.post_content("firstName", 3, true)?, last_name: r.post_content("lastName", 3, true)?, page_status, block_comments: !r.post_bool("allowComments")?, allow_posts_from_friends: r.post_bool("allowPostsFromFriends")?, friends_list_public: r.post_bool("publicFriendsList")?, personal_website, virtual_directory, allow_mails: r.post_bool("allow_comunic_mails")?, public_note: Some(r.post_content("publicNote", 0, false)?), }; account_helper::set_general(&new_settings)?; r.success("Settings updated") } /// Check the availability of a virtual directory pub fn check_virtual_directory(r: &mut HttpRequestHandler) -> RequestResult { // We can do this check just by getting it, there is no need to store it in a variable r.post_checked_virtual_directory_opt("directory", r.user_id()?.id(), VirtualDirType::USER)?; r.success("The directory is available!") } /// Get the current language of the user pub fn get_language(r: &mut HttpRequestHandler) -> RequestResult { let user = user_helper::find_user_by_id(r.user_id_ref()?)?; r.set_response(LanguageSettingsAPI::new(&user)) } /// Set the current language of the user pub fn set_language(r: &mut HttpRequestHandler) -> RequestResult { let lang = r.post_string("lang")?; if !SUPPORTED_LANGUAGES.contains(&lang.as_str()) { r.forbidden("Language not supported!".to_string())?; } let new_settings = LangSettings { id: r.user_id()?, lang, }; account_helper::set_language_settings(&new_settings)?; r.success("Language settings updated.") } /// Get security settings pub fn get_security(r: &mut HttpRequestHandler) -> RequestResult { r.need_user_password("password")?; 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.") }