Wrap Context with Autentication enum

Context should contain information which is required to get
the information if the service call is authenticated.  Context
could be the username for example.  But services call other
services internally and for this, authentication must not be
checked.  In this case, they can now pass Authentication::Full
which always successfully authenticates.
This commit is contained in:
Simon Goller 2024-05-09 14:58:19 +02:00
parent bf94ec33de
commit b0000c0117
18 changed files with 252 additions and 217 deletions

View file

@ -1,10 +1,12 @@
use std::sync::Arc;
use std::fmt::Debug;
use async_trait::async_trait;
use time::PrimitiveDateTime;
use uuid::Uuid;
use crate::ServiceError;
use crate::permission::Authentication;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Booking {
@ -51,14 +53,14 @@ impl TryFrom<&Booking> for dao::booking::BookingEntity {
#[async_trait]
pub trait BookingService {
type Context: Clone + Send + Sync;
type Context: Clone + PartialEq + Eq + Debug + Send + Sync;
async fn get_all(&self, context: Self::Context) -> Result<Arc<[Booking]>, ServiceError>;
async fn get(&self, id: Uuid, context: Self::Context) -> Result<Booking, ServiceError>;
async fn get_all(&self, context: Authentication<Self::Context>) -> Result<Arc<[Booking]>, ServiceError>;
async fn get(&self, id: Uuid, context: Authentication<Self::Context>) -> Result<Booking, ServiceError>;
async fn create(
&self,
booking: &Booking,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<Booking, ServiceError>;
async fn delete(&self, id: Uuid, context: Self::Context) -> Result<(), ServiceError>;
async fn delete(&self, id: Uuid, context: Authentication<Self::Context>) -> Result<(), ServiceError>;
}

View file

@ -1,4 +1,5 @@
use std::sync::Arc;
use std::fmt::Debug;
use async_trait::async_trait;
use mockall::automock;
@ -41,63 +42,74 @@ impl From<&dao::PrivilegeEntity> for Privilege {
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Authentication<Context: Clone + PartialEq + Eq + Send + Sync + Debug + 'static>{
Full,
Context(Context),
}
impl<Context: Clone + Debug + PartialEq + Eq + Send + Sync + 'static> From<Context> for Authentication<Context> {
fn from(context: Context) -> Self {
Self::Context(context)
}
}
#[automock(type Context=();)]
#[async_trait]
pub trait PermissionService {
type Context: Clone + Send + Sync + 'static;
type Context: Clone + PartialEq + Eq + Debug + Send + Sync + 'static;
async fn check_permission(
&self,
privilege: &str,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError>;
async fn create_user(&self, user: &str, context: Self::Context) -> Result<(), ServiceError>;
async fn user_exists(&self, user: &str, context: Self::Context) -> Result<bool, ServiceError>;
async fn delete_user(&self, user: &str, context: Self::Context) -> Result<(), ServiceError>;
async fn get_all_users(&self, context: Self::Context) -> Result<Arc<[User]>, ServiceError>;
async fn create_user(&self, user: &str, context: Authentication<Self::Context>) -> Result<(), ServiceError>;
async fn user_exists(&self, user: &str, context: Authentication<Self::Context>) -> Result<bool, ServiceError>;
async fn delete_user(&self, user: &str, context: Authentication<Self::Context>) -> Result<(), ServiceError>;
async fn get_all_users(&self, context: Authentication<Self::Context>) -> Result<Arc<[User]>, ServiceError>;
async fn create_role(&self, role: &str, context: Self::Context) -> Result<(), ServiceError>;
async fn delete_role(&self, role: &str, context: Self::Context) -> Result<(), ServiceError>;
async fn get_all_roles(&self, context: Self::Context) -> Result<Arc<[Role]>, ServiceError>;
async fn create_role(&self, role: &str, context: Authentication<Self::Context>) -> Result<(), ServiceError>;
async fn delete_role(&self, role: &str, context: Authentication<Self::Context>) -> Result<(), ServiceError>;
async fn get_all_roles(&self, context: Authentication<Self::Context>) -> Result<Arc<[Role]>, ServiceError>;
async fn create_privilege(
&self,
privilege: &str,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError>;
async fn delete_privilege(
&self,
privilege: &str,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError>;
async fn get_all_privileges(
&self,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<Arc<[Privilege]>, ServiceError>;
async fn add_user_role(
&self,
user: &str,
role: &str,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError>;
async fn add_role_privilege(
&self,
role: &str,
privilege: &str,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError>;
async fn delete_role_privilege(
&self,
role: &str,
privilege: &str,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError>;
async fn delete_user_role(
&self,
user: &str,
role: &str,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError>;
}

View file

@ -1,10 +1,12 @@
use std::sync::Arc;
use std::fmt::Debug;
use async_trait::async_trait;
use mockall::automock;
use uuid::Uuid;
use crate::ServiceError;
use crate::permission::Authentication;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SalesPerson {
@ -40,31 +42,31 @@ impl From<&SalesPerson> for dao::sales_person::SalesPersonEntity {
#[automock(type Context=();)]
#[async_trait]
pub trait SalesPersonService {
type Context: Clone + Send + Sync + 'static;
type Context: Clone + Debug + PartialEq + Eq + Send + Sync + 'static;
async fn get_all(&self, context: Self::Context) -> Result<Arc<[SalesPerson]>, ServiceError>;
async fn get(&self, id: Uuid, context: Self::Context) -> Result<SalesPerson, ServiceError>;
async fn exists(&self, id: Uuid, context: Self::Context) -> Result<bool, ServiceError>;
async fn get_all(&self, context: Authentication<Self::Context>) -> Result<Arc<[SalesPerson]>, ServiceError>;
async fn get(&self, id: Uuid, context: Authentication<Self::Context>) -> Result<SalesPerson, ServiceError>;
async fn exists(&self, id: Uuid, context: Authentication<Self::Context>) -> Result<bool, ServiceError>;
async fn create(
&self,
item: &SalesPerson,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<SalesPerson, ServiceError>;
async fn update(
&self,
item: &SalesPerson,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<SalesPerson, ServiceError>;
async fn delete(&self, id: Uuid, context: Self::Context) -> Result<(), ServiceError>;
async fn delete(&self, id: Uuid, context: Authentication<Self::Context>) -> Result<(), ServiceError>;
async fn get_assigned_user(
&self,
sales_person_id: Uuid,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<Option<Arc<str>>, ServiceError>;
async fn set_user(
&self,
sales_person_id: Uuid,
user_id: Option<Arc<str>>,
context: Self::Context,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError>;
}

View file

@ -2,8 +2,10 @@ use async_trait::async_trait;
use mockall::automock;
use std::sync::Arc;
use uuid::Uuid;
use std::fmt::Debug;
use crate::ServiceError;
use crate::permission::Authentication;
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum DayOfWeek {
@ -85,12 +87,12 @@ impl From<&Slot> for dao::slot::SlotEntity {
#[automock(type Context=();)]
#[async_trait]
pub trait SlotService {
type Context: Clone + Send + Sync + 'static;
type Context: Clone + Debug + PartialEq + Eq + Send + Sync + 'static;
async fn get_slots(&self, context: Self::Context) -> Result<Arc<[Slot]>, ServiceError>;
async fn get_slot(&self, id: &Uuid, context: Self::Context) -> Result<Slot, ServiceError>;
async fn exists(&self, id: Uuid, context: Self::Context) -> Result<bool, ServiceError>;
async fn create_slot(&self, slot: &Slot, context: Self::Context) -> Result<Slot, ServiceError>;
async fn delete_slot(&self, id: &Uuid, context: Self::Context) -> Result<(), ServiceError>;
async fn update_slot(&self, slot: &Slot, context: Self::Context) -> Result<(), ServiceError>;
async fn get_slots(&self, context: Authentication<Self::Context>) -> Result<Arc<[Slot]>, ServiceError>;
async fn get_slot(&self, id: &Uuid, context: Authentication<Self::Context>) -> Result<Slot, ServiceError>;
async fn exists(&self, id: Uuid, context: Authentication<Self::Context>) -> Result<bool, ServiceError>;
async fn create_slot(&self, slot: &Slot, context: Authentication<Self::Context>) -> Result<Slot, ServiceError>;
async fn delete_slot(&self, id: &Uuid, context: Authentication<Self::Context>) -> Result<(), ServiceError>;
async fn update_slot(&self, slot: &Slot, context: Authentication<Self::Context>) -> Result<(), ServiceError>;
}

View file

@ -1,4 +1,5 @@
use std::sync::Arc;
use std::fmt::Debug;
use async_trait::async_trait;
use mockall::automock;
@ -8,7 +9,7 @@ use crate::ServiceError;
#[automock(type Context=();)]
#[async_trait]
pub trait UserService {
type Context: Clone + Send + Sync + 'static;
type Context: Clone + Debug + PartialEq + Eq + Send + Sync + 'static;
async fn current_user(&self, context: Self::Context) -> Result<Arc<str>, ServiceError>;
}