Skip to main content

openauth_plugins/access/
types.rs

1use std::collections::{BTreeMap, BTreeSet};
2
3/// Connector used to combine resource or action checks.
4#[derive(Clone, Copy, Debug, Eq, PartialEq)]
5pub enum Connector {
6    /// Every requested item must be allowed.
7    And,
8    /// At least one requested item must be allowed.
9    Or,
10}
11
12/// Resource-to-actions access statements.
13pub type Statements = BTreeMap<String, BTreeSet<String>>;
14
15/// Resource-to-action request map.
16pub type AccessRequest = BTreeMap<String, ResourceRequest>;
17
18/// Permission request for a single resource.
19#[derive(Clone, Debug, Eq, PartialEq)]
20pub struct ResourceRequest {
21    pub(crate) actions: BTreeSet<String>,
22    pub(crate) connector: Connector,
23}
24
25impl ResourceRequest {
26    /// Require all listed actions for the resource.
27    pub fn all<I, S>(actions: I) -> Self
28    where
29        I: IntoIterator<Item = S>,
30        S: Into<String>,
31    {
32        Self {
33            actions: actions.into_iter().map(Into::into).collect(),
34            connector: Connector::And,
35        }
36    }
37
38    /// Require any listed action for the resource.
39    pub fn any<I, S>(actions: I) -> Self
40    where
41        I: IntoIterator<Item = S>,
42        S: Into<String>,
43    {
44        Self {
45            actions: actions.into_iter().map(Into::into).collect(),
46            connector: Connector::Or,
47        }
48    }
49
50    pub(crate) fn actions(&self) -> &BTreeSet<String> {
51        &self.actions
52    }
53
54    pub(crate) fn connector(&self) -> Connector {
55        self.connector
56    }
57}
58
59/// Access role with its allowed statements.
60#[derive(Clone, Debug, Eq, PartialEq)]
61pub struct Role {
62    pub(crate) statements: Statements,
63}
64
65impl Role {
66    pub(crate) fn new(statements: Statements) -> Self {
67        Self { statements }
68    }
69
70    /// Return the role's allowed statements.
71    pub fn statements(&self) -> &Statements {
72        &self.statements
73    }
74}