1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 21:39:21 +00:00

Administrators can get information about a single user

This commit is contained in:
Pierre HUBERT 2021-07-12 17:50:14 +02:00
parent 82717312e6
commit 19887f525e
11 changed files with 106 additions and 29 deletions

View File

@ -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<String>,
account_image: String,
friend_list_public: bool,
is_email_public: bool,
personal_website: Option<String>,
public_note: Option<String>,
location: Option<String>,
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,
}
}
}

View File

@ -11,4 +11,5 @@ pub mod admin_res_create_reset_token;
pub mod admin_role_api; pub mod admin_role_api;
pub mod admin_res_create_account; pub mod admin_res_create_account;
pub mod admin_log_api; pub mod admin_log_api;
pub mod admin_search_user_result; pub mod admin_search_user_result_api;
pub mod admin_user_info_api;

View File

@ -5,7 +5,7 @@ use serde::Serialize;
use crate::api_data::custom_emoji::CustomEmojiAPI; use crate::api_data::custom_emoji::CustomEmojiAPI;
use crate::data::error::ResultBoxError; 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::data::user::AccountImageVisibility::{COMUNIC_USERS, EVERYONE};
use crate::helpers::{background_image_helper, custom_emojies_helper, friends_helper, likes_helper, user_helper}; use crate::helpers::{background_image_helper, custom_emojies_helper, friends_helper, likes_helper, user_helper};
use crate::helpers::likes_helper::LikeType; use crate::helpers::likes_helper::LikeType;
@ -53,8 +53,8 @@ impl APIUserInfo {
userID: info.id.id(), userID: info.id.id(),
firstName: info.first_name.to_string(), firstName: info.first_name.to_string(),
lastName: info.last_name.to_string(), lastName: info.last_name.to_string(),
publicPage: info.status != UserPageStatus::PRIVATE, publicPage: info.user_page_visibility != UserPageVisibility::PRIVATE,
openPage: info.status == UserPageStatus::OPEN, openPage: info.user_page_visibility == UserPageVisibility::OPEN,
virtualDirectory: info.virtual_directory.clone().unwrap_or(String::new()), virtualDirectory: info.virtual_directory.clone().unwrap_or(String::new()),
accountImage: APIUserInfo::get_account_image_url(user_id, info)?, accountImage: APIUserInfo::get_account_image_url(user_id, info)?,
customEmojis: custom_emojies_helper::get_list_user(&info.id)? customEmojis: custom_emojies_helper::get_list_user(&info.id)?

View File

@ -2,12 +2,13 @@
//! //!
//! @author Pierre Hubert //! @author Pierre Hubert
use crate::data::http_request_handler::HttpRequestHandler; use crate::api_data::admin::admin_search_user_result_api::AdminSearchUserResult;
use crate::routes::RequestResult; use crate::api_data::admin::admin_user_info_api::AdminUserInfoAPI;
use crate::data::base_request_handler::BaseRequestHandler;
use crate::constants::admin::AdminRole; 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::helpers::user_helper;
use crate::api_data::admin::admin_search_user_result::AdminSearchUserResult; use crate::routes::RequestResult;
/// Search for user /// Search for user
pub fn search(r: &mut HttpRequestHandler) -> RequestResult { pub fn search(r: &mut HttpRequestHandler) -> RequestResult {
@ -20,3 +21,13 @@ pub fn search(r: &mut HttpRequestHandler) -> RequestResult {
r.set_response(results.into_iter().map(AdminSearchUserResult::new).collect::<Vec<_>>()) r.set_response(results.into_iter().map(AdminSearchUserResult::new).collect::<Vec<_>>())
} }
/// 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))
}

View File

@ -19,7 +19,7 @@ use crate::data::new_custom_emoji::NewCustomEmoji;
use crate::data::new_data_conservation_policy::NewDataConservationPolicy; use crate::data::new_data_conservation_policy::NewDataConservationPolicy;
use crate::data::new_notifications_settings::NewNotificationsSettings; use crate::data::new_notifications_settings::NewNotificationsSettings;
use crate::data::security_settings::{SecurityQuestion, SecuritySettings}; 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::{account_helper, custom_emojies_helper, user_helper};
use crate::helpers::virtual_directory_helper::VirtualDirType; use crate::helpers::virtual_directory_helper::VirtualDirType;
use crate::routes::RequestResult; use crate::routes::RequestResult;
@ -35,9 +35,9 @@ pub fn get_general(r: &mut HttpRequestHandler) -> RequestResult {
/// Set the general settings of the user /// Set the general settings of the user
pub fn set_general(r: &mut HttpRequestHandler) -> RequestResult { pub fn set_general(r: &mut HttpRequestHandler) -> RequestResult {
let page_status = match (r.post_bool("isPublic")?, r.post_bool("isOpen")?) { let page_status = match (r.post_bool("isPublic")?, r.post_bool("isOpen")?) {
(true, true) => UserPageStatus::OPEN, (true, true) => UserPageVisibility::OPEN,
(true, false) => UserPageStatus::PUBLIC, (true, false) => UserPageVisibility::PUBLIC,
(_, _) => UserPageStatus::PRIVATE, (_, _) => UserPageVisibility::PRIVATE,
}; };
let personal_website = r.post_url_opt("personnalWebsite", false)?; let personal_website = r.post_url_opt("personnalWebsite", false)?;

View File

@ -2,13 +2,13 @@
//! //!
//! @author Pierre Hubert //! @author Pierre Hubert
use crate::data::user::{UserID, UserPageStatus}; use crate::data::user::{UserID, UserPageVisibility};
pub struct GeneralSettings { pub struct GeneralSettings {
pub id: UserID, pub id: UserID,
pub first_name: String, pub first_name: String,
pub last_name: String, pub last_name: String,
pub page_status: UserPageStatus, pub page_status: UserPageVisibility,
pub block_comments: bool, pub block_comments: bool,
pub allow_posts_from_friends: bool, pub allow_posts_from_friends: bool,
pub friends_list_public: bool, pub friends_list_public: bool,

View File

@ -62,12 +62,22 @@ impl PartialEq<UserID> for &UserID {
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum UserPageStatus { pub enum UserPageVisibility {
OPEN, OPEN,
PUBLIC, PUBLIC,
PRIVATE, PRIVATE,
} }
impl UserPageVisibility {
pub fn to_api(&self) -> &'static str {
match self {
UserPageVisibility::OPEN => "open",
UserPageVisibility::PUBLIC => "public",
UserPageVisibility::PRIVATE => "private"
}
}
}
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
pub enum AccountImageVisibility { pub enum AccountImageVisibility {
@ -110,7 +120,7 @@ pub struct User {
pub first_name: String, pub first_name: String,
pub last_name: String, pub last_name: String,
pub last_activity: u64, pub last_activity: u64,
pub status: UserPageStatus, pub user_page_visibility: UserPageVisibility,
pub virtual_directory: Option<String>, pub virtual_directory: Option<String>,
pub account_image_path: Option<String>, pub account_image_path: Option<String>,
pub account_image_visibility: AccountImageVisibility, pub account_image_visibility: AccountImageVisibility,
@ -151,12 +161,12 @@ impl User {
/// Check if user's page is public /// Check if user's page is public
pub fn is_page_public(&self) -> bool { 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 /// Check if user's page is open
pub fn is_page_open(&self) -> bool { 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 /// Get the URL pointing to the default account image

View File

@ -12,7 +12,7 @@ use crate::data::new_account::NewAccount;
use crate::data::new_data_conservation_policy::NewDataConservationPolicy; use crate::data::new_data_conservation_policy::NewDataConservationPolicy;
use crate::data::new_notifications_settings::NewNotificationsSettings; use crate::data::new_notifications_settings::NewNotificationsSettings;
use crate::data::security_settings::SecuritySettings; 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::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::{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}; 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) .cond_user_id("ID", &settings.id)
.set_str("prenom", &settings.first_name) .set_str("prenom", &settings.first_name)
.set_str("nom", &settings.last_name) .set_str("nom", &settings.last_name)
.set_legacy_bool("public", settings.page_status != UserPageStatus::PRIVATE) .set_legacy_bool("public", settings.page_status != UserPageVisibility::PRIVATE)
.set_legacy_bool("pageouverte", settings.page_status == UserPageStatus::OPEN) .set_legacy_bool("pageouverte", settings.page_status == UserPageVisibility::OPEN)
.set_legacy_bool("bloquecommentaire", settings.block_comments) .set_legacy_bool("bloquecommentaire", settings.block_comments)
.set_legacy_bool("autoriser_post_amis", settings.allow_posts_from_friends) .set_legacy_bool("autoriser_post_amis", settings.allow_posts_from_friends)
.set_legacy_bool("autorise_mail", settings.allow_mails) .set_legacy_bool("autorise_mail", settings.allow_mails)

View File

@ -1,7 +1,7 @@
use crate::constants::database_tables_names::USERS_TABLE; use crate::constants::database_tables_names::USERS_TABLE;
use crate::data::error::{Res, ResultBoxError}; use crate::data::error::{Res, ResultBoxError};
use crate::data::user::{AccountImageVisibility, User, UserID, UserPageStatus}; use crate::data::user::{AccountImageVisibility, User, UserID, UserPageVisibility};
use crate::data::user::UserPageStatus::PUBLIC; use crate::data::user::UserPageVisibility::PUBLIC;
use crate::helpers::{database, friends_helper}; use crate::helpers::{database, friends_helper};
use crate::helpers::friends_helper::are_friend; use crate::helpers::friends_helper::are_friend;
@ -40,11 +40,11 @@ pub fn get_all_users() -> Res<Vec<User>> {
fn db_to_user(res: &database::RowResult) -> ResultBoxError<User> { fn db_to_user(res: &database::RowResult) -> ResultBoxError<User> {
// Page status // Page status
let page_status = if res.get_int64("pageouverte")? == 1 { let page_status = if res.get_int64("pageouverte")? == 1 {
UserPageStatus::OPEN UserPageVisibility::OPEN
} else if res.get_int64("public")? == 1 { } else if res.get_int64("public")? == 1 {
UserPageStatus::PUBLIC UserPageVisibility::PUBLIC
} else { } else {
UserPageStatus::PRIVATE UserPageVisibility::PRIVATE
}; };
// Account image visibility // Account image visibility
@ -60,7 +60,7 @@ fn db_to_user(res: &database::RowResult) -> ResultBoxError<User> {
password: res.get_str("password")?, password: res.get_str("password")?,
first_name: res.get_str("prenom")?, first_name: res.get_str("prenom")?,
last_name: res.get_str("nom")?, last_name: res.get_str("nom")?,
status: page_status, user_page_visibility: page_status,
last_activity: res.get_u64("last_activity")?, last_activity: res.get_u64("last_activity")?,
virtual_directory: res.get_optional_str("sous_repertoire")?, virtual_directory: res.get_optional_str("sous_repertoire")?,
account_image_path: res.get_optional_str("account_image_path")?, 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); 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 // Open page = OK
if visibility == UserPageStatus::OPEN { if visibility == UserPageVisibility::OPEN {
return Ok(true); return Ok(true);
} }

View File

@ -397,5 +397,6 @@ pub fn get_routes() -> Vec<Route> {
// Admin users management controller // Admin users management controller
Route::admin_post("/admin/users/search", Box::new(admin_users_controller::search)), 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))
] ]
} }