pub struct AuthorizationAspect { /* private fields */ }Expand description
Role-based access control aspect.
Enforces authorization checks before function execution based on required roles or permissions.
§Example
ⓘ
use aspect_std::AuthorizationAspect;
use aspect_macros::aspect;
// Require "admin" role
let auth = AuthorizationAspect::require_role("admin", || {
get_current_user_roles()
});
#[aspect(auth)]
fn delete_user(user_id: u64) -> Result<(), String> {
// Only admins can delete users
Ok(())
}Implementations§
Source§impl AuthorizationAspect
impl AuthorizationAspect
Sourcepub fn require_role<F>(role: &str, role_provider: F) -> Self
pub fn require_role<F>(role: &str, role_provider: F) -> Self
Sourcepub fn require_roles<F>(
roles: &[&str],
role_provider: F,
mode: AuthMode,
) -> Self
pub fn require_roles<F>( roles: &[&str], role_provider: F, mode: AuthMode, ) -> Self
Create an authorization aspect that requires multiple roles.
§Arguments
roles- The required rolesrole_provider- Function that returns the current user’s rolesmode- Whether to require ALL or ANY of the roles
§Example
ⓘ
let auth = AuthorizationAspect::require_roles(
&["admin", "moderator"],
|| get_current_roles(),
AuthMode::RequireAny
);Trait Implementations§
Source§impl Aspect for AuthorizationAspect
impl Aspect for AuthorizationAspect
Source§fn after(&self, _ctx: &JoinPoint, _result: &(dyn Any + 'static))
fn after(&self, _ctx: &JoinPoint, _result: &(dyn Any + 'static))
Advice executed after the target function completes successfully. Read more
Source§fn after_error(&self, _ctx: &JoinPoint, _error: &AspectError)
fn after_error(&self, _ctx: &JoinPoint, _error: &AspectError)
Advice executed when the target function encounters an error. Read more
Source§fn around(
&self,
pjp: ProceedingJoinPoint<'_>,
) -> Result<Box<dyn Any>, AspectError>
fn around( &self, pjp: ProceedingJoinPoint<'_>, ) -> Result<Box<dyn Any>, AspectError>
Advice that wraps the entire target function execution. Read more
Source§impl Clone for AuthorizationAspect
impl Clone for AuthorizationAspect
Source§fn clone(&self) -> AuthorizationAspect
fn clone(&self) -> AuthorizationAspect
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for AuthorizationAspect
impl !RefUnwindSafe for AuthorizationAspect
impl Send for AuthorizationAspect
impl Sync for AuthorizationAspect
impl Unpin for AuthorizationAspect
impl UnsafeUnpin for AuthorizationAspect
impl !UnwindSafe for AuthorizationAspect
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more