use super::*;
impl PermissionCheckerDefault {
pub fn new(interface: Arc<dyn GarrisonInterface>) -> Self {
Self { interface }
}
}
#[async_trait]
impl PermissionChecker for PermissionCheckerDefault {
async fn has_permission(&self, login_id: &str, permission: &str) -> GarrisonResult<bool> {
let normalized = permission.nfc().collect::<String>();
if normalized.is_empty() {
return Err(GarrisonError::InvalidParam("core-perm-empty::".to_string()));
}
if normalized.len() > 256 {
return Err(GarrisonError::InvalidParam(format!(
"permission too long: {} bytes (max 256)",
normalized.len()
)));
}
let perms = self.interface.get_permission_list(login_id).await?;
Ok(perms.iter().any(|p| p == &normalized))
}
async fn has_role(&self, login_id: &str, role: &str) -> GarrisonResult<bool> {
if role.is_empty() {
return Err(GarrisonError::InvalidParam("core-role-empty::".to_string()));
}
let roles = self.interface.get_role_list(login_id).await?;
Ok(roles.iter().any(|r| r == role))
}
async fn has_any_permission(&self, login_id: &str, perms: &[&str]) -> bool {
for perm in perms {
if self.has_permission(login_id, perm).await.unwrap_or(false) {
return true;
}
}
false
}
async fn has_all_permissions(&self, login_id: &str, perms: &[&str]) -> bool {
for perm in perms {
if !self.has_permission(login_id, perm).await.unwrap_or(false) {
return false;
}
}
true
}
}