use std::any::Any;
use super::{AuthorizationError, AuthorizationResponse, Gate, GateError, IntoAuthorizationResponse};
pub struct UserGate<User> {
gate: Gate<User>,
user: Option<User>,
}
impl<User> UserGate<User>
where
User: Clone + Send + Sync + 'static,
{
pub(super) fn new(gate: Gate<User>, user: Option<User>) -> Self {
Self { gate, user }
}
pub async fn inspect<Context>(
&self,
ability: impl AsRef<str>,
context: &Context,
) -> Result<AuthorizationResponse, GateError>
where
Context: Any + Clone + Send + Sync + 'static,
{
self.gate.inspect(self.user.clone(), ability.as_ref(), context).await
}
pub async fn allows<Context>(&self, ability: impl AsRef<str>, context: &Context) -> Result<bool, GateError>
where
Context: Any + Clone + Send + Sync + 'static,
{
Ok(self.inspect(ability, context).await?.allowed())
}
pub async fn denies<Context>(&self, ability: impl AsRef<str>, context: &Context) -> Result<bool, GateError>
where
Context: Any + Clone + Send + Sync + 'static,
{
Ok(!self.allows(ability, context).await?)
}
pub async fn authorize<Context>(
&self,
ability: impl AsRef<str>,
context: &Context,
) -> Result<AuthorizationResponse, AuthorizationError>
where
Context: Any + Clone + Send + Sync + 'static,
{
self.inspect(ability, context).await?.authorize()
}
pub async fn check<Ability, Abilities, Context>(
&self,
abilities: Abilities,
context: &Context,
) -> Result<bool, GateError>
where
Ability: AsRef<str>,
Abilities: IntoIterator<Item = Ability>,
Context: Any + Clone + Send + Sync + 'static,
{
for ability in abilities {
if !self.allows(ability, context).await? {
return Ok(false);
}
}
Ok(true)
}
pub async fn any<Ability, Abilities, Context>(
&self,
abilities: Abilities,
context: &Context,
) -> Result<bool, GateError>
where
Ability: AsRef<str>,
Abilities: IntoIterator<Item = Ability>,
Context: Any + Clone + Send + Sync + 'static,
{
for ability in abilities {
if self.allows(ability, context).await? {
return Ok(true);
}
}
Ok(false)
}
pub async fn none<Ability, Abilities, Context>(
&self,
abilities: Abilities,
context: &Context,
) -> Result<bool, GateError>
where
Ability: AsRef<str>,
Abilities: IntoIterator<Item = Ability>,
Context: Any + Clone + Send + Sync + 'static,
{
Ok(!self.any(abilities, context).await?)
}
pub fn allow_if<Outcome>(&self, condition: Outcome) -> Result<AuthorizationResponse, AuthorizationError>
where
Outcome: IntoAuthorizationResponse,
{
condition.into_on_demand_response(true).authorize()
}
pub fn allow_if_with_user<Outcome>(
&self,
check: impl FnOnce(&User) -> Outcome,
) -> Result<AuthorizationResponse, AuthorizationError>
where
Outcome: IntoAuthorizationResponse,
{
self.user
.as_ref()
.map(check)
.map_or_else(AuthorizationResponse::deny, |response| response.into_on_demand_response(true))
.authorize()
}
pub fn allow_if_for_guests<Outcome>(
&self,
check: impl FnOnce(Option<&User>) -> Outcome,
) -> Result<AuthorizationResponse, AuthorizationError>
where
Outcome: IntoAuthorizationResponse,
{
check(self.user.as_ref()).into_on_demand_response(true).authorize()
}
pub fn deny_if<Outcome>(&self, condition: Outcome) -> Result<AuthorizationResponse, AuthorizationError>
where
Outcome: IntoAuthorizationResponse,
{
condition.into_on_demand_response(false).authorize()
}
pub fn deny_if_with_user<Outcome>(
&self,
check: impl FnOnce(&User) -> Outcome,
) -> Result<AuthorizationResponse, AuthorizationError>
where
Outcome: IntoAuthorizationResponse,
{
self.user
.as_ref()
.map(check)
.map_or_else(AuthorizationResponse::deny, |response| response.into_on_demand_response(false))
.authorize()
}
pub fn deny_if_for_guests<Outcome>(
&self,
check: impl FnOnce(Option<&User>) -> Outcome,
) -> Result<AuthorizationResponse, AuthorizationError>
where
Outcome: IntoAuthorizationResponse,
{
check(self.user.as_ref()).into_on_demand_response(false).authorize()
}
}