allow user admin commands to take the username only

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-04-21 18:38:43 -04:00 committed by June
parent 8b3c4a528c
commit affd063df6
2 changed files with 33 additions and 9 deletions

View File

@ -1,7 +1,7 @@
pub(crate) mod user_commands; pub(crate) mod user_commands;
use clap::Subcommand; 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 self::user_commands::{create, deactivate, deactivate_all, list, list_joined_rooms, reset_password};
use crate::Result; use crate::Result;
@ -20,7 +20,7 @@ pub(crate) enum UserCommand {
/// - Reset user password /// - Reset user password
ResetPassword { ResetPassword {
/// Username of the user for whom the password should be reset /// Username of the user for whom the password should be reset
username: Box<UserId>, username: String,
}, },
/// - Deactivate a user /// - Deactivate a user
@ -30,7 +30,7 @@ pub(crate) enum UserCommand {
Deactivate { Deactivate {
#[arg(short, long)] #[arg(short, long)]
leave_rooms: bool, leave_rooms: bool,
user_id: Box<UserId>, user_id: String,
}, },
/// - Deactivate a list of users /// - 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 /// - Lists all the rooms (local and remote) that the specified user is
/// joined in /// joined in
ListJoinedRooms { ListJoinedRooms {
user_id: Box<UserId>, user_id: String,
}, },
} }

View File

@ -25,6 +25,7 @@ pub(super) async fn create(
_body: Vec<&str>, username: String, password: Option<String>, _body: Vec<&str>, username: String, password: Option<String>,
) -> Result<RoomMessageEventContent> { ) -> Result<RoomMessageEventContent> {
let password = password.unwrap_or_else(|| utils::random_string(AUTO_GEN_PASSWORD_LENGTH)); let password = password.unwrap_or_else(|| utils::random_string(AUTO_GEN_PASSWORD_LENGTH));
// Validate user id // Validate user id
let user_id = let user_id =
match UserId::parse_with_server_name(username.as_str().to_lowercase(), services().globals.server_name()) { 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() { if user_id.is_historical() {
return Ok(RoomMessageEventContent::text_plain(format!( return Ok(RoomMessageEventContent::text_plain(format!(
"Userid {user_id} is not allowed due to historical" "Userid {user_id} is not allowed due to historical"
))); )));
} }
if services().users.exists(&user_id)? { if services().users.exists(&user_id)? {
return Ok(RoomMessageEventContent::text_plain(format!("Userid {user_id} already exists"))); 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( pub(super) async fn deactivate(
_body: Vec<&str>, leave_rooms: bool, user_id: Box<UserId>, _body: Vec<&str>, leave_rooms: bool, user_id: String,
) -> Result<RoomMessageEventContent> { ) -> Result<RoomMessageEventContent> {
let user_id = Arc::<UserId>::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::<UserId>::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 // check if user belongs to our server
if user_id.server_name() != services().globals.server_name() { 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<UserId>) -> Result<RoomMessageEventContent> { pub(super) async fn reset_password(_body: Vec<&str>, username: String) -> Result<RoomMessageEventContent> {
// Validate user id
let user_id = let user_id =
match UserId::parse_with_server_name(username.as_str().to_lowercase(), services().globals.server_name()) { match UserId::parse_with_server_name(username.as_str().to_lowercase(), services().globals.server_name()) {
Ok(id) => id, Ok(id) => Arc::<UserId>::from(id),
Err(e) => { Err(e) => {
return Ok(RoomMessageEventContent::text_plain(format!( return Ok(RoomMessageEventContent::text_plain(format!(
"The supplied username is not a valid username: {e}" "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<UserId>) -> Result<RoomMessageEventContent> { pub(super) async fn list_joined_rooms(_body: Vec<&str>, user_id: String) -> Result<RoomMessageEventContent> {
// 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::<UserId>::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() { if user_id.server_name() != services().globals.server_name() {
return Ok(RoomMessageEventContent::text_plain("User does not belong to our server.")); return Ok(RoomMessageEventContent::text_plain("User does not belong to our server."));
} }