AsyncRoleSystem

Struct AsyncRoleSystem 

Source
pub struct AsyncRoleSystem<S>
where S: Storage + Send + Sync,
{ /* private fields */ }
Expand description

Async wrapper around the role system for non-blocking operations.

Implementations§

Source§

impl<S> AsyncRoleSystem<S>
where S: Storage + Send + Sync,

Source

pub fn new(role_system: RoleSystem<S>) -> AsyncRoleSystem<S>

Create a new async role system.

Source

pub async fn register_role(&self, role: Role) -> Result<(), Error>

Register a new role in the system.

Source

pub async fn get_role(&self, name: &str) -> Result<Option<Role>, Error>

Get a role by name.

Source

pub async fn add_role_inheritance( &self, child: &str, parent: &str, ) -> Result<(), Error>

Add role inheritance (child inherits from parent).

Source

pub async fn remove_role_inheritance( &self, child: &str, parent: &str, ) -> Result<(), Error>

Remove role inheritance.

Source

pub async fn assign_role( &self, subject: &Subject, role_name: &str, ) -> Result<(), Error>

Assign a role to a subject.

Source

pub async fn remove_role( &self, subject: &Subject, role_name: &str, ) -> Result<(), Error>

Remove a role from a subject.

Source

pub async fn elevate_role( &self, subject: &Subject, role_name: &str, duration: Option<Duration>, ) -> Result<(), Error>

Temporarily elevate a subject’s role.

Source

pub async fn check_permission( &self, subject: &Subject, action: &str, resource: &Resource, ) -> Result<bool, Error>

Check if a subject has a specific permission on a resource.

Source

pub async fn check_permission_with_context( &self, subject: &Subject, action: &str, resource: &Resource, context: &HashMap<String, String>, ) -> Result<bool, Error>

Check permission with additional context.

Source

pub async fn get_subject_roles( &self, subject: &Subject, ) -> Result<HashSet<String>, Error>

Get all roles assigned to a subject.

Source

pub async fn batch_check_permissions( &self, subject: &Subject, checks: &[(String, Resource)], ) -> Result<Vec<(String, Resource, bool)>, Error>

Batch check multiple permissions for a subject.

Source

pub async fn atomic_role_operations<F, R>( &self, operations: F, ) -> Result<R, Error>
where F: FnOnce(&mut RoleSystem<S>) -> Result<R, Error> + Send,

Perform multiple role operations atomically.

Source

pub async fn with_read_access<F, R>(&self, operation: F) -> R
where F: FnOnce(&RoleSystem<S>) -> R + Send,

Get a read-only reference to the role system for complex queries.

Source

pub async fn get_hierarchy_tree( &self, config: Option<HierarchyConfig>, ) -> Result<RoleHierarchyTree, Error>

Get the complete hierarchy tree structure.

This method provides a structured view of the entire role hierarchy, useful for visualization, API responses, and external system integration.

§Arguments
  • config - Optional hierarchy configuration. If None, uses default settings.
§Returns

A RoleHierarchyTree containing the complete hierarchy structure with metadata.

§Example
let storage = MemoryStorage::new();
let role_sys = RoleSystem::with_storage(storage, RoleSystemConfig::default());
let role_system = AsyncRoleSystem::new(role_sys);

let config = HierarchyConfigBuilder::new()
    .enable_hierarchy_access(true)
    .max_depth(10)
    .build();

let tree = role_system.get_hierarchy_tree(Some(config)).await?;
println!("Total roles: {}, Max depth: {}", tree.total_roles, tree.max_depth);
Source

pub async fn get_role_ancestors( &self, role_id: &str, _include_inherited: bool, ) -> Result<Vec<String>, Error>

Get all parent roles for a given role (ancestors).

This method returns all roles that the specified role inherits from, including both direct parents and inherited ancestors.

§Arguments
  • role_id - The ID of the role to get ancestors for
  • _include_inherited - Whether to include inherited (indirect) parents
§Returns

A vector of role IDs representing all ancestor roles.

§Example
let ancestors = role_system.get_role_ancestors("junior_dev", true).await?;
for ancestor_id in ancestors {
    println!("Inherits from: {}", ancestor_id);
}
Source

pub async fn get_role_descendants( &self, role_id: &str, _include_inherited: bool, ) -> Result<Vec<String>, Error>

Get all child roles for a given role (descendants).

This method returns all roles that inherit from the specified role, including both direct children and inherited descendants.

§Arguments
  • role_id - The ID of the role to get descendants for
  • _include_inherited - Whether to include inherited (indirect) children
§Returns

A vector of role IDs representing all descendant roles.

§Example
let storage = MemoryStorage::new();
let role_sys = RoleSystem::with_storage(storage, RoleSystemConfig::default());
let role_system = AsyncRoleSystem::new(role_sys);
let descendants = role_system.get_role_descendants("team_lead", true).await?;
for descendant_id in descendants {
    println!("Has child: {}", descendant_id);
}
Source

pub async fn get_role_siblings( &self, role_id: &str, ) -> Result<Vec<String>, Error>

Get all sibling roles for a given role.

Sibling roles are roles that share the same parent in the hierarchy.

§Arguments
  • role_id - The ID of the role to get siblings for
§Returns

A vector of role IDs representing all sibling roles.

§Example
let storage = MemoryStorage::new();
let role_sys = RoleSystem::with_storage(storage, RoleSystemConfig::default());
let role_system = AsyncRoleSystem::new(role_sys);
let siblings = role_system.get_role_siblings("senior_dev").await?;
for sibling_id in siblings {
    println!("Sibling role: {}", sibling_id);
}
Source

pub async fn get_role_relationships( &self, _relationship_type: Option<RelationshipType>, ) -> Result<Vec<RoleRelationship>, Error>

Get all role relationships in the hierarchy.

This method returns all parent-child relationships, useful for database storage, API responses, and external system integration.

§Arguments
  • relationship_type - Optional filter for relationship type
§Returns

A vector of RoleRelationship objects representing all relationships.

§Example
let storage = MemoryStorage::new();
let role_sys = RoleSystem::with_storage(storage, RoleSystemConfig::default());
let role_system = AsyncRoleSystem::new(role_sys);

// Get all relationships
let all_relationships = role_system.get_role_relationships(None).await?;

// Get only direct relationships
let direct_relationships = role_system
    .get_role_relationships(Some(RelationshipType::Direct))
    .await?;
Source

pub async fn is_role_ancestor( &self, ancestor_id: &str, descendant_id: &str, ) -> Result<bool, Error>

Check if one role is an ancestor of another.

This method checks if ancestor_id is in the inheritance chain of descendant_id.

§Arguments
  • ancestor_id - The potential ancestor role ID
  • descendant_id - The potential descendant role ID
§Returns

true if ancestor_id is an ancestor of descendant_id.

§Example
let storage = MemoryStorage::new();
let role_sys = RoleSystem::with_storage(storage, RoleSystemConfig::default());
let role_system = AsyncRoleSystem::new(role_sys);
let is_ancestor = role_system
    .is_role_ancestor("admin", "junior_dev")
    .await?;

if is_ancestor {
    println!("admin is an ancestor of junior_dev");
}
Source

pub async fn get_role_depth(&self, role_id: &str) -> Result<usize, Error>

Get the hierarchy depth of a role.

The depth is the number of levels from the root of the hierarchy. Root roles have depth 0.

§Arguments
  • role_id - The ID of the role to get depth for
§Returns

The depth of the role in the hierarchy.

§Example
let storage = MemoryStorage::new();
let role_sys = RoleSystem::with_storage(storage, RoleSystemConfig::default());
let role_system = AsyncRoleSystem::new(role_sys);
let depth = role_system.get_role_depth("senior_dev").await?;
println!("Role depth: {}", depth);

Trait Implementations§

Source§

impl<S> Clone for AsyncRoleSystem<S>
where S: Storage + Send + Sync,

Source§

fn clone(&self) -> AsyncRoleSystem<S>

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

Auto Trait Implementations§

§

impl<S> Freeze for AsyncRoleSystem<S>

§

impl<S> !RefUnwindSafe for AsyncRoleSystem<S>

§

impl<S> Send for AsyncRoleSystem<S>

§

impl<S> Sync for AsyncRoleSystem<S>

§

impl<S> Unpin for AsyncRoleSystem<S>

§

impl<S> !UnwindSafe for AsyncRoleSystem<S>

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<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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> 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<T> ErasedDestructor for T
where T: 'static,