pub struct Permission { /* private fields */ }
Expand description
A permission represents an action that can be performed on a resource type.
Permissions follow the format “action:resource” or “action:resource:instance” Examples:
- “read:documents” - Read any document
- “read:documents:doc123” - Read specific document doc123
- “admin:users” - Admin access to users
- “admin:users:user456” - Admin access to specific user
Special permissions:
- “:” grants all permissions (super admin)
- “action:*” grants all actions on any resource
- “*:resource” grants any action on a specific resource
- “action:resource:*” grants action on any instance of resource
Implementations§
Source§impl Permission
impl Permission
Sourcepub fn new(
action: impl Into<String>,
resource_type: impl Into<String>,
) -> Permission
pub fn new( action: impl Into<String>, resource_type: impl Into<String>, ) -> Permission
Create a new permission for an action on a resource type.
Sourcepub fn try_new(
action: impl Into<String>,
resource_type: impl Into<String>,
) -> Result<Permission, Error>
pub fn try_new( action: impl Into<String>, resource_type: impl Into<String>, ) -> Result<Permission, Error>
Try to create a new permission, returning an error if validation fails.
Sourcepub fn with_instance(
action: impl Into<String>,
resource_type: impl Into<String>,
instance: impl Into<String>,
) -> Permission
pub fn with_instance( action: impl Into<String>, resource_type: impl Into<String>, instance: impl Into<String>, ) -> Permission
Create a new permission for an action on a specific resource instance.
Sourcepub fn with_condition<F>(
action: impl Into<String>,
resource_type: impl Into<String>,
condition: F,
) -> Permission
pub fn with_condition<F>( action: impl Into<String>, resource_type: impl Into<String>, condition: F, ) -> Permission
Create a permission with a conditional validator.
Sourcepub fn with_instance_and_condition<F>(
action: impl Into<String>,
resource_type: impl Into<String>,
instance: impl Into<String>,
condition: F,
) -> Permission
pub fn with_instance_and_condition<F>( action: impl Into<String>, resource_type: impl Into<String>, instance: impl Into<String>, condition: F, ) -> Permission
Create a permission with both instance and condition.
Sourcepub fn wildcard(resource_type: impl Into<String>) -> Permission
pub fn wildcard(resource_type: impl Into<String>) -> Permission
Create a wildcard permission that grants access to all actions on a resource type.
Sourcepub fn super_admin() -> Permission
pub fn super_admin() -> Permission
Create a super-admin permission that grants access to everything.
Sourcepub fn with_context(
resource_type: impl Into<String>,
action: impl Into<String>,
context: Option<impl Into<String>>,
) -> Permission
pub fn with_context( resource_type: impl Into<String>, action: impl Into<String>, context: Option<impl Into<String>>, ) -> Permission
Create a permission with enhanced context awareness.
§Example
use role_system::permission::Permission;
let perm = Permission::with_context("users", "read", Some("own_data"));
Sourcepub fn with_scope(
resource_type: impl Into<String>,
action: impl Into<String>,
scopes: Vec<impl Into<String>>,
) -> Vec<Permission>
pub fn with_scope( resource_type: impl Into<String>, action: impl Into<String>, scopes: Vec<impl Into<String>>, ) -> Vec<Permission>
Create a permission with multiple scopes/actions.
§Example
use role_system::permission::Permission;
let perms = Permission::with_scope("users", "read", vec!["profile", "preferences"]);
Sourcepub fn conditional(
resource_type: impl Into<String>,
action: impl Into<String>,
) -> ConditionalPermissionBuilder
pub fn conditional( resource_type: impl Into<String>, action: impl Into<String>, ) -> ConditionalPermissionBuilder
Create a conditional permission that depends on context.
§Example
use role_system::permission::Permission;
let perm = Permission::conditional("users", "update")
.when(|context| context.get("user_id") == context.get("target_id"));
Sourcepub fn resource_type(&self) -> &str
pub fn resource_type(&self) -> &str
Get the resource type this permission applies to.
Sourcepub fn instance(&self) -> Option<&str>
pub fn instance(&self) -> Option<&str>
Get the specific instance this permission applies to, if any.
Sourcepub fn matches(&self, action: &str, resource_type: &str) -> bool
pub fn matches(&self, action: &str, resource_type: &str) -> bool
Check if this permission matches the given action and resource type. For backward compatibility, this doesn’t consider instances.
Sourcepub fn matches_with_instance(
&self,
action: &str,
resource_type: &str,
instance: Option<&str>,
) -> bool
pub fn matches_with_instance( &self, action: &str, resource_type: &str, instance: Option<&str>, ) -> bool
Check if this permission matches the given action, resource type, and optional instance.
Sourcepub fn implies(&self, other: &Permission) -> bool
pub fn implies(&self, other: &Permission) -> bool
Check if this permission implies another permission. A permission implies another if it grants equal or greater access.
Examples:
- “read:documents” implies “read:documents:doc123”
- “admin:*” implies “admin:users”
- “:” implies any permission
- “read:documents:*” implies “read:documents:doc123”
Trait Implementations§
Source§impl Clone for Permission
impl Clone for Permission
Source§fn clone(&self) -> Permission
fn clone(&self) -> Permission
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for Permission
impl Debug for Permission
Source§impl<'de> Deserialize<'de> for Permission
impl<'de> Deserialize<'de> for Permission
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<Permission, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Permission, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl Display for Permission
impl Display for Permission
Source§impl FromStr for Permission
impl FromStr for Permission
Source§impl Hash for Permission
impl Hash for Permission
Source§impl PartialEq for Permission
impl PartialEq for Permission
Source§impl Serialize for Permission
impl Serialize for Permission
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,
impl Eq for Permission
Auto Trait Implementations§
impl Freeze for Permission
impl !RefUnwindSafe for Permission
impl Send for Permission
impl Sync for Permission
impl Unpin for Permission
impl !UnwindSafe for Permission
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<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
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 moreSource§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string
, but without panic on OOM.