1use alloc::collections::BTreeSet;
4use alloc::string::{String, ToString};
5use alloc::vec::Vec;
6
7use coqui_access::guatiao::library::ProviderError;
8
9use crate::check::{Context, Decision, Level, level};
10use crate::entry::{Entry, ItemAcl, Mode};
11use crate::error::AclError;
12use crate::options::OnMissing;
13use crate::rights::Rights;
14
15pub trait AclStore {
22 fn acl(&self, item: &str) -> Result<Option<ItemAcl>, ProviderError>;
28
29 fn parent(&self, item: &str) -> Result<Option<String>, ProviderError>;
35
36 fn check(
54 &self,
55 item: &str,
56 context: &Context<'_>,
57 wanted: &Rights,
58 ) -> Result<Decision, AclError> {
59 check_item(self, item, context, wanted)
60 }
61}
62
63fn check_item<S: AclStore + ?Sized>(
64 store: &S,
65 item: &str,
66 context: &Context<'_>,
67 wanted: &Rights,
68) -> Result<Decision, AclError> {
69 let context = context.clone().item(item);
70 let own = store.acl(item)?;
71 if let Some(own) = &own
72 && own.acl.mode != Mode::MostSpecific
73 {
74 return own.check(&context, wanted);
75 }
76 let limit = context.options.limits.depth;
77 let mut chain = Vec::from([(item.to_string(), own)]);
78 let mut seen = BTreeSet::from([item.to_string()]);
79 while let Some(parent) = store.parent(&chain[chain.len() - 1].0)? {
80 if !seen.insert(parent.clone()) {
81 return Err(AclError::Cycle);
82 }
83 if limit.is_some_and(|max| chain.len() >= max) {
84 return Err(AclError::TooDeep);
85 }
86 let acl = store.acl(&parent)?;
87 chain.push((parent, acl));
88 }
89 if chain.iter().all(|(_, acl)| acl.is_none()) {
90 return Ok(match context.options.on_missing {
91 OnMissing::Deny => Decision::nothing(wanted),
92 OnMissing::AllowAll => {
93 Decision::from_sets(wanted, &context.schema.all() | wanted, Rights::new(), None)
94 }
95 });
96 }
97 let mut in_force: Option<Level> = None;
98 for (depth, (_, acl)) in chain.iter().enumerate().rev() {
99 let Some(acl) = acl else { continue };
100 if acl.acl.mode != Mode::MostSpecific {
101 return Err(AclError::ModeMismatch);
102 }
103 let compiled = acl.acl.compile(context.schema, &context.options)?;
104 let here = context.for_item(acl);
105 let at_item = depth == 0;
106 let applicable: Vec<_> = acl
107 .acl
108 .entries
109 .iter()
110 .zip(&compiled)
111 .enumerate()
112 .filter(|(_, (entry, _))| applies_at(entry, at_item) && here.applies(entry))
113 .map(|(at, (entry, compiled))| (at, entry, compiled))
114 .collect();
115 if let Some(found) = level(&applicable) {
116 in_force = Some(found);
117 }
118 }
119 let item_acl = chain[0].1.as_ref();
120 let owner = item_acl.map_or_else(Rights::new, |acl| {
121 context.for_item(acl).owner_grant(&acl.acl)
122 });
123 Ok(match in_force {
124 Some(Level {
125 cancel: Some(at), ..
126 }) => Decision::cancelled(context.schema, wanted, at),
127 Some(found) => Decision::from_sets(
128 wanted,
129 &found.rights | &owner,
130 Rights::new(),
131 found.deciding,
132 ),
133 None => Decision::from_sets(wanted, owner, Rights::new(), None),
134 })
135}
136
137fn applies_at(entry: &Entry, at_item: bool) -> bool {
139 if at_item {
140 !entry.inherit.inherit_only
141 } else {
142 entry.inherit.containers
143 }
144}