diff --git a/src/service/admin/user/mod.rs b/src/service/admin/user/mod.rs index d14f7cfc..771b866b 100644 --- a/src/service/admin/user/mod.rs +++ b/src/service/admin/user/mod.rs @@ -1,7 +1,7 @@ pub(crate) mod user_commands; use clap::Subcommand; -use ruma::{events::room::message::RoomMessageEventContent, UserId}; +use ruma::events::room::message::RoomMessageEventContent; use self::user_commands::{create, deactivate, deactivate_all, list, list_joined_rooms, reset_password}; use crate::Result; @@ -20,7 +20,7 @@ pub(crate) enum UserCommand { /// - Reset user password ResetPassword { /// Username of the user for whom the password should be reset - username: Box, + username: String, }, /// - Deactivate a user @@ -30,7 +30,7 @@ pub(crate) enum UserCommand { Deactivate { #[arg(short, long)] leave_rooms: bool, - user_id: Box, + user_id: String, }, /// - Deactivate a list of users @@ -60,7 +60,7 @@ pub(crate) enum UserCommand { /// - Lists all the rooms (local and remote) that the specified user is /// joined in ListJoinedRooms { - user_id: Box, + user_id: String, }, } diff --git a/src/service/admin/user/user_commands.rs b/src/service/admin/user/user_commands.rs index 47a43233..52cc6792 100644 --- a/src/service/admin/user/user_commands.rs +++ b/src/service/admin/user/user_commands.rs @@ -25,6 +25,7 @@ pub(super) async fn create( _body: Vec<&str>, username: String, password: Option, ) -> Result { let password = password.unwrap_or_else(|| utils::random_string(AUTO_GEN_PASSWORD_LENGTH)); + // Validate user id let user_id = match UserId::parse_with_server_name(username.as_str().to_lowercase(), services().globals.server_name()) { @@ -35,11 +36,13 @@ pub(super) async fn create( ))) }, }; + if user_id.is_historical() { return Ok(RoomMessageEventContent::text_plain(format!( "Userid {user_id} is not allowed due to historical" ))); } + if services().users.exists(&user_id)? { return Ok(RoomMessageEventContent::text_plain(format!("Userid {user_id} already exists"))); } @@ -117,9 +120,18 @@ pub(super) async fn create( } pub(super) async fn deactivate( - _body: Vec<&str>, leave_rooms: bool, user_id: Box, + _body: Vec<&str>, leave_rooms: bool, user_id: String, ) -> Result { - let user_id = Arc::::from(user_id); + // Validate user id + let user_id = + match UserId::parse_with_server_name(user_id.as_str().to_lowercase(), services().globals.server_name()) { + Ok(id) => Arc::::from(id), + Err(e) => { + return Ok(RoomMessageEventContent::text_plain(format!( + "The supplied username is not a valid username: {e}" + ))) + }, + }; // check if user belongs to our server if user_id.server_name() != services().globals.server_name() { @@ -156,10 +168,11 @@ pub(super) async fn deactivate( } } -pub(super) async fn reset_password(_body: Vec<&str>, username: Box) -> Result { +pub(super) async fn reset_password(_body: Vec<&str>, username: String) -> Result { + // Validate user id let user_id = match UserId::parse_with_server_name(username.as_str().to_lowercase(), services().globals.server_name()) { - Ok(id) => id, + Ok(id) => Arc::::from(id), Err(e) => { return Ok(RoomMessageEventContent::text_plain(format!( "The supplied username is not a valid username: {e}" @@ -279,7 +292,18 @@ pub(super) async fn deactivate_all(body: Vec<&str>, leave_rooms: bool, force: bo } } -pub(super) async fn list_joined_rooms(_body: Vec<&str>, user_id: Box) -> Result { +pub(super) async fn list_joined_rooms(_body: Vec<&str>, user_id: String) -> Result { + // Validate user id + let user_id = + match UserId::parse_with_server_name(user_id.as_str().to_lowercase(), services().globals.server_name()) { + Ok(id) => Arc::::from(id), + Err(e) => { + return Ok(RoomMessageEventContent::text_plain(format!( + "The supplied username is not a valid username: {e}" + ))) + }, + }; + if user_id.server_name() != services().globals.server_name() { return Ok(RoomMessageEventContent::text_plain("User does not belong to our server.")); }