Initial commit
This commit is contained in:
commit
dccfa2d4cf
19 changed files with 3156 additions and 0 deletions
12
service_impl/Cargo.toml
Normal file
12
service_impl/Cargo.toml
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
[package]
|
||||
name = "service_impl"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies.service]
|
||||
path = "../service"
|
||||
|
||||
[dependencies.dao]
|
||||
path = "../dao"
|
||||
71
service_impl/src/lib.rs
Normal file
71
service_impl/src/lib.rs
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
pub struct HelloServiceImpl<HelloDao, PermissionService>
|
||||
where
|
||||
HelloDao: dao::HelloDao + Sync + Send,
|
||||
PermissionService: service::PermissionService + Sync + Send,
|
||||
{
|
||||
hello_dao: Arc<HelloDao>,
|
||||
permission_service: Arc<PermissionService>,
|
||||
}
|
||||
impl<HelloDao, PermissionService> HelloServiceImpl<HelloDao, PermissionService>
|
||||
where
|
||||
HelloDao: dao::HelloDao + Sync + Send,
|
||||
PermissionService: service::PermissionService + Sync + Send,
|
||||
{
|
||||
pub fn new(hello_dao: Arc<HelloDao>, permission_service: Arc<PermissionService>) -> Self {
|
||||
Self {
|
||||
hello_dao,
|
||||
permission_service,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<HelloDao, PermissionService> service::HelloService
|
||||
for HelloServiceImpl<HelloDao, PermissionService>
|
||||
where
|
||||
HelloDao: dao::HelloDao + Sync + Send,
|
||||
PermissionService: service::PermissionService + Sync + Send,
|
||||
{
|
||||
async fn hello(&self) -> Result<Arc<str>, service::ServiceError> {
|
||||
self.permission_service.check_permission("hello").await?;
|
||||
Ok(self.hello_dao.get_hello().await?)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PermissionServiceImpl<PermissionDao>
|
||||
where
|
||||
PermissionDao: dao::PermissionDao + Send + Sync,
|
||||
{
|
||||
permission_dao: Arc<PermissionDao>,
|
||||
}
|
||||
impl<PermissionDao> PermissionServiceImpl<PermissionDao>
|
||||
where
|
||||
PermissionDao: dao::PermissionDao + Send + Sync,
|
||||
{
|
||||
pub fn new(permission_dao: Arc<PermissionDao>) -> Self {
|
||||
Self { permission_dao }
|
||||
}
|
||||
}
|
||||
|
||||
impl<PermissionDao> service::PermissionService for PermissionServiceImpl<PermissionDao>
|
||||
where
|
||||
PermissionDao: dao::PermissionDao + Send + Sync,
|
||||
{
|
||||
async fn check_permission(&self, privilege: &str) -> Result<(), service::ServiceError> {
|
||||
let current_user = self.current_user().await?;
|
||||
if self
|
||||
.permission_dao
|
||||
.has_privilege(current_user.as_ref(), privilege)
|
||||
.await?
|
||||
{
|
||||
Ok(())
|
||||
} else {
|
||||
Err(service::ServiceError::Forbidden)
|
||||
}
|
||||
}
|
||||
|
||||
async fn current_user(&self) -> Result<Arc<str>, service::ServiceError> {
|
||||
Ok("DEVUSER".into())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue