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