Skip to main content

AbacPolicy

Struct AbacPolicy 

Source
pub struct AbacPolicy<S, R, A, C, F> { /* private fields */ }
Expand description

An attribute-based access control policy. Define a condition closure that determines whether a subject is allowed to perform an action on a resource, given the additional context. If it returns true, access is granted. Otherwise, access is denied.

§Example

We define simple types for a user, a resource, an action, and a context. We then create a built-in ABAC policy that grants access if the user “owns” a resource as determined by the resource’s owner_id.


// Define our core types.
#[derive(Debug, Clone)]
struct User {
    id: Uuid,
}

#[derive(Debug, Clone)]
struct Resource {
    owner_id: Uuid,
}

#[derive(Debug, Clone)]
struct Action;

#[derive(Debug, Clone)]
struct EmptyContext;

// Create an ABAC policy.
// This policy grants access if the user's ID matches the resource's owner.
let abac_policy = AbacPolicy::new(
    |user: &User, resource: &Resource, _action: &Action, _context: &EmptyContext| {
        user.id == resource.owner_id
    },
);

// Create a PermissionChecker and add the ABAC policy.
let mut checker = PermissionChecker::<User, Resource, Action, EmptyContext>::new();
checker.add_policy(abac_policy);

// Create a sample user
let user = User {
    id: Uuid::new_v4(),
};

// Create a resource owned by the user, and one that is not
let owned_resource = Resource { owner_id: user.id };
let other_resource = Resource { owner_id: Uuid::new_v4() };
let context = EmptyContext;

// This check should succeed because the user is the owner:
assert!(checker.evaluate_access(&user, &Action, &owned_resource, &context).await.is_granted());

// This check should fail because the user is not the owner:
assert!(!checker.evaluate_access(&user, &Action, &other_resource, &context).await.is_granted());

Implementations§

Source§

impl<S, R, A, C, F> AbacPolicy<S, R, A, C, F>

Source

pub fn new(condition: F) -> Self

Creates a new ABAC policy from a condition closure.

Examples found in repository?
examples/axum.rs (lines 309-319)
306fn payment_refund_policy() -> Box<dyn Policy<User, Resource, Action, RequestContext>> {
307    // Alternatively, we can just do a single condition for finance_manager,
308    // or combine them. Here let's say "finance_manager" or "refund_specialist".
309    Box::new(AbacPolicy::new(
310        |user: &User, resource: &Resource, action: &Action, _ctx: &RequestContext| {
311            if let Resource::Payment(_) = resource {
312                if matches!(action, Action::RefundPayment) {
313                    return user.roles.contains(&"finance_manager".into())
314                        || user.roles.contains(&"refund_specialist".into());
315                }
316            }
317            false
318        },
319    ))
320}

Trait Implementations§

Source§

impl<S, R, A, C, F> Policy<S, R, A, C> for AbacPolicy<S, R, A, C, F>
where S: Sync + Send, R: Sync + Send, A: Sync + Send, C: Sync + Send, F: Fn(&S, &R, &A, &C) -> bool + Sync + Send,

Source§

fn evaluate_access<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>( &'life0 self, subject: &'life1 S, action: &'life2 A, resource: &'life3 R, context: &'life4 C, ) -> Pin<Box<dyn Future<Output = PolicyEvalResult> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait, 'life4: 'async_trait,

Evaluates whether access should be granted. Read more
Source§

fn policy_type(&self) -> String

Policy name for debugging
Source§

fn security_rule(&self) -> SecurityRuleMetadata

Metadata describing the security rule that backs this policy. Read more

Auto Trait Implementations§

§

impl<S, R, A, C, F> Freeze for AbacPolicy<S, R, A, C, F>
where F: Freeze,

§

impl<S, R, A, C, F> RefUnwindSafe for AbacPolicy<S, R, A, C, F>

§

impl<S, R, A, C, F> Send for AbacPolicy<S, R, A, C, F>
where F: Send, S: Send, R: Send, A: Send, C: Send,

§

impl<S, R, A, C, F> Sync for AbacPolicy<S, R, A, C, F>
where F: Sync, S: Sync, R: Sync, A: Sync, C: Sync,

§

impl<S, R, A, C, F> Unpin for AbacPolicy<S, R, A, C, F>
where F: Unpin, S: Unpin, R: Unpin, A: Unpin, C: Unpin,

§

impl<S, R, A, C, F> UnsafeUnpin for AbacPolicy<S, R, A, C, F>
where F: UnsafeUnpin,

§

impl<S, R, A, C, F> UnwindSafe for AbacPolicy<S, R, A, C, F>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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<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