Refactor family structures

This commit is contained in:
Pierre HUBERT 2023-06-22 15:07:45 +02:00
parent 4e1c78724f
commit b655ac5910

View File

@ -1,9 +1,10 @@
use crate::models::{FamilyID, Membership, UserID}; use crate::models::{FamilyID, Membership};
use crate::services::families_service; use crate::services::families_service;
use crate::services::login_token_service::LoginToken; use crate::services::login_token_service::LoginToken;
use actix_web::dev::Payload; use actix_web::dev::Payload;
use actix_web::{FromRequest, HttpRequest}; use actix_web::{FromRequest, HttpRequest};
use serde::Deserialize; use serde::Deserialize;
use std::ops::Deref;
#[derive(Debug)] #[derive(Debug)]
pub struct FamilyInPath(Membership); pub struct FamilyInPath(Membership);
@ -15,31 +16,21 @@ impl FamilyInPath {
async fn load_family_from_path(t: &LoginToken, id: FamilyID) -> anyhow::Result<Self> { async fn load_family_from_path(t: &LoginToken, id: FamilyID) -> anyhow::Result<Self> {
Ok(Self(families_service::get_membership(id, t.user_id).await?)) Ok(Self(families_service::get_membership(id, t.user_id).await?))
} }
pub fn user_id(&self) -> UserID {
self.0.user_id()
} }
pub fn family_id(&self) -> FamilyID { impl Deref for FamilyInPath {
self.0.family_id() type Target = Membership;
}
pub fn is_admin(&self) -> bool { fn deref(&self) -> &Self::Target {
self.0.is_admin &self.0
} }
} }
impl FamilyInPathWithAdminMembership { impl Deref for FamilyInPathWithAdminMembership {
pub fn user_id(&self) -> UserID { type Target = FamilyInPath;
self.0.user_id()
}
pub fn family_id(&self) -> FamilyID { fn deref(&self) -> &Self::Target {
self.0.family_id() &self.0
}
pub fn is_admin(&self) -> bool {
self.0.is_admin()
} }
} }
@ -81,7 +72,7 @@ impl FromRequest for FamilyInPathWithAdminMembership {
Box::pin(async move { Box::pin(async move {
let family = FamilyInPath::extract(&req).await?; let family = FamilyInPath::extract(&req).await?;
if !family.is_admin() { if !family.is_admin {
log::error!( log::error!(
"The user {:?} attempted to perform restricted action on family {:?}!", "The user {:?} attempted to perform restricted action on family {:?}!",
family.user_id(), family.user_id(),