use serde::{Deserialize, Serialize};
use crate::grant::Effect;
use crate::resource::Resource;
use crate::subject::Subject;
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct Request<'a> {
pub subject: &'a Subject,
pub permission: &'a str,
pub resource: &'a Resource,
pub field: Option<&'a str>,
}
impl<'a> Request<'a> {
pub fn new(subject: &'a Subject, permission: &'a str, resource: &'a Resource) -> Self {
Self {
subject,
permission,
resource,
field: None,
}
}
#[must_use]
pub fn field(mut self, field: &'a str) -> Self {
self.field = Some(field);
self
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum Reason {
Override { name: String },
Grant {
grant_id: String,
permission: String,
rank: usize,
strategy: String,
},
NoApplicableGrant,
UnknownPermission,
MissingPrerequisite { permission: String, prerequisite: String },
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Decision {
pub effect: Effect,
pub reason: Reason,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub matched: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub warnings: Vec<String>,
}
impl Decision {
pub fn is_allowed(&self) -> bool {
self.effect == Effect::Allow
}
pub(crate) fn deny(reason: Reason) -> Self {
Self {
effect: Effect::Deny,
reason,
matched: Vec::new(),
warnings: Vec::new(),
}
}
}