use std::collections::BTreeMap;
use crate::core::metadata::Metadata;
use crate::core::object_id::ObjectId;
pub use crate::security::actor::ActorId;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum Operation {
Create,
Read,
Write,
Move,
Copy,
Delete,
ChangeMetadata,
ChangeOwner,
Execute,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AuthorizationResult {
Allowed,
Denied {
reason: Option<String>,
},
}
impl AuthorizationResult {
pub fn is_allowed(&self) -> bool {
matches!(self, Self::Allowed)
}
}
#[derive(Clone, Debug)]
pub struct ParentReference<'a> {
pub object: ObjectId,
pub metadata: &'a Metadata,
}
#[derive(Clone, Debug)]
pub struct AuthorizationRequest<'a> {
pub actor: Option<ActorId>,
pub operation: Operation,
pub object: ObjectId,
pub metadata: &'a Metadata,
pub parent: Option<ParentReference<'a>>,
}
pub trait AuthorizationPolicy: Send + Sync {
fn authorize(&self, request: &AuthorizationRequest<'_>) -> AuthorizationResult;
}
#[derive(Clone, Copy, Debug, Default)]
pub struct PermissivePolicy;
impl AuthorizationPolicy for PermissivePolicy {
fn authorize(&self, _request: &AuthorizationRequest<'_>) -> AuthorizationResult {
AuthorizationResult::Allowed
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct AuthorizationData {
fields: BTreeMap<String, String>,
}
impl AuthorizationData {
pub fn new() -> Self {
Self::default()
}
pub fn insert(&mut self, key: impl Into<String>, value: impl Into<String>) -> Option<String> {
self.fields.insert(key.into(), value.into())
}
pub fn remove(&mut self, key: &str) -> Option<String> {
self.fields.remove(key)
}
pub fn get(&self, key: &str) -> Option<&str> {
self.fields.get(key).map(String::as_str)
}
pub fn fields(&self) -> &BTreeMap<String, String> {
&self.fields
}
pub fn is_empty(&self) -> bool {
self.fields.is_empty()
}
}