pub struct Role { /* private fields */ }
Expand description
A role represents a collection of permissions that can be assigned to subjects.
Implementations§
Source§impl Role
impl Role
Sourcepub fn with_id(id: impl Into<String>, name: impl Into<String>) -> Role
pub fn with_id(id: impl Into<String>, name: impl Into<String>) -> Role
Create a new role with a specific ID.
Sourcepub fn with_description(self, description: impl Into<String>) -> Role
pub fn with_description(self, description: impl Into<String>) -> Role
Set the role’s description.
Sourcepub fn description(&self) -> Option<&str>
pub fn description(&self) -> Option<&str>
Get the role’s description.
Sourcepub fn add_permission(self, permission: Permission) -> Role
pub fn add_permission(self, permission: Permission) -> Role
Add a permission to this role.
Sourcepub fn add_permissions(
self,
permissions: impl IntoIterator<Item = Permission>,
) -> Role
pub fn add_permissions( self, permissions: impl IntoIterator<Item = Permission>, ) -> Role
Add multiple permissions to this role.
Sourcepub fn remove_permission(&mut self, permission: &Permission)
pub fn remove_permission(&mut self, permission: &Permission)
Remove a permission from this role.
Sourcepub fn has_permission_exact(&self, permission: &Permission) -> bool
pub fn has_permission_exact(&self, permission: &Permission) -> bool
Check if this role has a specific permission.
Sourcepub fn has_permission(
&self,
action: &str,
resource_type: &str,
context: &HashMap<String, String>,
) -> bool
pub fn has_permission( &self, action: &str, resource_type: &str, context: &HashMap<String, String>, ) -> bool
Check if this role grants permission for an action on a resource type.
Sourcepub fn permissions(&self) -> &PermissionSet
pub fn permissions(&self) -> &PermissionSet
Get all permissions granted by this role.
Sourcepub fn with_metadata(
self,
key: impl Into<String>,
value: impl Into<String>,
) -> Role
pub fn with_metadata( self, key: impl Into<String>, value: impl Into<String>, ) -> Role
Set metadata for this role.
Sourcepub fn all_metadata(&self) -> &HashMap<String, String>
pub fn all_metadata(&self) -> &HashMap<String, String>
Get all metadata.
Sourcepub fn set_active(&mut self, active: bool)
pub fn set_active(&mut self, active: bool)
Set whether this role is active.
Sourcepub fn deactivate(self) -> Role
pub fn deactivate(self) -> Role
Deactivate this role.
Sourcepub fn merge_permissions(&mut self, other: &Role)
pub fn merge_permissions(&mut self, other: &Role)
Merge permissions from another role into this one.
Sourcepub fn parent_role_id(&self) -> Option<&str>
pub fn parent_role_id(&self) -> Option<&str>
Get the parent role ID if this role has a parent in the hierarchy.
This method provides access to the direct parent relationship, enabling use cases like API responses, admin interfaces, and JWT token generation.
Returns None
if:
- This role has no parent (it’s a root role)
- Hierarchy access is disabled in configuration
§Example
use role_system::Role;
let role = Role::new("junior_dev");
if let Some(parent_id) = role.parent_role_id() {
println!("Parent role: {}", parent_id);
}
§Note
This method returns None
by default to maintain backward compatibility.
The actual parent relationship is managed by the RoleHierarchy
system.
To use this feature, the role must be created through a system that
tracks hierarchy relationships and enables hierarchy access.
Sourcepub fn child_role_ids(&self) -> Vec<&str>
pub fn child_role_ids(&self) -> Vec<&str>
Get the child role IDs if this role has children in the hierarchy.
This method provides access to direct child relationships, useful for building admin interfaces, API responses, and permission visualization.
Returns empty vector if:
- This role has no children
- Hierarchy access is disabled in configuration
§Example
use role_system::Role;
let role = Role::new("team_lead");
let children = role.child_role_ids();
for child_id in children {
println!("Child role: {}", child_id);
}
§Note
This method returns an empty vector by default to maintain backward compatibility.
The actual child relationships are managed by the RoleHierarchy
system.
To use this feature, the role must be created through a system that
tracks hierarchy relationships and enables hierarchy access.
Sourcepub fn is_root_role(&self) -> bool
pub fn is_root_role(&self) -> bool
Check if this role is a root role (has no parent).
A root role is one that sits at the top of a hierarchy branch and doesn’t inherit from any other role.
§Example
use role_system::Role;
let admin_role = Role::new("admin");
if admin_role.is_root_role() {
println!("This is a top-level role");
}
§Note
Returns true
by default since individual Role instances don’t
track hierarchy relationships. Use RoleHierarchy
or AsyncRoleSystem
for actual hierarchy-aware operations.
Sourcepub fn is_leaf_role(&self) -> bool
pub fn is_leaf_role(&self) -> bool
Check if this role is a leaf role (has no children).
A leaf role is one that doesn’t have any roles inheriting from it.
§Example
use role_system::Role;
let intern_role = Role::new("intern");
if intern_role.is_leaf_role() {
println!("This role has no children");
}
§Note
Returns true
by default since individual Role instances don’t
track hierarchy relationships. Use RoleHierarchy
or AsyncRoleSystem
for actual hierarchy-aware operations.
Sourcepub fn hierarchy_depth(&self) -> usize
pub fn hierarchy_depth(&self) -> usize
Get the depth of this role in the hierarchy.
Root roles have depth 0, their children have depth 1, etc. This is useful for visualization and API responses.
§Example
use role_system::Role;
let role = Role::new("senior_dev");
let depth = role.hierarchy_depth();
println!("Role depth: {}", depth);
§Note
Returns 0
by default since individual Role instances don’t
track hierarchy relationships. Use RoleHierarchy
or AsyncRoleSystem
for actual hierarchy-aware depth calculation.
Sourcepub fn hierarchy_metadata(&self) -> HashMap<String, String>
pub fn hierarchy_metadata(&self) -> HashMap<String, String>
Get metadata about this role’s position in the hierarchy.
Returns a HashMap containing hierarchy-related metadata such as:
- “parent_count”: Number of ancestors
- “child_count”: Number of direct children
- “descendant_count”: Total number of descendants
- “depth”: Depth in hierarchy
§Example
use role_system::Role;
let role = Role::new("manager");
let hierarchy_meta = role.hierarchy_metadata();
if let Some(depth) = hierarchy_meta.get("depth") {
println!("Hierarchy depth: {}", depth);
}
§Note
Returns minimal metadata by default. Use RoleHierarchy
or AsyncRoleSystem
for complete hierarchy metadata.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Role
impl<'de> Deserialize<'de> for Role
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<Role, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Role, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl Serialize for Role
impl Serialize for Role
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Auto Trait Implementations§
impl Freeze for Role
impl !RefUnwindSafe for Role
impl Send for Role
impl Sync for Role
impl Unpin for Role
impl !UnwindSafe for Role
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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