use crate::ids::{PrincipalId, RoleId, TenantId};
use crate::permission::Permission;
use crate::scope::GrantScope;
use async_trait::async_trait;
#[doc(hidden)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct EffectiveGrant {
pub role: RoleId,
pub permission: Permission,
pub scope: GrantScope,
}
impl EffectiveGrant {
pub fn new(role: RoleId, permission: Permission, scope: GrantScope) -> Self {
Self {
role,
permission,
scope,
}
}
}
#[async_trait]
pub trait Cache: Send + Sync {
async fn get_effective_grants(
&self,
tenant: &TenantId,
principal: &PrincipalId,
config_signature: &str,
) -> Option<Vec<EffectiveGrant>>;
async fn set_effective_grants(
&self,
tenant: &TenantId,
principal: &PrincipalId,
config_signature: &str,
grants: Vec<EffectiveGrant>,
);
async fn invalidate_principal(&self, tenant: &TenantId, principal: &PrincipalId);
async fn invalidate_role(&self, tenant: &TenantId, role: &RoleId);
async fn invalidate_tenant(&self, tenant: &TenantId);
async fn invalidate_all(&self);
}
#[derive(Debug, Default, Clone, Copy)]
pub struct NoCache;
#[async_trait]
impl Cache for NoCache {
async fn get_effective_grants(
&self,
_tenant: &TenantId,
_principal: &PrincipalId,
_config_signature: &str,
) -> Option<Vec<EffectiveGrant>> {
None
}
async fn set_effective_grants(
&self,
_tenant: &TenantId,
_principal: &PrincipalId,
_config_signature: &str,
_grants: Vec<EffectiveGrant>,
) {
}
async fn invalidate_principal(&self, _tenant: &TenantId, _principal: &PrincipalId) {}
async fn invalidate_role(&self, _tenant: &TenantId, _role: &RoleId) {}
async fn invalidate_tenant(&self, _tenant: &TenantId) {}
async fn invalidate_all(&self) {}
}