diff --git a/src/api_data/admin/admin_search_user_result.rs b/src/api_data/admin/admin_search_user_result_api.rs similarity index 100% rename from src/api_data/admin/admin_search_user_result.rs rename to src/api_data/admin/admin_search_user_result_api.rs diff --git a/src/api_data/admin/admin_user_info_api.rs b/src/api_data/admin/admin_user_info_api.rs new file mode 100644 index 0000000..77a92c2 --- /dev/null +++ b/src/api_data/admin/admin_user_info_api.rs @@ -0,0 +1,54 @@ +//! # Admin API : User information +//! +//! Get information about a single user +//! +//! @author Pierre Hubert + +use crate::data::user::User; + +#[derive(serde::Serialize)] +pub struct AdminUserInfoAPI { + id: u64, + first_name: String, + last_name: String, + email: String, + account_creation_time: u64, + last_activity: u64, + page_visibility: &'static str, + directory: Option, + account_image: String, + friend_list_public: bool, + is_email_public: bool, + personal_website: Option, + public_note: Option, + location: Option, + block_comments: bool, + allow_posts_from_friends: bool, + allow_mails: bool, + lang: String, +} + +impl AdminUserInfoAPI { + pub fn new(user: User) -> Self { + Self { + account_image: user.account_image_url_for_admin(), + friend_list_public: user.public_friends_list, + is_email_public: user.is_email_public, + personal_website: user.personal_website, + public_note: user.public_note, + location: user.location, + block_comments: user.block_comments_on_his_page, + allow_posts_from_friends: user.allow_posts_from_friends, + allow_mails: user.allow_mails, + id: user.id.id(), + first_name: user.first_name, + last_name: user.last_name, + email: user.email, + account_creation_time: user.account_creation_time, + last_activity: user.last_activity, + page_visibility: user.user_page_visibility.to_api(), + directory: user.virtual_directory, + lang: user.lang, + } + } +} \ No newline at end of file diff --git a/src/api_data/admin/mod.rs b/src/api_data/admin/mod.rs index d9d1bae..32e6178 100644 --- a/src/api_data/admin/mod.rs +++ b/src/api_data/admin/mod.rs @@ -11,4 +11,5 @@ pub mod admin_res_create_reset_token; pub mod admin_role_api; pub mod admin_res_create_account; pub mod admin_log_api; -pub mod admin_search_user_result; \ No newline at end of file +pub mod admin_search_user_result_api; +pub mod admin_user_info_api; \ No newline at end of file diff --git a/src/api_data/user_info.rs b/src/api_data/user_info.rs index f8bd964..fa4c7cf 100644 --- a/src/api_data/user_info.rs +++ b/src/api_data/user_info.rs @@ -5,7 +5,7 @@ use serde::Serialize; use crate::api_data::custom_emoji::CustomEmojiAPI; use crate::data::error::ResultBoxError; -use crate::data::user::{User, UserID, UserPageStatus}; +use crate::data::user::{User, UserID, UserPageVisibility}; use crate::data::user::AccountImageVisibility::{COMUNIC_USERS, EVERYONE}; use crate::helpers::{background_image_helper, custom_emojies_helper, friends_helper, likes_helper, user_helper}; use crate::helpers::likes_helper::LikeType; @@ -53,8 +53,8 @@ impl APIUserInfo { userID: info.id.id(), firstName: info.first_name.to_string(), lastName: info.last_name.to_string(), - publicPage: info.status != UserPageStatus::PRIVATE, - openPage: info.status == UserPageStatus::OPEN, + publicPage: info.user_page_visibility != UserPageVisibility::PRIVATE, + openPage: info.user_page_visibility == UserPageVisibility::OPEN, virtualDirectory: info.virtual_directory.clone().unwrap_or(String::new()), accountImage: APIUserInfo::get_account_image_url(user_id, info)?, customEmojis: custom_emojies_helper::get_list_user(&info.id)? diff --git a/src/controllers/admin/admin_users_controller.rs b/src/controllers/admin/admin_users_controller.rs index f4f5272..3d2b474 100644 --- a/src/controllers/admin/admin_users_controller.rs +++ b/src/controllers/admin/admin_users_controller.rs @@ -2,12 +2,13 @@ //! //! @author Pierre Hubert -use crate::data::http_request_handler::HttpRequestHandler; -use crate::routes::RequestResult; -use crate::data::base_request_handler::BaseRequestHandler; +use crate::api_data::admin::admin_search_user_result_api::AdminSearchUserResult; +use crate::api_data::admin::admin_user_info_api::AdminUserInfoAPI; use crate::constants::admin::AdminRole; +use crate::data::base_request_handler::BaseRequestHandler; +use crate::data::http_request_handler::HttpRequestHandler; use crate::helpers::user_helper; -use crate::api_data::admin::admin_search_user_result::AdminSearchUserResult; +use crate::routes::RequestResult; /// Search for user pub fn search(r: &mut HttpRequestHandler) -> RequestResult { @@ -19,4 +20,14 @@ pub fn search(r: &mut HttpRequestHandler) -> RequestResult { let results = user_helper::search_user_admin(&name, &email, 50)?; r.set_response(results.into_iter().map(AdminSearchUserResult::new).collect::>()) +} + +/// Get information about a single user +pub fn get_single(r: &mut HttpRequestHandler) -> RequestResult { + r.check_admin_has_role(AdminRole::MANAGE_USERS)?; + + let user_id = r.post_user_id("user_id")?; + let user = user_helper::find_user_by_id(&user_id)?; + + r.set_response(AdminUserInfoAPI::new(user)) } \ No newline at end of file diff --git a/src/controllers/settings_controller.rs b/src/controllers/settings_controller.rs index ff612de..3d9ca30 100644 --- a/src/controllers/settings_controller.rs +++ b/src/controllers/settings_controller.rs @@ -19,7 +19,7 @@ use crate::data::new_custom_emoji::NewCustomEmoji; use crate::data::new_data_conservation_policy::NewDataConservationPolicy; use crate::data::new_notifications_settings::NewNotificationsSettings; use crate::data::security_settings::{SecurityQuestion, SecuritySettings}; -use crate::data::user::{AccountImageVisibility, UserPageStatus}; +use crate::data::user::{AccountImageVisibility, UserPageVisibility}; use crate::helpers::{account_helper, custom_emojies_helper, user_helper}; use crate::helpers::virtual_directory_helper::VirtualDirType; use crate::routes::RequestResult; @@ -35,9 +35,9 @@ pub fn get_general(r: &mut HttpRequestHandler) -> RequestResult { /// 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, + (true, true) => UserPageVisibility::OPEN, + (true, false) => UserPageVisibility::PUBLIC, + (_, _) => UserPageVisibility::PRIVATE, }; let personal_website = r.post_url_opt("personnalWebsite", false)?; diff --git a/src/data/general_settings.rs b/src/data/general_settings.rs index 8c203c9..6f2809e 100644 --- a/src/data/general_settings.rs +++ b/src/data/general_settings.rs @@ -2,13 +2,13 @@ //! //! @author Pierre Hubert -use crate::data::user::{UserID, UserPageStatus}; +use crate::data::user::{UserID, UserPageVisibility}; pub struct GeneralSettings { pub id: UserID, pub first_name: String, pub last_name: String, - pub page_status: UserPageStatus, + pub page_status: UserPageVisibility, pub block_comments: bool, pub allow_posts_from_friends: bool, pub friends_list_public: bool, diff --git a/src/data/user.rs b/src/data/user.rs index 2371773..84ed8ca 100644 --- a/src/data/user.rs +++ b/src/data/user.rs @@ -62,12 +62,22 @@ impl PartialEq for &UserID { } #[derive(Debug, PartialEq)] -pub enum UserPageStatus { +pub enum UserPageVisibility { OPEN, PUBLIC, PRIVATE, } +impl UserPageVisibility { + pub fn to_api(&self) -> &'static str { + match self { + UserPageVisibility::OPEN => "open", + UserPageVisibility::PUBLIC => "public", + UserPageVisibility::PRIVATE => "private" + } + } +} + #[derive(Debug, PartialEq)] #[allow(non_camel_case_types)] pub enum AccountImageVisibility { @@ -110,7 +120,7 @@ pub struct User { pub first_name: String, pub last_name: String, pub last_activity: u64, - pub status: UserPageStatus, + pub user_page_visibility: UserPageVisibility, pub virtual_directory: Option, pub account_image_path: Option, pub account_image_visibility: AccountImageVisibility, @@ -151,12 +161,12 @@ impl User { /// Check if user's page is public pub fn is_page_public(&self) -> bool { - !matches!(self.status, UserPageStatus::PRIVATE) + !matches!(self.user_page_visibility, UserPageVisibility::PRIVATE) } /// Check if user's page is open pub fn is_page_open(&self) -> bool { - matches!(self.status, UserPageStatus::OPEN) + matches!(self.user_page_visibility, UserPageVisibility::OPEN) } /// Get the URL pointing to the default account image diff --git a/src/helpers/account_helper.rs b/src/helpers/account_helper.rs index fdf7b2b..6eada9a 100644 --- a/src/helpers/account_helper.rs +++ b/src/helpers/account_helper.rs @@ -12,7 +12,7 @@ use crate::data::new_account::NewAccount; use crate::data::new_data_conservation_policy::NewDataConservationPolicy; use crate::data::new_notifications_settings::NewNotificationsSettings; use crate::data::security_settings::SecuritySettings; -use crate::data::user::{AccountImageVisibility, User, UserID, UserPageStatus}; +use crate::data::user::{AccountImageVisibility, User, UserID, UserPageVisibility}; use crate::data::user_token::{PushNotificationToken, UserAccessToken}; use crate::helpers::{comments_helper, conversations_helper, custom_emojies_helper, database, events_helper, forez_presence_helper, friends_helper, groups_helper, likes_helper, notifications_helper, posts_helper, push_notifications_helper, survey_helper, user_helper}; use crate::helpers::database::{DeleteQuery, InsertQuery, QueryInfo, RowResult, UpdateInfo}; @@ -225,8 +225,8 @@ pub fn set_general(settings: &GeneralSettings) -> ResultBoxError { .cond_user_id("ID", &settings.id) .set_str("prenom", &settings.first_name) .set_str("nom", &settings.last_name) - .set_legacy_bool("public", settings.page_status != UserPageStatus::PRIVATE) - .set_legacy_bool("pageouverte", settings.page_status == UserPageStatus::OPEN) + .set_legacy_bool("public", settings.page_status != UserPageVisibility::PRIVATE) + .set_legacy_bool("pageouverte", settings.page_status == UserPageVisibility::OPEN) .set_legacy_bool("bloquecommentaire", settings.block_comments) .set_legacy_bool("autoriser_post_amis", settings.allow_posts_from_friends) .set_legacy_bool("autorise_mail", settings.allow_mails) diff --git a/src/helpers/user_helper.rs b/src/helpers/user_helper.rs index 547431a..5db3b8f 100644 --- a/src/helpers/user_helper.rs +++ b/src/helpers/user_helper.rs @@ -1,7 +1,7 @@ use crate::constants::database_tables_names::USERS_TABLE; use crate::data::error::{Res, ResultBoxError}; -use crate::data::user::{AccountImageVisibility, User, UserID, UserPageStatus}; -use crate::data::user::UserPageStatus::PUBLIC; +use crate::data::user::{AccountImageVisibility, User, UserID, UserPageVisibility}; +use crate::data::user::UserPageVisibility::PUBLIC; use crate::helpers::{database, friends_helper}; use crate::helpers::friends_helper::are_friend; @@ -40,11 +40,11 @@ pub fn get_all_users() -> Res> { fn db_to_user(res: &database::RowResult) -> ResultBoxError { // Page status let page_status = if res.get_int64("pageouverte")? == 1 { - UserPageStatus::OPEN + UserPageVisibility::OPEN } else if res.get_int64("public")? == 1 { - UserPageStatus::PUBLIC + UserPageVisibility::PUBLIC } else { - UserPageStatus::PRIVATE + UserPageVisibility::PRIVATE }; // Account image visibility @@ -60,7 +60,7 @@ fn db_to_user(res: &database::RowResult) -> ResultBoxError { password: res.get_str("password")?, first_name: res.get_str("prenom")?, last_name: res.get_str("nom")?, - status: page_status, + user_page_visibility: page_status, last_activity: res.get_u64("last_activity")?, virtual_directory: res.get_optional_str("sous_repertoire")?, account_image_path: res.get_optional_str("account_image_path")?, @@ -109,10 +109,10 @@ pub fn can_see_user_page(user_id: &UserID, target_user: &UserID) -> ResultBoxErr return Ok(true); } - let visibility = find_user_by_id(target_user)?.status; + let visibility = find_user_by_id(target_user)?.user_page_visibility; // Open page = OK - if visibility == UserPageStatus::OPEN { + if visibility == UserPageVisibility::OPEN { return Ok(true); } diff --git a/src/routes.rs b/src/routes.rs index fce454f..0f1e6e1 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -397,5 +397,6 @@ pub fn get_routes() -> Vec { // Admin users management controller Route::admin_post("/admin/users/search", Box::new(admin_users_controller::search)), + Route::admin_post("/admin/users/info", Box::new(admin_users_controller::get_single)) ] } \ No newline at end of file