Easily get current signed in user in requests

This commit is contained in:
Pierre HUBERT 2022-04-05 17:40:15 +02:00
parent 30c8470bcb
commit fc92b9452a
4 changed files with 50 additions and 13 deletions

View File

@ -1,5 +1,4 @@
use actix::Addr;
use actix_identity::Identity;
use actix_web::{HttpResponse, Responder, web};
use askama::Template;
@ -7,8 +6,8 @@ use crate::actors::{bruteforce_actor, users_actor};
use crate::actors::bruteforce_actor::BruteForceActor;
use crate::actors::users_actor::UsersActor;
use crate::constants::{APP_NAME, MAX_FAILED_LOGIN_ATTEMPTS, MIN_PASS_LEN};
use crate::data::current_user::CurrentUser;
use crate::data::remote_ip::RemoteIP;
use crate::data::session_identity::SessionIdentity;
use crate::data::user::User;
#[derive(Template)]
@ -55,11 +54,8 @@ struct ChangePasswordPage {
}
/// Account details page
pub async fn account_settings_details_route(id: Identity, users: web::Data<Addr<UsersActor>>) -> impl Responder {
let user: User = users.send(
users_actor::GetUserRequest(SessionIdentity(&id).user_id())
).await.unwrap().0.unwrap();
pub async fn account_settings_details_route(user: CurrentUser) -> impl Responder {
let user = user.into();
HttpResponse::Ok()
.body(AccountDetailsPage {
_parent: BaseSettingsPage::get("Account details", &user, None, None).await,
@ -78,7 +74,7 @@ pub struct PassChangeRequest {
}
/// Change password route
pub async fn change_password_route(id: Identity,
pub async fn change_password_route(user: CurrentUser,
users: web::Data<Addr<UsersActor>>,
req: Option<web::Form<PassChangeRequest>>,
bruteforce: web::Data<Addr<BruteForceActor>>,
@ -86,9 +82,7 @@ pub async fn change_password_route(id: Identity,
let mut danger = None;
let mut success = None;
let user: User = users.send(
users_actor::GetUserRequest(SessionIdentity(&id).user_id())
).await.unwrap().0.unwrap();
let user: User = user.into();
let failed_attempts = bruteforce.send(bruteforce_actor::CountFailedAttempt { ip: remote_ip.into() }).await.unwrap();
if failed_attempts > MAX_FAILED_LOGIN_ATTEMPTS {

42
src/data/current_user.rs Normal file
View File

@ -0,0 +1,42 @@
use std::future::Future;
use std::pin::Pin;
use actix::Addr;
use actix_identity::Identity;
use actix_web::{Error, FromRequest, HttpRequest, web};
use actix_web::dev::Payload;
use crate::actors::users_actor;
use crate::actors::users_actor::UsersActor;
use crate::data::session_identity::SessionIdentity;
use crate::data::user::User;
pub struct CurrentUser(User);
impl From<CurrentUser> for User {
fn from(user: CurrentUser) -> Self {
user.0
}
}
impl FromRequest for CurrentUser {
type Error = Error;
type Future = Pin<Box<dyn Future<Output=Result<Self, Self::Error>>>>;
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
let user_actor: &web::Data<Addr<UsersActor>> = req.app_data().expect("UserActor undefined!");
let user_actor: Addr<UsersActor> = user_actor.as_ref().clone();
let identity: Identity = Identity::from_request(req, payload).into_inner()
.expect("Failed to get identity!");
let user_id = SessionIdentity(&identity).user_id();
Box::pin(async move {
let user: User = user_actor.send(
users_actor::GetUserRequest(user_id)
).await.unwrap().0.unwrap();
Ok(CurrentUser(user))
})
}
}

View File

@ -3,4 +3,5 @@ pub mod entity_manager;
pub mod service;
pub mod session_identity;
pub mod user;
pub mod remote_ip;
pub mod remote_ip;
pub mod current_user;

View File

@ -18,7 +18,7 @@ impl From<RemoteIP> for IpAddr {
impl FromRequest for RemoteIP {
type Error = Error;
type Future = Ready<Result<RemoteIP, Error>>;
type Future = Ready<Result<Self, Error>>;
#[inline]
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {