Browse Source

handle unactivated accounts in the correct spot

pbs
jake 4 years ago
parent
commit
a14aee4e29
  1. 1
      src/bin/main.rs
  2. 1
      src/entity/account.rs
  3. 2
      src/entity/gateway/inmemory.rs
  4. 3
      src/entity/gateway/postgres/postgres.rs
  5. 4
      src/login/login.rs
  6. 31
      src/ship/packet/handler/auth.rs
  7. 1
      tests/common.rs

1
src/bin/main.rs

@ -60,6 +60,7 @@ fn main() {
muted_until: None,
created_at: chrono::Utc::now(),
flags: 0,
activated: true,
};
let fake_user = entity_gateway.create_user(fake_user).await.unwrap();
entity_gateway.create_user_settings(NewUserSettingsEntity::new(fake_user.id)).await.unwrap();

1
src/entity/account.rs

@ -23,6 +23,7 @@ pub struct NewUserAccountEntity {
pub muted_until: Option<chrono::DateTime<chrono::Utc>>,
pub created_at: chrono::DateTime<chrono::Utc>,
pub flags: u32,
pub activated: bool,
}
#[derive(Clone, Debug)]

2
src/entity/gateway/inmemory.rs

@ -47,7 +47,7 @@ impl EntityGateway for InMemoryGateway {
muted_until: user.muted_until,
created_at: user.created_at,
flags: user.flags,
activated: true,
activated: user.activated,
};
users.insert(user.id, user.clone());
Ok(user)

3
src/entity/gateway/postgres/postgres.rs

@ -93,10 +93,11 @@ impl PostgresGateway {
#[async_trait::async_trait]
impl EntityGateway for PostgresGateway {
async fn create_user(&mut self, user: NewUserAccountEntity) -> Result<UserAccountEntity, GatewayError> {
let new_user = sqlx::query_as::<_, PgUserAccount>("insert into user_accounts (email, username, password) values ($1, $2, $3) returning *;")
let new_user = sqlx::query_as::<_, PgUserAccount>("insert into user_accounts (email, username, password, activated) values ($1, $2, $3, $4) returning *;")
.bind(user.email)
.bind(user.username)
.bind(user.password)
.bind(user.activated)
.fetch_one(&self.pool).await?;
Ok(new_user.into())
}

4
src/login/login.rs

@ -61,6 +61,10 @@ pub async fn get_login_status(entity_gateway: &impl EntityGateway, pkt: &Login)
let username = array_to_utf8(pkt.username).map_err(|_err| AccountStatus::Error)?;
let password = array_to_utf8(pkt.password).map_err(|_err| AccountStatus::Error)?;
let user = entity_gateway.get_user_by_name(username).await.map_err(|_| AccountStatus::InvalidUser)?;
if !user.activated {
return Err(AccountStatus::PayUp)
}
let verified = bcrypt::verify(password, user.password.as_str()).map_err(|_err| AccountStatus::Error)?;
match verified {
true => if user.banned_until.map(|banned| banned > chrono::Utc::now()).unwrap_or(false) {

31
src/ship/packet/handler/auth.rs

@ -15,25 +15,20 @@ pub async fn validate_login<EG: EntityGateway>(id: ClientId,
-> Result<Vec<SendShipPacket>, ShipError> {
Ok(match get_login_status(entity_gateway, pkt).await {
Ok(user) => {
if user.activated {
let mut response = LoginResponse::by_status(AccountStatus::Ok, Session::new());
response.guildcard = user.id.0 as u32;
response.team_id = user.team_id.map_or(31, |ti| ti) as u32;
let characters = entity_gateway.get_characters_by_user(&user).await?;
let character = characters
.get(pkt.session.character_slot as usize)
.ok_or(ShipError::InvalidSlot(id, pkt.session.character_slot as u32))?.as_ref()
.ok_or(ShipError::NoCharacterInSlot(id, pkt.session.character_slot as u32))?
.clone();
let settings = entity_gateway.get_user_settings_by_user(&user).await?;
let mut response = LoginResponse::by_status(AccountStatus::Ok, Session::new());
response.guildcard = user.id.0 as u32;
response.team_id = user.team_id.map_or(31, |ti| ti) as u32;
let characters = entity_gateway.get_characters_by_user(&user).await?;
let character = characters
.get(pkt.session.character_slot as usize)
.ok_or(ShipError::InvalidSlot(id, pkt.session.character_slot as u32))?.as_ref()
.ok_or(ShipError::NoCharacterInSlot(id, pkt.session.character_slot as u32))?
.clone();
let settings = entity_gateway.get_user_settings_by_user(&user).await?;
item_manager.load_character(entity_gateway, &character).await?;
clients.insert(id, ClientState::new(user, settings, character, pkt.session));
vec![SendShipPacket::LoginResponse(response), SendShipPacket::ShipBlockList(ShipBlockList::new(&&ship_name, 3))]
}
else {
vec![SendShipPacket::LoginResponse(LoginResponse::by_status(AccountStatus::Locked, Session::new()))]
}
item_manager.load_character(entity_gateway, &character).await?;
clients.insert(id, ClientState::new(user, settings, character, pkt.session));
vec![SendShipPacket::LoginResponse(response), SendShipPacket::ShipBlockList(ShipBlockList::new(&&ship_name, 3))]
},
Err(err) => {
vec![SendShipPacket::LoginResponse(LoginResponse::by_status(err, Session::new()))]

1
tests/common.rs

@ -23,6 +23,7 @@ pub async fn new_user_character<EG: EntityGateway>(entity_gateway: &mut EG, user
muted_until: None,
created_at: chrono::Utc::now(),
flags: 0,
activated: true,
};
let user = entity_gateway.create_user(new_user).await.unwrap();

Loading…
Cancel
Save