1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-29 08:46:27 +00:00

Administrators can search for users

This commit is contained in:
Pierre HUBERT 2021-07-12 17:18:06 +02:00
parent cb44497fee
commit 82717312e6
7 changed files with 97 additions and 4 deletions

View File

@ -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,
}
}
}

View File

@ -11,3 +11,4 @@ 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;

View File

@ -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::<Vec<_>>())
}

View File

@ -6,3 +6,4 @@ pub mod admin_account_controller;
pub mod admin_keys_controller; pub mod admin_keys_controller;
pub mod admin_roles_controller; pub mod admin_roles_controller;
pub mod admin_logs_controller; pub mod admin_logs_controller;
pub mod admin_users_controller;

View File

@ -170,6 +170,16 @@ impl User {
user_data_url(crate::constants::ERROR_ACCOUNT_IMAGE) 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 /// Check if this user has an account image or not
pub fn has_account_image(&self) -> bool { pub fn has_account_image(&self) -> bool {
self.account_image_path.is_some() self.account_image_path.is_some()

View File

@ -38,8 +38,6 @@ pub fn get_all_users() -> Res<Vec<User>> {
/// Execute query & return result /// Execute query & return result
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 UserPageStatus::OPEN
@ -197,3 +195,35 @@ pub fn search_user(query: &str, limit: u64) -> ResultBoxError<Vec<UserID>> {
.set_limit(limit) .set_limit(limit)
.exec(|row| row.get_user_id("ID")) .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<Vec<User>> {
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)
}

View File

@ -394,5 +394,8 @@ pub fn get_routes() -> Vec<Route> {
// Admin logs controller // Admin logs controller
Route::admin_post("/admin/logs/list", Box::new(admin_logs_controller::get_list)), 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)),
] ]
} }