mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2024-11-25 23:09:22 +00:00
Administrators can search for users
This commit is contained in:
parent
cb44497fee
commit
82717312e6
26
src/api_data/admin/admin_search_user_result.rs
Normal file
26
src/api_data/admin/admin_search_user_result.rs
Normal 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,
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
pub mod admin_log_api;
|
||||
pub mod admin_search_user_result;
|
22
src/controllers/admin/admin_users_controller.rs
Normal file
22
src/controllers/admin/admin_users_controller.rs
Normal 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<_>>())
|
||||
}
|
@ -5,4 +5,5 @@
|
||||
pub mod admin_account_controller;
|
||||
pub mod admin_keys_controller;
|
||||
pub mod admin_roles_controller;
|
||||
pub mod admin_logs_controller;
|
||||
pub mod admin_logs_controller;
|
||||
pub mod admin_users_controller;
|
@ -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()
|
||||
|
@ -38,8 +38,6 @@ pub fn get_all_users() -> Res<Vec<User>> {
|
||||
|
||||
/// Execute query & return result
|
||||
fn db_to_user(res: &database::RowResult) -> ResultBoxError<User> {
|
||||
|
||||
|
||||
// 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<Vec<UserID>> {
|
||||
.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<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)
|
||||
}
|
@ -394,5 +394,8 @@ pub fn get_routes() -> Vec<Route> {
|
||||
|
||||
// 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)),
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue
Block a user