AccessPolicy

Struct AccessPolicy 

Source
pub struct AccessPolicy<R, G>
where R: AccessHierarchy + Eq + Display, G: Eq,
{ /* private fields */ }
Expand description

Domain object representing access requirements for a protected resource.

This captures the business rules about what roles, groups, or permissions are required to access a particular resource or route. Access is granted if the user meets ANY of the specified requirements (OR logic).

Implementations§

Source§

impl<R, G> AccessPolicy<R, G>
where R: AccessHierarchy + Eq + Display, G: Eq,

Source

pub fn deny_all() -> Self

Creates a new access policy with no requirements (denies all access).

This is the secure default - no access is granted unless explicitly configured through the builder methods.

Source

pub fn require_role(role: R) -> Self

Creates a policy that allows access for users with the specified role.

Use this when you need exact role matching without hierarchy. For scenarios where supervisor roles should also have access, use require_role_or_supervisor().

§Example
use axum_gate::authz::AccessPolicy;
use axum_gate::prelude::{Role, Group};

let policy: AccessPolicy<Role, Group> = AccessPolicy::require_role(Role::Admin);
Source

pub fn require_role_or_supervisor(role: R) -> Self

Creates a policy that allows access for users with the specified role or any supervisor role.

Use this when you want hierarchical access control where higher-level roles automatically inherit permissions from lower-level roles. This is ideal for organizational structures where managers should have access to employee resources.

This leverages the role hierarchy defined by the AccessHierarchy trait.

§Example
use axum_gate::authz::AccessPolicy;
use axum_gate::prelude::{Role, Group};

// Allows Moderator role and Admin role (if Admin supervises Moderator)
let policy: AccessPolicy<Role, Group> = AccessPolicy::require_role_or_supervisor(Role::Moderator);
Source

pub fn require_group(group: G) -> Self

Creates a policy that allows access for users in the specified group.

Use this for team-based or department-based access control. Groups are ideal for cross-cutting concerns that don’t fit hierarchical role structures, such as project teams, geographical regions, or temporary access grants.

§Example
use axum_gate::authz::AccessPolicy;
use axum_gate::prelude::{Role, Group};

let policy = AccessPolicy::<Role, Group>::require_group(Group::new("engineering"));
Source

pub fn require_permission<P: Into<PermissionId>>(permission: P) -> Self

Creates a policy that allows access for users with the specified permission.

§Example
use axum_gate::authz::AccessPolicy;
use axum_gate::permissions::PermissionId;
use axum_gate::prelude::{Role, Group};

// Using a permission name (hashed deterministically to 64-bit ID)
let policy: AccessPolicy<Role, Group> =
    AccessPolicy::require_permission(PermissionId::from("read:api"));

// Or directly from &str via Into<PermissionId>
let policy2: AccessPolicy<Role, Group> =
    AccessPolicy::require_permission("write:api");
Source

pub fn or_require_role(self, role: R) -> Self

Adds an additional role requirement to this policy.

Access will be granted if the user has ANY of the configured roles.

Source

pub fn or_require_role_or_supervisor(self, role: R) -> Self

Adds an additional role or supervisor requirement to this policy.

Access will be granted if the user has the specified role or supervises it.

Source

pub fn or_require_group(self, group: G) -> Self

Adds an additional group requirement to this policy.

Access will be granted if the user is in ANY of the configured groups.

Source

pub fn or_require_permission<P: Into<PermissionId>>(self, permission: P) -> Self

Adds an additional permission requirement to this policy.

Access will be granted if the user has ANY of the configured permissions.

Source

pub fn or_require_permissions<P: Into<PermissionId>>( self, permissions: Vec<P>, ) -> Self

Adds multiple additional permission requirements to this policy.

Access will be granted if the user has ANY of the configured permissions.

Source

pub fn role_requirements(&self) -> &[AccessScope<R>]

Returns the role requirements for this policy.

Source

pub fn group_requirements(&self) -> &[G]

Returns the group requirements for this policy.

Source

pub fn permission_requirements(&self) -> &Permissions

Returns the permission requirements for this policy.

Source

pub fn denies_all(&self) -> bool

Returns true if this policy has no requirements (denies all access).

This is useful for validation - a policy that denies all access might indicate a configuration error.

Source

pub fn has_requirements(&self) -> bool

Returns true if this policy has at least one requirement configured.

This is useful for validating that a policy is properly configured with some access requirements rather than being completely empty.

Source

pub fn into_components(self) -> (Vec<AccessScope<R>>, Vec<G>, Permissions)

Converts this policy into the components needed by the authorization service.

This is primarily used internally when bridging to the authorization service.

Trait Implementations§

Source§

impl<R, G> Clone for AccessPolicy<R, G>
where R: AccessHierarchy + Eq + Display + Clone, G: Eq + Clone,

Source§

fn clone(&self) -> AccessPolicy<R, G>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<R, G> Debug for AccessPolicy<R, G>
where R: AccessHierarchy + Eq + Display + Debug, G: Eq + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<R, G> Freeze for AccessPolicy<R, G>

§

impl<R, G> RefUnwindSafe for AccessPolicy<R, G>

§

impl<R, G> Send for AccessPolicy<R, G>
where G: Send, R: Send,

§

impl<R, G> Sync for AccessPolicy<R, G>
where G: Sync, R: Sync,

§

impl<R, G> Unpin for AccessPolicy<R, G>
where G: Unpin, R: Unpin,

§

impl<R, G> UnwindSafe for AccessPolicy<R, G>
where G: UnwindSafe, R: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<G1, G2> Within<G2> for G1
where G2: Contains<G1>,

Source§

fn is_within(&self, b: &G2) -> bool

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,