Skip to main content

aurelin_framework/authorization/
user_gate.rs

1use std::any::Any;
2
3use super::{AuthorizationError, AuthorizationResponse, Gate, GateError, IntoAuthorizationResponse};
4
5/// A request-oriented view of a gate registry for one user or guest.
6pub struct UserGate<User> {
7    gate: Gate<User>,
8    user: Option<User>,
9}
10
11impl<User> UserGate<User>
12where
13    User: Clone + Send + Sync + 'static,
14{
15    pub(super) fn new(gate: Gate<User>, user: Option<User>) -> Self {
16        Self { gate, user }
17    }
18
19    /// Inspect an ability's complete response.
20    pub async fn inspect<Context>(
21        &self,
22        ability: impl AsRef<str>,
23        context: &Context,
24    ) -> Result<AuthorizationResponse, GateError>
25    where
26        Context: Any + Clone + Send + Sync + 'static,
27    {
28        self.gate.inspect(self.user.clone(), ability.as_ref(), context).await
29    }
30
31    /// Determine whether an ability is allowed.
32    pub async fn allows<Context>(&self, ability: impl AsRef<str>, context: &Context) -> Result<bool, GateError>
33    where
34        Context: Any + Clone + Send + Sync + 'static,
35    {
36        Ok(self.inspect(ability, context).await?.allowed())
37    }
38
39    /// Determine whether an ability is denied.
40    pub async fn denies<Context>(&self, ability: impl AsRef<str>, context: &Context) -> Result<bool, GateError>
41    where
42        Context: Any + Clone + Send + Sync + 'static,
43    {
44        Ok(!self.allows(ability, context).await?)
45    }
46
47    /// Authorize an ability or return its denial.
48    pub async fn authorize<Context>(
49        &self,
50        ability: impl AsRef<str>,
51        context: &Context,
52    ) -> Result<AuthorizationResponse, AuthorizationError>
53    where
54        Context: Any + Clone + Send + Sync + 'static,
55    {
56        self.inspect(ability, context).await?.authorize()
57    }
58
59    /// Determine whether every ability is allowed for the same context.
60    pub async fn check<Ability, Abilities, Context>(
61        &self,
62        abilities: Abilities,
63        context: &Context,
64    ) -> Result<bool, GateError>
65    where
66        Ability: AsRef<str>,
67        Abilities: IntoIterator<Item = Ability>,
68        Context: Any + Clone + Send + Sync + 'static,
69    {
70        for ability in abilities {
71            if !self.allows(ability, context).await? {
72                return Ok(false);
73            }
74        }
75        Ok(true)
76    }
77
78    /// Determine whether any ability is allowed for the same context.
79    pub async fn any<Ability, Abilities, Context>(
80        &self,
81        abilities: Abilities,
82        context: &Context,
83    ) -> Result<bool, GateError>
84    where
85        Ability: AsRef<str>,
86        Abilities: IntoIterator<Item = Ability>,
87        Context: Any + Clone + Send + Sync + 'static,
88    {
89        for ability in abilities {
90            if self.allows(ability, context).await? {
91                return Ok(true);
92            }
93        }
94        Ok(false)
95    }
96
97    /// Determine whether no ability is allowed for the same context.
98    pub async fn none<Ability, Abilities, Context>(
99        &self,
100        abilities: Abilities,
101        context: &Context,
102    ) -> Result<bool, GateError>
103    where
104        Ability: AsRef<str>,
105        Abilities: IntoIterator<Item = Ability>,
106        Context: Any + Clone + Send + Sync + 'static,
107    {
108        Ok(!self.any(abilities, context).await?)
109    }
110
111    /// Perform an inline allow check without running registered hooks.
112    pub fn allow_if<Outcome>(&self, condition: Outcome) -> Result<AuthorizationResponse, AuthorizationError>
113    where
114        Outcome: IntoAuthorizationResponse,
115    {
116        condition.into_on_demand_response(true).authorize()
117    }
118
119    /// Perform an authenticated-user inline allow check without registered hooks.
120    pub fn allow_if_with_user<Outcome>(
121        &self,
122        check: impl FnOnce(&User) -> Outcome,
123    ) -> Result<AuthorizationResponse, AuthorizationError>
124    where
125        Outcome: IntoAuthorizationResponse,
126    {
127        self.user
128            .as_ref()
129            .map(check)
130            .map_or_else(AuthorizationResponse::deny, |response| response.into_on_demand_response(true))
131            .authorize()
132    }
133
134    /// Perform a guest-aware inline allow check without registered hooks.
135    pub fn allow_if_for_guests<Outcome>(
136        &self,
137        check: impl FnOnce(Option<&User>) -> Outcome,
138    ) -> Result<AuthorizationResponse, AuthorizationError>
139    where
140        Outcome: IntoAuthorizationResponse,
141    {
142        check(self.user.as_ref()).into_on_demand_response(true).authorize()
143    }
144
145    /// Perform an inline deny check without running registered hooks.
146    pub fn deny_if<Outcome>(&self, condition: Outcome) -> Result<AuthorizationResponse, AuthorizationError>
147    where
148        Outcome: IntoAuthorizationResponse,
149    {
150        condition.into_on_demand_response(false).authorize()
151    }
152
153    /// Perform an authenticated-user inline deny check without registered hooks.
154    pub fn deny_if_with_user<Outcome>(
155        &self,
156        check: impl FnOnce(&User) -> Outcome,
157    ) -> Result<AuthorizationResponse, AuthorizationError>
158    where
159        Outcome: IntoAuthorizationResponse,
160    {
161        self.user
162            .as_ref()
163            .map(check)
164            .map_or_else(AuthorizationResponse::deny, |response| response.into_on_demand_response(false))
165            .authorize()
166    }
167
168    /// Perform a guest-aware inline deny check without registered hooks.
169    pub fn deny_if_for_guests<Outcome>(
170        &self,
171        check: impl FnOnce(Option<&User>) -> Outcome,
172    ) -> Result<AuthorizationResponse, AuthorizationError>
173    where
174        Outcome: IntoAuthorizationResponse,
175    {
176        check(self.user.as_ref()).into_on_demand_response(false).authorize()
177    }
178}