From d0f87467ece998fe8376bd4022db7500de3372c5 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 12 Jul 2021 19:00:41 +0200 Subject: [PATCH] Log admin requests to get user information --- src/controllers/admin/admin_users_controller.rs | 5 +++++ src/data/admin_action_log.rs | 4 ++++ src/data/user.rs | 17 +++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/src/controllers/admin/admin_users_controller.rs b/src/controllers/admin/admin_users_controller.rs index 3d2b474..621c911 100644 --- a/src/controllers/admin/admin_users_controller.rs +++ b/src/controllers/admin/admin_users_controller.rs @@ -5,8 +5,10 @@ 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::admin_action_log::AdminAction; use crate::data::base_request_handler::BaseRequestHandler; use crate::data::http_request_handler::HttpRequestHandler; +use crate::helpers::admin_log_helper::log_admin_action; use crate::helpers::user_helper; use crate::routes::RequestResult; @@ -29,5 +31,8 @@ pub fn get_single(r: &mut HttpRequestHandler) -> RequestResult { let user_id = r.post_user_id("user_id")?; let user = user_helper::find_user_by_id(&user_id)?; + log_admin_action(r.admin_id()?, &r.remote_ip(), + AdminAction::AccessUserPage { user_id, user_name: user.full_name() })?; + r.set_response(AdminUserInfoAPI::new(user)) } \ No newline at end of file diff --git a/src/data/admin_action_log.rs b/src/data/admin_action_log.rs index 9bde05b..3008460 100644 --- a/src/data/admin_action_log.rs +++ b/src/data/admin_action_log.rs @@ -1,6 +1,7 @@ //! # Admin action log use crate::data::admin::AdminID; +use crate::data::user::UserID; #[derive(serde::Serialize, serde::Deserialize, Clone)] pub enum AdminAction { @@ -13,6 +14,7 @@ pub enum AdminAction { UpdatedAdminGeneralSettings { target: AdminID, new_email: String, new_name: String }, AddAdminRole { target: AdminID, role: String }, RemoveAdminRole { target: AdminID, role: String }, + AccessUserPage { user_id: UserID, user_name: String }, UnsupportedAction { raw_data: String }, } @@ -45,6 +47,8 @@ impl AdminAction { AdminAction::RemoveAdminRole { .. } => { "Removed a role from [admin]{target}[/admin] => {role}" } + AdminAction::AccessUserPage { .. } => { "Administrator accessed information of user #{user_id} '{user_name}'." } + AdminAction::UnsupportedAction { .. } => { "Unsupported action. Raw data: {raw_data}" } } } diff --git a/src/data/user.rs b/src/data/user.rs index c65530d..361596c 100644 --- a/src/data/user.rs +++ b/src/data/user.rs @@ -1,5 +1,8 @@ use std::hash::{Hash, Hasher}; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; + +use crate::data::u64_visitor::U64Visitor; use crate::utils::user_data_utils::user_data_url; ///! User information @@ -61,6 +64,20 @@ impl PartialEq for &UserID { } } +impl Serialize for UserID { + fn serialize(&self, serializer: S) -> Result<::Ok, ::Error> where + S: Serializer { + serializer.serialize_u64(self.0) + } +} + +impl<'de> Deserialize<'de> for UserID { + fn deserialize(deserializer: D) -> Result>::Error> where + D: Deserializer<'de> { + deserializer.deserialize_u64(U64Visitor {}).map(|id| UserID::new(id)) + } +} + #[derive(Debug, PartialEq)] pub enum UserPageVisibility { OPEN,