base/interface/permission/
set.rs1use std::sync::Arc;
4
5use super::{Grant, GrantError, PathDenials, PathGuard, PermissionGrants, PermissionPolicy};
6
7#[derive(Clone)]
13#[non_exhaustive]
14pub struct PermissionSet {
15 pub policy: Arc<dyn PermissionPolicy>,
16 pub grants: Option<Arc<dyn PermissionGrants>>,
17 pub path_denials: Option<Arc<dyn PathDenials>>,
18 pub paths: Option<Arc<dyn PathGuard>>,
19}
20
21impl PermissionSet {
22 pub fn of<P: PermissionPolicy + PermissionGrants + PathDenials + 'static>(p: Arc<P>) -> Self {
27 Self {
28 policy: p.clone(),
29 grants: Some(p.clone()),
30 path_denials: Some(p),
31 paths: None,
32 }
33 }
34
35 pub fn without_grants(policy: Arc<dyn PermissionPolicy>) -> Self {
36 Self {
37 policy,
38 grants: None,
39 path_denials: None,
40 paths: None,
41 }
42 }
43
44 pub fn with_paths(mut self, paths: Arc<dyn PathGuard>) -> Self {
45 self.paths = Some(paths);
46 self
47 }
48
49 pub fn wrapping(
52 &self,
53 policy: Arc<dyn PermissionPolicy>,
54 path_denials: Option<Arc<dyn PathDenials>>,
55 ) -> Self {
56 Self {
57 policy,
58 path_denials,
59 ..self.clone()
60 }
61 }
62
63 pub fn admits(&self, grant: &Grant) -> Result<(), GrantError> {
66 match (&grant.narrow_only, &self.paths) {
67 (None, _) => Ok(()),
68 (Some(_), Some(paths)) => paths.admits(grant),
69 (Some(_), None) => Err(GrantError::too_broad(grant)),
70 }
71 }
72}