login trim username, improve validation, social ordering

This commit is contained in:
aljazerzen 2021-04-01 16:37:13 +02:00
parent 3506f754aa
commit 08146f6a3a
5 changed files with 44 additions and 22 deletions

View File

@ -14,15 +14,21 @@ pub struct Player {
impl Player { impl Player {
pub fn new(alias: String, uuid: Uuid) -> Self { Self { alias, uuid } } pub fn new(alias: String, uuid: Uuid) -> Self { Self { alias, uuid } }
pub fn is_valid(&self) -> bool { Self::alias_is_valid(&self.alias) } pub fn is_valid(&self) -> bool { Self::alias_validate(&self.alias).is_ok() }
pub fn alias_is_valid(alias: &str) -> bool { pub fn alias_validate(alias: &str) -> Result<(), AliasError> {
// TODO: Expose auth name validation and use it here. // TODO: Expose auth name validation and use it here.
// See https://gitlab.com/veloren/auth/-/blob/master/server/src/web.rs#L20 // See https://gitlab.com/veloren/auth/-/blob/master/server/src/web.rs#L20
alias if !alias
.chars() .chars()
.all(|c| c.is_alphanumeric() || c == '_' || c == '-') .all(|c| c.is_alphanumeric() || c == '_' || c == '-')
&& alias.len() <= MAX_ALIAS_LEN {
Err(AliasError::ForbiddenCharacters)
} else if alias.len() > MAX_ALIAS_LEN {
Err(AliasError::TooLong)
} else {
Ok(())
}
} }
/// Not to be confused with uid /// Not to be confused with uid
@ -38,3 +44,18 @@ pub struct Respawn;
impl Component for Respawn { impl Component for Respawn {
type Storage = NullStorage<Self>; type Storage = NullStorage<Self>;
} }
pub enum AliasError {
ForbiddenCharacters,
TooLong,
}
impl ToString for AliasError {
fn to_string(&self) -> String {
match *self {
AliasError::ForbiddenCharacters => "Alias contains illegal characters.",
AliasError::TooLong => "Alias is too long.",
}
.to_string()
}
}

View File

@ -751,11 +751,11 @@ fn handle_alias(
return; return;
} }
if let Ok(alias) = scan_fmt!(&args, &action.arg_fmt(), String) { if let Ok(alias) = scan_fmt!(&args, &action.arg_fmt(), String) {
if !comp::Player::alias_is_valid(&alias) { if let Err(error) = comp::Player::alias_validate(&alias) {
// Prevent silly aliases // Prevent silly aliases
server.notify_client( server.notify_client(
client, client,
ServerGeneral::server_msg(ChatType::CommandError, "Invalid alias."), ServerGeneral::server_msg(ChatType::CommandError, error.to_string()),
); );
return; return;
} }

View File

@ -386,8 +386,8 @@ impl<'a> Widget for Social<'a> {
player player
.character .character
.as_ref() .as_ref()
.map(|character| &character.name) .map(|character| character.name.to_lowercase())
.unwrap_or(&player.player_alias) .unwrap_or_else(|| player.player_alias.to_string())
}); });
for (i, (&uid, player_info)) in player_list.into_iter().enumerate() { for (i, (&uid, player_info)) in player_list.into_iter().enumerate() {
let hide_username = true; let hide_username = true;

View File

@ -340,18 +340,19 @@ fn attempt_login(
client_init: &mut Option<ClientInit>, client_init: &mut Option<ClientInit>,
runtime: Option<Arc<runtime::Runtime>>, runtime: Option<Arc<runtime::Runtime>>,
) { ) {
if comp::Player::alias_is_valid(&username) { if let Err(err) = comp::Player::alias_validate(&username) {
// Don't try to connect if there is already a connection in progress. *info_message = Some(err.to_string());
if client_init.is_none() { return;
*client_init = Some(ClientInit::new( }
connection_args,
username, // Don't try to connect if there is already a connection in progress.
Some(settings.graphics.view_distance), if client_init.is_none() {
password, *client_init = Some(ClientInit::new(
runtime, connection_args,
)); username,
} Some(settings.graphics.view_distance),
} else { password,
*info_message = Some("Invalid username".to_string()); runtime,
));
} }
} }

View File

@ -354,7 +354,7 @@ impl Controls {
}; };
events.push(Event::LoginAttempt { events.push(Event::LoginAttempt {
username: self.login_info.username.clone(), username: self.login_info.username.trim().to_string(),
password: self.login_info.password.clone(), password: self.login_info.password.clone(),
server_address: self.login_info.server.clone(), server_address: self.login_info.server.clone(),
}); });