use std::fmt::Debug; use std::sync::Arc; use async_trait::async_trait; use mockall::automock; use crate::ServiceError; pub const SALES_PRIVILEGE: &str = "sales"; pub const HR_PRIVILEGE: &str = "hr"; pub const SHIFTPLANNER_PRIVILEGE: &str = "shiftplanner"; /// For mocking the context locally since there is actually /// no context. #[derive(Clone, Debug, PartialEq, Eq)] pub struct MockContext; #[derive(Debug, PartialEq, Eq)] pub struct User { pub name: Arc, } impl From<&dao::UserEntity> for User { fn from(user: &dao::UserEntity) -> Self { Self { name: user.name.clone(), } } } #[derive(Debug, PartialEq, Eq)] pub struct Role { pub name: Arc, } impl From<&dao::RoleEntity> for Role { fn from(role: &dao::RoleEntity) -> Self { Self { name: role.name.clone(), } } } #[derive(Debug, PartialEq, Eq)] pub struct Privilege { pub name: Arc, } impl From<&dao::PrivilegeEntity> for Privilege { fn from(privilege: &dao::PrivilegeEntity) -> Self { Self { name: privilege.name.clone(), } } } #[derive(Clone, Debug, PartialEq, Eq)] pub enum Authentication { Full, Context(Context), } impl From for Authentication { fn from(context: Context) -> Self { Self::Context(context) } } #[automock(type Context=();)] #[async_trait] pub trait PermissionService { type Context: Clone + PartialEq + Eq + Debug + Send + Sync + 'static; async fn current_user_id( &self, context: Authentication, ) -> Result>, ServiceError>; async fn check_permission( &self, privilege: &str, context: Authentication, ) -> Result<(), ServiceError>; async fn check_user( &self, user: &str, context: Authentication, ) -> Result<(), ServiceError>; async fn get_privileges_for_current_user( &self, context: Authentication, ) -> Result, ServiceError>; async fn create_user( &self, user: &str, context: Authentication, ) -> Result<(), ServiceError>; async fn user_exists( &self, user: &str, context: Authentication, ) -> Result; async fn delete_user( &self, user: &str, context: Authentication, ) -> Result<(), ServiceError>; async fn get_all_users( &self, context: Authentication, ) -> Result, ServiceError>; async fn create_role( &self, role: &str, context: Authentication, ) -> Result<(), ServiceError>; async fn delete_role( &self, role: &str, context: Authentication, ) -> Result<(), ServiceError>; async fn get_all_roles( &self, context: Authentication, ) -> Result, ServiceError>; async fn create_privilege( &self, privilege: &str, context: Authentication, ) -> Result<(), ServiceError>; async fn delete_privilege( &self, privilege: &str, context: Authentication, ) -> Result<(), ServiceError>; async fn get_all_privileges( &self, context: Authentication, ) -> Result, ServiceError>; async fn add_user_role( &self, user: &str, role: &str, context: Authentication, ) -> Result<(), ServiceError>; async fn add_role_privilege( &self, role: &str, privilege: &str, context: Authentication, ) -> Result<(), ServiceError>; async fn delete_role_privilege( &self, role: &str, privilege: &str, context: Authentication, ) -> Result<(), ServiceError>; async fn delete_user_role( &self, user: &str, role: &str, context: Authentication, ) -> Result<(), ServiceError>; }