Update count users: It's now list_local_users and contains the number and the usernames

This commit is contained in:
Torsten Flammiger 2021-12-26 20:00:31 +01:00
parent 39787b41cb
commit a69eb277d4
3 changed files with 28 additions and 12 deletions

View File

@ -13,7 +13,7 @@ use tracing::warn;
pub enum AdminCommand { pub enum AdminCommand {
RegisterAppservice(serde_yaml::Value), RegisterAppservice(serde_yaml::Value),
ListAppservices, ListAppservices,
CountLocalUsers, ListLocalUsers,
SendMessage(RoomMessageEventContent), SendMessage(RoomMessageEventContent),
} }
@ -94,15 +94,17 @@ impl Admin {
let state_lock = mutex_state.lock().await; let state_lock = mutex_state.lock().await;
match event { match event {
AdminCommand::CountLocalUsers => { AdminCommand::ListLocalUsers => {
// count_local_users() only returns with OK(x) where x is the number of found accounts // collect all local users
if let Ok(usercount) = guard.users.count_local_users() { let users = guard.users.iter_locals();
let message = format!("Found {} local user account(s)", usercount);
send_message(RoomMessageEventContent::text_plain(message), guard, &state_lock); let mut msg: String = format!("Found {} local user account(s):\n", users.len());
} else { msg += &users.join("\n");
// if count_local_users() only returns with OK(x), then why is this? ;-)
send_message(RoomMessageEventContent::text_plain("Unable to count local users"), guard, &state_lock); // send number of local users as plain text:
} // TODO: send as Markdown
send_message(RoomMessageEventContent::text_plain(&msg), guard, &state_lock);
} }
AdminCommand::RegisterAppservice(yaml) => { AdminCommand::RegisterAppservice(yaml) => {
guard.appservice.register_appservice(yaml).unwrap(); // TODO handle error guard.appservice.register_appservice(yaml).unwrap(); // TODO handle error

View File

@ -1531,8 +1531,8 @@ impl Rooms {
"list_appservices" => { "list_appservices" => {
db.admin.send(AdminCommand::ListAppservices); db.admin.send(AdminCommand::ListAppservices);
} }
"count_local_users" => { "list_local_users" => {
db.admin.send(AdminCommand::CountLocalUsers); db.admin.send(AdminCommand::ListLocalUsers);
} }
"get_auth_chain" => { "get_auth_chain" => {
if args.len() == 1 { if args.len() == 1 {

View File

@ -84,6 +84,8 @@ impl Users {
Ok(self.userid_password.iter().count()) Ok(self.userid_password.iter().count())
} }
/// The method is DEPRECATED and was replaced by iter_locals()
///
/// This method will only count those local user accounts with /// This method will only count those local user accounts with
/// a password thus returning only real accounts on this instance. /// a password thus returning only real accounts on this instance.
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
@ -92,6 +94,7 @@ impl Users {
Ok(n) Ok(n)
} }
/// Find out which user an access token belongs to. /// Find out which user an access token belongs to.
#[tracing::instrument(skip(self, token))] #[tracing::instrument(skip(self, token))]
pub fn find_from_token(&self, token: &str) -> Result<Option<(Box<UserId>, String)>> { pub fn find_from_token(&self, token: &str) -> Result<Option<(Box<UserId>, String)>> {
@ -131,6 +134,17 @@ impl Users {
}) })
} }
/// Returns a vector of local usernames
#[tracing::instrument(skip(self))]
pub fn iter_locals(&self) -> Vec<String> {
self.userid_password.iter().filter(|(_, pw)| pw.len() > 0).map(|(username, _)| {
match utils::string_from_bytes(&username) {
Ok(s) => s,
Err(e) => e.to_string()
}
}).collect::<Vec<String>>()
}
/// Returns the password hash for the given user. /// Returns the password hash for the given user.
#[tracing::instrument(skip(self, user_id))] #[tracing::instrument(skip(self, user_id))]
pub fn password_hash(&self, user_id: &UserId) -> Result<Option<String>> { pub fn password_hash(&self, user_id: &UserId) -> Result<Option<String>> {