Restructuring: Move permission code in separate modules

This commit is contained in:
Simon Goller 2024-04-30 15:06:12 +02:00
parent b964063dbb
commit 82e89baeeb
12 changed files with 799 additions and 784 deletions

View file

@ -3,6 +3,14 @@ use mockall::automock;
use std::{future::Future, sync::Arc};
use thiserror::Error;
pub mod permission;
pub use permission::MockPermissionService;
pub use permission::PermissionService;
pub use permission::Privilege;
pub use permission::Role;
pub use permission::User;
#[derive(Debug, Error)]
pub enum ServiceError {
#[error("Database query error: {0}")]
@ -17,65 +25,6 @@ pub trait HelloService {
fn hello(&self) -> impl Future<Output = Result<Arc<str>, ServiceError>> + Send;
}
#[derive(Debug, PartialEq, Eq)]
pub struct User {
pub name: Arc<str>,
}
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<str>,
}
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<str>,
}
impl From<&dao::PrivilegeEntity> for Privilege {
fn from(privilege: &dao::PrivilegeEntity) -> Self {
Self {
name: privilege.name.clone(),
}
}
}
#[automock]
#[async_trait]
pub trait PermissionService {
async fn check_permission(&self, privilege: &str) -> Result<(), ServiceError>;
async fn create_user(&self, user: &str) -> Result<(), ServiceError>;
async fn delete_user(&self, user: &str) -> Result<(), ServiceError>;
async fn get_all_users(&self) -> Result<Arc<[User]>, ServiceError>;
async fn create_role(&self, role: &str) -> Result<(), ServiceError>;
async fn delete_role(&self, role: &str) -> Result<(), ServiceError>;
async fn get_all_roles(&self) -> Result<Arc<[Role]>, ServiceError>;
async fn create_privilege(&self, privilege: &str) -> Result<(), ServiceError>;
async fn delete_privilege(&self, privilege: &str) -> Result<(), ServiceError>;
async fn get_all_privileges(&self) -> Result<Arc<[Privilege]>, ServiceError>;
async fn add_user_role(&self, user: &str, role: &str) -> Result<(), ServiceError>;
async fn add_role_privilege(&self, role: &str, privilege: &str) -> Result<(), ServiceError>;
async fn delete_role_privilege(&self, role: &str, privilege: &str) -> Result<(), ServiceError>;
async fn delete_user_role(&self, user: &str, role: &str) -> Result<(), ServiceError>;
}
#[automock]
#[async_trait]
pub trait UserService {

65
service/src/permission.rs Normal file
View file

@ -0,0 +1,65 @@
use std::sync::Arc;
use async_trait::async_trait;
use mockall::automock;
use crate::ServiceError;
#[derive(Debug, PartialEq, Eq)]
pub struct User {
pub name: Arc<str>,
}
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<str>,
}
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<str>,
}
impl From<&dao::PrivilegeEntity> for Privilege {
fn from(privilege: &dao::PrivilegeEntity) -> Self {
Self {
name: privilege.name.clone(),
}
}
}
#[automock]
#[async_trait]
pub trait PermissionService {
async fn check_permission(&self, privilege: &str) -> Result<(), ServiceError>;
async fn create_user(&self, user: &str) -> Result<(), ServiceError>;
async fn delete_user(&self, user: &str) -> Result<(), ServiceError>;
async fn get_all_users(&self) -> Result<Arc<[User]>, ServiceError>;
async fn create_role(&self, role: &str) -> Result<(), ServiceError>;
async fn delete_role(&self, role: &str) -> Result<(), ServiceError>;
async fn get_all_roles(&self) -> Result<Arc<[Role]>, ServiceError>;
async fn create_privilege(&self, privilege: &str) -> Result<(), ServiceError>;
async fn delete_privilege(&self, privilege: &str) -> Result<(), ServiceError>;
async fn get_all_privileges(&self) -> Result<Arc<[Privilege]>, ServiceError>;
async fn add_user_role(&self, user: &str, role: &str) -> Result<(), ServiceError>;
async fn add_role_privilege(&self, role: &str, privilege: &str) -> Result<(), ServiceError>;
async fn delete_role_privilege(&self, role: &str, privilege: &str) -> Result<(), ServiceError>;
async fn delete_user_role(&self, user: &str, role: &str) -> Result<(), ServiceError>;
}