pub struct Gate<A: Actor> { /* private fields */ }Expand description
A registry of authorization abilities.
Checks are synchronous. An authorization decision that needs a database round-trip is a sign the subject should have been loaded already — the controller has the record in hand by the time it authorizes, and re-fetching it inside the check would double every query.
Implementations§
Source§impl<A: Actor> Gate<A>
impl<A: Actor> Gate<A>
Sourcepub fn define<S, F>(self, ability: impl Into<String>, check: F) -> Self
pub fn define<S, F>(self, ability: impl Into<String>, check: F) -> Self
Define an ability taking the actor and an optional subject.
Sourcepub fn define_simple<F>(self, ability: impl Into<String>, check: F) -> Self
pub fn define_simple<F>(self, ability: impl Into<String>, check: F) -> Self
Define an ability that ignores its subject.
Sourcepub fn before<F>(self, check: F) -> Self
pub fn before<F>(self, check: F) -> Self
Register a check that runs before every ability.
Returning Some(true) grants regardless of the ability — the “an admin
may do anything” rule, in one place instead of at the top of every
check. None defers.
Sourcepub fn allows<S: 'static>(
&self,
ability: &str,
actor: &A,
subject: Option<&S>,
) -> bool
pub fn allows<S: 'static>( &self, ability: &str, actor: &A, subject: Option<&S>, ) -> bool
Whether actor may perform ability on subject.
An undefined ability is denied. Defaulting to “allow” would mean a typo in an ability name silently opens a hole.
Sourcepub fn denies<S: 'static>(
&self,
ability: &str,
actor: &A,
subject: Option<&S>,
) -> bool
pub fn denies<S: 'static>( &self, ability: &str, actor: &A, subject: Option<&S>, ) -> bool
The inverse of allows.
Sourcepub fn allows_any(&self, ability: &str, actor: &A) -> bool
pub fn allows_any(&self, ability: &str, actor: &A) -> bool
Whether actor may perform ability, with no subject.
Grant, or fail with a 403.