mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
move already-logged-in check to register
This commit is contained in:
parent
0ea102dc9c
commit
0c8448517e
@ -43,19 +43,6 @@ impl PendingLogin {
|
||||
impl Component for PendingLogin {
|
||||
type Storage = IdvStorage<Self>;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum LoginError {
|
||||
AlreadyLoggedIn(Uuid, String),
|
||||
RegisterError(RegisterError),
|
||||
}
|
||||
|
||||
impl From<RegisterError> for LoginError {
|
||||
fn from(inner: RegisterError) -> LoginError {
|
||||
LoginError::RegisterError(inner)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LoginProvider {
|
||||
runtime: Arc<Runtime>,
|
||||
accounts: HashMap<Uuid, String>,
|
||||
@ -86,11 +73,7 @@ impl LoginProvider {
|
||||
}
|
||||
}
|
||||
|
||||
fn login(&mut self, uuid: Uuid, username: String) -> Result<(), LoginError> {
|
||||
// make sure that the user is not logged in already
|
||||
if self.accounts.contains_key(&uuid) {
|
||||
return Err(LoginError::AlreadyLoggedIn(uuid, username));
|
||||
}
|
||||
fn login(&mut self, uuid: Uuid, username: String) -> Result<(), RegisterError> {
|
||||
info!(?username, "New User");
|
||||
self.accounts.insert(uuid, username);
|
||||
Ok(())
|
||||
@ -133,7 +116,7 @@ impl LoginProvider {
|
||||
admins: &HashSet<Uuid>,
|
||||
whitelist: &HashSet<Uuid>,
|
||||
banlist: &HashMap<Uuid, BanRecord>,
|
||||
) -> Option<Result<(String, Uuid), LoginError>> {
|
||||
) -> Option<Result<(String, Uuid), RegisterError>> {
|
||||
match pending.pending_r.try_recv() {
|
||||
Ok(Err(e)) => Some(Err(e.into())),
|
||||
Ok(Ok((username, uuid))) => {
|
||||
|
@ -1,4 +1,9 @@
|
||||
use crate::{EditableSettings, client::Client, login_provider::{LoginError, LoginProvider, PendingLogin}, metrics::PlayerMetrics};
|
||||
use crate::{
|
||||
client::Client,
|
||||
login_provider::{LoginProvider, PendingLogin},
|
||||
metrics::PlayerMetrics,
|
||||
EditableSettings,
|
||||
};
|
||||
use common::{
|
||||
comp::{Admin, Player, Stats},
|
||||
event::{EventBus, ServerEvent},
|
||||
@ -125,43 +130,39 @@ impl<'a> System<'a> for Sys {
|
||||
finished_pending.push(entity);
|
||||
trace!(?r, "pending login returned");
|
||||
match r {
|
||||
Err(LoginError::RegisterError(e)) => {
|
||||
Err(e) => {
|
||||
client.send(ServerRegisterAnswer::Err(e))?;
|
||||
return Ok(());
|
||||
}
|
||||
Err(LoginError::AlreadyLoggedIn(uuid, ref username)) => {
|
||||
if let Some((old_entity, old_client, _)) =
|
||||
(&entities, &clients, &players)
|
||||
.join()
|
||||
.find(|(_, _, old_player)| old_player.uuid() == uuid)
|
||||
{
|
||||
// Remove old client
|
||||
server_event_bus
|
||||
.emit_now(ServerEvent::ClientDisconnect(old_entity));
|
||||
let _ = old_client.send(ServerGeneral::Disconnect(
|
||||
DisconnectReason::Kicked(String::from(
|
||||
"You have logged in from another location.",
|
||||
)),
|
||||
));
|
||||
// We can't login the new client right now as the
|
||||
// removal of the old client and player occurs later in
|
||||
// the tick, so we instead setup the new login to be
|
||||
// processed in the next tick
|
||||
// Create "fake" successful pending auth and mark it to
|
||||
// be inserted into pending_logins at the end of this
|
||||
// run
|
||||
retries.push((
|
||||
entity,
|
||||
PendingLogin::new_success(username.to_string(), uuid),
|
||||
));
|
||||
}
|
||||
return Ok(());
|
||||
},
|
||||
Ok((username, uuid)) => (username, uuid),
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
// Check if user is already logged-in
|
||||
if let Some((old_entity, old_client, _)) = (&entities, &clients, &players)
|
||||
.join()
|
||||
.find(|(_, _, old_player)| old_player.uuid() == uuid)
|
||||
{
|
||||
// Remove old client
|
||||
server_event_bus.emit_now(ServerEvent::ClientDisconnect(old_entity));
|
||||
let _ = old_client.send(ServerGeneral::Disconnect(DisconnectReason::Kicked(
|
||||
String::from("You have logged in from another location."),
|
||||
)));
|
||||
// We can't login the new client right now as the
|
||||
// removal of the old client and player occurs later in
|
||||
// the tick, so we instead setup the new login to be
|
||||
// processed in the next tick
|
||||
// Create "fake" successful pending auth and mark it to
|
||||
// be inserted into pending_logins at the end of this
|
||||
// run
|
||||
retries.push((
|
||||
entity,
|
||||
PendingLogin::new_success(username.to_string(), uuid),
|
||||
));
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let player = Player::new(username, uuid);
|
||||
let is_admin = editable_settings.admins.contains(&uuid);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user