From 82717312e671e2098a452e9716fddb2a83b308a8 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 12 Jul 2021 17:18:06 +0200 Subject: [PATCH] Administrators can search for users --- .../admin/admin_search_user_result.rs | 26 ++++++++++++++ src/api_data/admin/mod.rs | 3 +- .../admin/admin_users_controller.rs | 22 ++++++++++++ src/controllers/admin/mod.rs | 3 +- src/data/user.rs | 10 ++++++ src/helpers/user_helper.rs | 34 +++++++++++++++++-- src/routes.rs | 3 ++ 7 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 src/api_data/admin/admin_search_user_result.rs create mode 100644 src/controllers/admin/admin_users_controller.rs diff --git a/src/api_data/admin/admin_search_user_result.rs b/src/api_data/admin/admin_search_user_result.rs new file mode 100644 index 0000000..3c749b7 --- /dev/null +++ b/src/api_data/admin/admin_search_user_result.rs @@ -0,0 +1,26 @@ +//! # Search user result API entry +//! +//! @author Pierre Hubert + +use crate::data::user::User; + +#[derive(serde::Serialize)] +pub struct AdminSearchUserResult { + id: u64, + first_name: String, + last_name: String, + email: String, + account_image: String +} + +impl AdminSearchUserResult { + pub fn new(user: User) -> Self { + Self { + id: user.id.id(), + account_image: user.account_image_url_for_admin(), + first_name: user.first_name, + last_name: user.last_name, + email: user.email, + } + } +} \ No newline at end of file diff --git a/src/api_data/admin/mod.rs b/src/api_data/admin/mod.rs index d7a0e88..d9d1bae 100644 --- a/src/api_data/admin/mod.rs +++ b/src/api_data/admin/mod.rs @@ -10,4 +10,5 @@ pub mod admin_keys_api; pub mod admin_res_create_reset_token; pub mod admin_role_api; pub mod admin_res_create_account; -pub mod admin_log_api; \ No newline at end of file +pub mod admin_log_api; +pub mod admin_search_user_result; \ No newline at end of file diff --git a/src/controllers/admin/admin_users_controller.rs b/src/controllers/admin/admin_users_controller.rs new file mode 100644 index 0000000..f4f5272 --- /dev/null +++ b/src/controllers/admin/admin_users_controller.rs @@ -0,0 +1,22 @@ +//! # Admin : users management controller +//! +//! @author Pierre Hubert + +use crate::data::http_request_handler::HttpRequestHandler; +use crate::routes::RequestResult; +use crate::data::base_request_handler::BaseRequestHandler; +use crate::constants::admin::AdminRole; +use crate::helpers::user_helper; +use crate::api_data::admin::admin_search_user_result::AdminSearchUserResult; + +/// Search for user +pub fn search(r: &mut HttpRequestHandler) -> RequestResult { + r.check_admin_has_role(AdminRole::MANAGE_USERS)?; + + let name = r.post_string_opt("name", 0, true)?; + let email = r.post_string_opt("email", 0, true)?; + + let results = user_helper::search_user_admin(&name, &email, 50)?; + + r.set_response(results.into_iter().map(AdminSearchUserResult::new).collect::>()) +} \ No newline at end of file diff --git a/src/controllers/admin/mod.rs b/src/controllers/admin/mod.rs index c755ee6..d76edc9 100644 --- a/src/controllers/admin/mod.rs +++ b/src/controllers/admin/mod.rs @@ -5,4 +5,5 @@ pub mod admin_account_controller; pub mod admin_keys_controller; pub mod admin_roles_controller; -pub mod admin_logs_controller; \ No newline at end of file +pub mod admin_logs_controller; +pub mod admin_users_controller; \ No newline at end of file diff --git a/src/data/user.rs b/src/data/user.rs index 8f347a5..2371773 100644 --- a/src/data/user.rs +++ b/src/data/user.rs @@ -170,6 +170,16 @@ impl User { user_data_url(crate::constants::ERROR_ACCOUNT_IMAGE) } + /// Get the URL pointing to a user account image for an administrator. An administrator can + /// only access to the user account image if it is visible to all Comunic users + pub fn account_image_url_for_admin(&self) -> String { + match (&self.account_image_visibility, &self.account_image_path) { + (AccountImageVisibility::FRIENDS, _) => User::error_account_image_url(), + (_, None) => User::default_account_image_url(), + (_, Some(path)) => user_data_url(path), + } + } + /// Check if this user has an account image or not pub fn has_account_image(&self) -> bool { self.account_image_path.is_some() diff --git a/src/helpers/user_helper.rs b/src/helpers/user_helper.rs index 7727351..547431a 100644 --- a/src/helpers/user_helper.rs +++ b/src/helpers/user_helper.rs @@ -38,8 +38,6 @@ pub fn get_all_users() -> Res> { /// Execute query & return result fn db_to_user(res: &database::RowResult) -> ResultBoxError { - - // Page status let page_status = if res.get_int64("pageouverte")? == 1 { UserPageStatus::OPEN @@ -196,4 +194,36 @@ pub fn search_user(query: &str, limit: u64) -> ResultBoxError> { .add_custom_where_argument_str(&query) .set_limit(limit) .exec(|row| row.get_user_id("ID")) +} + +/// Search for user in the database / admin version +pub fn search_user_admin(name: &str, email: &str, limit: u64) -> ResultBoxError> { + let name_query = format!("%{}%", name.replace(" ", "%")); + + let mut custom_where = String::new(); + let mut query = database::QueryInfo::new(USERS_TABLE); + + if !name.is_empty() { + custom_where.push_str("(nom LIKE ?) || (prenom LIKE ?) || (CONCAT(prenom, '%', nom) LIKE ?) || (CONCAT(nom, '%', prenom) LIKE ?)"); + + query = query + .add_custom_where_argument_str(&name_query) + .add_custom_where_argument_str(&name_query) + .add_custom_where_argument_str(&name_query) + .add_custom_where_argument_str(&name_query); + } + + if !email.is_empty() { + if !custom_where.is_empty() { + custom_where.push_str(" OR "); + } + custom_where.push_str("(mail LIKE ?)"); + query = query.add_custom_where_argument_str(&format!("%{}%", email)) + } + + + query + .set_custom_where(&custom_where) + .set_limit(limit) + .exec(db_to_user) } \ No newline at end of file diff --git a/src/routes.rs b/src/routes.rs index c507c04..fce454f 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -394,5 +394,8 @@ pub fn get_routes() -> Vec { // Admin logs controller Route::admin_post("/admin/logs/list", Box::new(admin_logs_controller::get_list)), + + // Admin users management controller + Route::admin_post("/admin/users/search", Box::new(admin_users_controller::search)), ] } \ No newline at end of file