Skip to main content

themis/
decision.rs

1use serde::{Deserialize, Serialize};
2
3use crate::grant::Effect;
4use crate::resource::Resource;
5use crate::subject::Subject;
6
7/// Question put to the engine: can `subject` `permission` on `resource`?
8#[derive(Debug, Clone, Copy)]
9pub struct Request<'a> {
10    pub subject: &'a Subject,
11    pub permission: &'a str,
12    pub resource: &'a Resource,
13}
14
15impl<'a> Request<'a> {
16    pub fn new(subject: &'a Subject, permission: &'a str, resource: &'a Resource) -> Self {
17        Self {
18            subject,
19            permission,
20            resource,
21        }
22    }
23}
24
25/// Why the decision came out the way it did.
26#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
27#[serde(tag = "kind", rename_all = "snake_case")]
28pub enum Reason {
29    /// An override decided before the grants.
30    Override { name: String },
31    /// A grant decided, chosen by the strategy.
32    Grant {
33        grant_id: String,
34        permission: String,
35        rank: usize,
36        strategy: String,
37    },
38    /// No grant applied: default-deny.
39    NoApplicableGrant,
40    /// The requested permission is not in the catalog.
41    UnknownPermission,
42}
43
44#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
45pub struct Decision {
46    pub effect: Effect,
47    pub reason: Reason,
48    /// Ids of all the grants that applied, in source order.
49    #[serde(default, skip_serializing_if = "Vec::is_empty")]
50    pub matched: Vec<String>,
51    /// Problems that did not prevent the decision (condition with a missing attribute, ReBAC without a store).
52    #[serde(default, skip_serializing_if = "Vec::is_empty")]
53    pub warnings: Vec<String>,
54}
55
56impl Decision {
57    pub fn is_allowed(&self) -> bool {
58        self.effect == Effect::Allow
59    }
60
61    pub(crate) fn deny(reason: Reason) -> Self {
62        Self {
63            effect: Effect::Deny,
64            reason,
65            matched: Vec::new(),
66            warnings: Vec::new(),
67        }
68    }
69}