Compare commits

..

2 Commits

3 changed files with 18 additions and 8 deletions

View File

@ -7,6 +7,7 @@ use crate::data::user::{User, UserID, verify_password};
pub enum LoginResult { pub enum LoginResult {
AccountNotFound, AccountNotFound,
InvalidPassword, InvalidPassword,
AccountDisabled,
Success(User), Success(User),
} }
@ -54,6 +55,10 @@ impl Handler<LoginRequest> for UsersActor {
return MessageResult(LoginResult::InvalidPassword); return MessageResult(LoginResult::InvalidPassword);
} }
if !user.enabled {
return MessageResult(LoginResult::AccountDisabled);
}
MessageResult(LoginResult::Success(user)) MessageResult(LoginResult::Success(user))
} }
} }

View File

@ -114,6 +114,11 @@ pub async fn login_route(users: web::Data<Addr<UsersActor>>,
} }
} }
LoginResult::AccountDisabled => {
log::warn!("Failed login for username {} : account is disabled", login);
danger = "Your account is disabled!".to_string();
}
c => { c => {
// TODO : add bruteforce detection // TODO : add bruteforce detection
log::warn!("Failed login for username {} : {:?}", login, c); log::warn!("Failed login for username {} : {:?}", login, c);

View File

@ -11,7 +11,7 @@ use askama::Template;
use crate::constants::{ADMIN_ROUTES, AUTHENTICATED_ROUTES}; use crate::constants::{ADMIN_ROUTES, AUTHENTICATED_ROUTES};
use crate::controllers::base_controller::redirect_user_for_login; use crate::controllers::base_controller::redirect_user_for_login;
use crate::data::session_identity::{SessionIdentity, SessionIdentityData}; use crate::data::session_identity::{SessionIdentity, SessionIdentityData, SessionStatus};
// There are two steps in middleware processing. // There are two steps in middleware processing.
// 1. Middleware initialization, middleware factory gets called with // 1. Middleware initialization, middleware factory gets called with
@ -40,19 +40,19 @@ impl<S, B> Transform<S, ServiceRequest> for AuthMiddleware
} }
#[derive(Debug)] #[derive(Debug)]
enum SessionStatus { enum ConnStatus {
SignedOut, SignedOut,
RegularUser, RegularUser,
Admin, Admin,
} }
impl SessionStatus { impl ConnStatus {
pub fn is_auth(&self) -> bool { pub fn is_auth(&self) -> bool {
!matches!(self, SessionStatus::SignedOut) !matches!(self, ConnStatus::SignedOut)
} }
pub fn is_admin(&self) -> bool { pub fn is_admin(&self) -> bool {
matches!(self, SessionStatus::Admin) matches!(self, ConnStatus::Admin)
} }
} }
@ -92,9 +92,9 @@ impl<S, B> Service<ServiceRequest> for AuthInnerMiddleware<S>
} }
let identity = match SessionIdentity::deserialize_session_data(req.get_identity()) { let identity = match SessionIdentity::deserialize_session_data(req.get_identity()) {
None => SessionStatus::SignedOut, Some(SessionIdentityData { status: SessionStatus::SignedIn, is_admin: true, .. }) => ConnStatus::Admin,
Some(SessionIdentityData { is_admin: true, .. }) => SessionStatus::Admin, Some(SessionIdentityData { status: SessionStatus::SignedIn, .. }) => ConnStatus::RegularUser,
_ => SessionStatus::RegularUser, _ => ConnStatus::SignedOut,
}; };
// Redirect user to login page // Redirect user to login page