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

View file

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

View file

@ -84,6 +84,8 @@ impl Users {
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
/// a password thus returning only real accounts on this instance.
#[tracing::instrument(skip(self))]
@ -92,6 +94,7 @@ impl Users {
Ok(n)
}
/// Find out which user an access token belongs to.
#[tracing::instrument(skip(self, token))]
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.
#[tracing::instrument(skip(self, user_id))]
pub fn password_hash(&self, user_id: &UserId) -> Result<Option<String>> {