Converted banlist to use Uuids as the key instead of the username

This commit is contained in:
tylerlowrey 2020-08-11 12:44:30 -04:00 committed by Joshua Yanovski
parent 2c55afcfa9
commit 9e7ec63800
3 changed files with 57 additions and 37 deletions

View File

@ -26,6 +26,7 @@ use world::util::Sampler;
use scan_fmt::{scan_fmt, scan_fmt_some};
use tracing::error;
use crate::login_provider::LoginProvider;
pub trait ChatCommandExt {
fn execute(&self, server: &mut Server, entity: EcsEntity, args: String);
@ -1870,8 +1871,10 @@ fn handle_ban(
scan_fmt_some!(&args, &action.arg_fmt(), String, String)
{
let reason = reason_opt.unwrap_or_default();
let uuid_result = server.state.ecs().read_resource::<LoginProvider>().username_to_uuid(&target_alias);
if server.settings().banlist.contains_key(&target_alias) {
if let Ok(uuid) = uuid_result {
if server.settings().banlist.contains_key(&uuid) {
server.notify_client(
client,
ChatType::CommandError
@ -1879,7 +1882,7 @@ fn handle_ban(
)
} else {
server.settings_mut().edit(|s| {
s.banlist.insert(target_alias.clone(), reason.clone());
s.banlist.insert(uuid, (target_alias.clone(), reason.clone()));
});
server.notify_client(
client,
@ -1899,6 +1902,12 @@ fn handle_ban(
kick_player(server, target_player, &reason);
}
}
} else {
server.notify_client(
client,
ChatType::CommandError.server_msg(format!("Unable to determine UUID for username \"{}\"", target_alias))
)
}
} else {
server.notify_client(
client,
@ -1915,13 +1924,23 @@ fn handle_unban(
action: &ChatCommand,
) {
if let Ok(username) = scan_fmt!(&args, &action.arg_fmt(), String) {
let uuid_result = server.state.ecs().read_resource::<LoginProvider>().username_to_uuid(&username);
if let Ok(uuid) = uuid_result {
server.settings_mut().edit(|s| {
s.banlist.remove(&username);
s.banlist.remove(&uuid);
});
server.notify_client(
client,
ChatType::CommandInfo.server_msg(format!("{} was successfully unbanned", username)),
);
} else {
server.notify_client(
client,
ChatType::CommandError.server_msg(format!("Unable to determine UUID for username \"{}\"", username))
)
}
} else {
server.notify_client(
client,

View File

@ -53,7 +53,7 @@ impl LoginProvider {
&mut self,
username_or_token: &str,
whitelist: &[String],
banlist: &HashMap<String, String>,
banlist: &HashMap<Uuid, (String,String)>,
) -> Result<(String, Uuid), RegisterError> {
self
// resolve user information
@ -61,9 +61,9 @@ impl LoginProvider {
// if found, check name against whitelist or if user is admin
.and_then(|(username, uuid)| {
// user cannot join if they are listed on the banlist
if let Some(ban_record) = banlist.get(&username) {
if let Some(ban_record) = banlist.get(&uuid) {
// Pull reason string out of ban record and send a copy of it
return Err(RegisterError::Banned(ban_record.clone()));
return Err(RegisterError::Banned(ban_record.1.clone()));
}
// user can only join if he is admin, the whitelist is empty (everyone can join)
@ -105,6 +105,6 @@ impl LoginProvider {
}
pub fn username_to_uuid(&self, username: &str) -> Result<Uuid, AuthClientError> {
self.auth_server.map_or_else(|| Ok(derive_uuid(username)), |username| auth.username_to_uuid(username))
self.auth_server.as_ref().map_or_else(|| Ok(derive_uuid(username)), |auth| auth.username_to_uuid(&username))
}
}

View File

@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
use std::{fs, io::prelude::*, net::SocketAddr, path::PathBuf, time::Duration};
use tracing::{error, warn};
use world::sim::FileOpts;
use authc::Uuid;
const DEFAULT_WORLD_SEED: u32 = 59686;
@ -21,7 +22,7 @@ pub struct ServerSettings {
pub start_time: f64,
pub admins: Vec<String>,
pub whitelist: Vec<String>,
pub banlist: HashMap<String, String>,
pub banlist: HashMap<Uuid, (String, String)>,
/// When set to None, loads the default map file (if available); otherwise,
/// uses the value of the file options to decide how to proceed.
pub map_file: Option<FileOpts>,