pub struct Permissions { /* private fields */ }Expand description
A set of granted permissions.
Internally this type stores permission IDs in a compressed bitmap for compact
storage and fast membership checks. The public API accepts permission names
or precomputed PermissionId values while keeping the bitmap representation
internal.
In day-to-day application code, this is the type you use to grant, revoke, and check fine-grained capabilities.
§Examples
use webgates_core::permissions::Permissions;
let mut permissions = Permissions::new();
permissions
.grant("read:profile")
.grant("write:profile")
.grant("delete:profile");
assert!(permissions.has("read:profile"));
assert!(!permissions.has("admin:users"));
assert!(permissions.has_all(["read:profile", "write:profile"]));
assert!(permissions.has_any(["read:profile", "admin:users"]));Build a set immutably:
use webgates_core::permissions::Permissions;
let permissions = Permissions::new()
.with("read:api")
.with("write:api")
.build();
assert!(permissions.has("read:api"));
assert!(permissions.has("write:api"));Implementations§
Source§impl Permissions
impl Permissions
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty permission set.
§Examples
use webgates_core::permissions::Permissions;
let permissions = Permissions::new();
assert!(permissions.is_empty());Sourcepub fn grant<P>(&mut self, permission: P) -> &mut Selfwhere
P: Into<PermissionId>,
pub fn grant<P>(&mut self, permission: P) -> &mut Selfwhere
P: Into<PermissionId>,
Grants a permission to this set.
Returns a mutable reference to self for method chaining.
§Examples
use webgates_core::permissions::permission_id::PermissionId;
use webgates_core::permissions::Permissions;
let mut permissions = Permissions::new();
permissions
.grant("read:profile")
.grant(PermissionId::from("write:profile"));
assert!(permissions.has("read:profile"));
assert!(permissions.has(PermissionId::from("write:profile")));Sourcepub fn revoke<P>(&mut self, permission: P) -> &mut Selfwhere
P: Into<PermissionId>,
pub fn revoke<P>(&mut self, permission: P) -> &mut Selfwhere
P: Into<PermissionId>,
Revokes a permission from this set.
Returns a mutable reference to self for method chaining.
§Examples
use webgates_core::permissions::permission_id::PermissionId;
use webgates_core::permissions::Permissions;
let mut permissions: Permissions = ["read:profile", "write:profile"].into_iter().collect();
permissions.revoke(PermissionId::from("write:profile"));
assert!(permissions.has("read:profile"));
assert!(!permissions.has("write:profile"));Sourcepub fn has<P>(&self, permission: P) -> boolwhere
P: Into<PermissionId>,
pub fn has<P>(&self, permission: P) -> boolwhere
P: Into<PermissionId>,
Returns true when a specific permission is granted.
§Examples
use webgates_core::permissions::permission_id::PermissionId;
use webgates_core::permissions::Permissions;
let permissions: Permissions = ["read:profile"].into_iter().collect();
assert!(permissions.has("read:profile"));
assert!(permissions.has(PermissionId::from("read:profile")));
assert!(!permissions.has("write:profile"));Sourcepub fn has_all<I, P>(&self, permissions: I) -> bool
pub fn has_all<I, P>(&self, permissions: I) -> bool
Returns true when all specified permissions are granted.
§Examples
use webgates_core::permissions::permission_id::PermissionId;
use webgates_core::permissions::Permissions;
let permissions: Permissions = [
"read:profile",
"write:profile",
"read:posts",
].into_iter().collect();
assert!(permissions.has_all(["read:profile", "write:profile"]));
assert!(permissions.has_all([PermissionId::from("read:profile")]));
assert!(!permissions.has_all(["read:profile", "admin:users"]));Sourcepub fn has_any<I, P>(&self, permissions: I) -> bool
pub fn has_any<I, P>(&self, permissions: I) -> bool
Returns true when any of the specified permissions are granted.
§Examples
use webgates_core::permissions::permission_id::PermissionId;
use webgates_core::permissions::Permissions;
let permissions: Permissions = ["read:profile"].into_iter().collect();
assert!(permissions.has_any(["read:profile", "write:profile"]));
assert!(permissions.has_any([PermissionId::from("read:profile")]));
assert!(!permissions.has_any(["write:profile", "admin:users"]));Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of granted permissions in this set.
§Examples
use webgates_core::permissions::Permissions;
let permissions: Permissions = ["read:profile", "write:profile"].into_iter().collect();
assert_eq!(permissions.len(), 2);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the set contains no permissions.
§Examples
use webgates_core::permissions::Permissions;
let permissions = Permissions::new();
assert!(permissions.is_empty());
let mut permissions = Permissions::new();
permissions.grant("read:profile");
assert!(!permissions.is_empty());Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Removes all permissions from this set.
§Examples
use webgates_core::permissions::Permissions;
let mut permissions: Permissions = ["read:profile", "write:profile"].into_iter().collect();
assert!(!permissions.is_empty());
permissions.clear();
assert!(permissions.is_empty());Sourcepub fn union(&mut self, other: &Permissions) -> &mut Self
pub fn union(&mut self, other: &Permissions) -> &mut Self
Merges another permission set into this one.
After this call, the set contains every permission that exists in either set.
§Examples
use webgates_core::permissions::Permissions;
let mut permissions1: Permissions = ["read:profile"].into_iter().collect();
let permissions2: Permissions = ["write:profile"].into_iter().collect();
permissions1.union(&permissions2);
assert!(permissions1.has("read:profile"));
assert!(permissions1.has("write:profile"));Sourcepub fn intersection(&mut self, other: &Permissions) -> &mut Self
pub fn intersection(&mut self, other: &Permissions) -> &mut Self
Intersects this set with another permission set.
After this call, only permissions present in both sets remain.
§Examples
use webgates_core::permissions::Permissions;
let mut permissions1: Permissions = ["read:profile", "write:profile"].into_iter().collect();
let permissions2: Permissions = ["read:profile", "admin:users"].into_iter().collect();
permissions1.intersection(&permissions2);
assert!(permissions1.has("read:profile"));
assert!(!permissions1.has("write:profile"));
assert!(!permissions1.has("admin:users"));Sourcepub fn difference(&mut self, other: &Permissions) -> &mut Self
pub fn difference(&mut self, other: &Permissions) -> &mut Self
Removes from this set any permissions that also exist in another set.
§Examples
use webgates_core::permissions::Permissions;
let mut permissions1: Permissions = ["read:profile", "write:profile"].into_iter().collect();
let permissions2: Permissions = ["write:profile"].into_iter().collect();
permissions1.difference(&permissions2);
assert!(permissions1.has("read:profile"));
assert!(!permissions1.has("write:profile"));Sourcepub fn with<P>(self, permission: P) -> Selfwhere
P: Into<PermissionId>,
pub fn with<P>(self, permission: P) -> Selfwhere
P: Into<PermissionId>,
Builder-style variant of Self::grant.
Use this when constructing a permission set fluently without mutable access.
§Examples
use webgates_core::permissions::permission_id::PermissionId;
use webgates_core::permissions::Permissions;
let permissions = Permissions::new()
.with("read:profile")
.with(PermissionId::from("write:profile"))
.build();
assert!(permissions.has("read:profile"));
assert!(permissions.has("write:profile"));Sourcepub fn build(self) -> Self
pub fn build(self) -> Self
Finalizes the fluent builder pattern.
This returns self unchanged and mostly serves readability in builder-style code.
§Examples
use webgates_core::permissions::Permissions;
let permissions = Permissions::new()
.with("read:profile")
.with("write:profile")
.build();Sourcepub fn iter(&self) -> impl Iterator<Item = u64> + '_
pub fn iter(&self) -> impl Iterator<Item = u64> + '_
Returns an iterator over the raw permission IDs in this set.
Use this when you need to inspect all granted permissions or integrate with lower-level systems that work with permission IDs directly.
§Examples
use webgates_core::permissions::Permissions;
let permissions: Permissions = ["read:profile", "write:profile"].into_iter().collect();
let ids: Vec<u64> = permissions.iter().collect();
assert_eq!(ids.len(), 2);Trait Implementations§
Source§impl AsRef<RoaringTreemap> for Permissions
impl AsRef<RoaringTreemap> for Permissions
Source§fn as_ref(&self) -> &RoaringTreemap
fn as_ref(&self) -> &RoaringTreemap
Source§impl Clone for Permissions
impl Clone for Permissions
Source§fn clone(&self) -> Permissions
fn clone(&self) -> Permissions
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Permissions
impl Debug for Permissions
Source§impl Default for Permissions
impl Default for Permissions
Source§impl<'de> Deserialize<'de> for Permissions
impl<'de> Deserialize<'de> for Permissions
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Display for Permissions
impl Display for Permissions
Source§impl From<Permissions> for RoaringTreemap
impl From<Permissions> for RoaringTreemap
Source§fn from(permissions: Permissions) -> Self
fn from(permissions: Permissions) -> Self
Source§impl From<RoaringTreemap> for Permissions
impl From<RoaringTreemap> for Permissions
Source§fn from(bitmap: RoaringTreemap) -> Self
fn from(bitmap: RoaringTreemap) -> Self
Source§impl<P> FromIterator<P> for Permissionswhere
P: Into<PermissionId>,
impl<P> FromIterator<P> for Permissionswhere
P: Into<PermissionId>,
Source§fn from_iter<I: IntoIterator<Item = P>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = P>>(iter: I) -> Self
Creates a permission set from an iterator of permission values.
§Examples
use webgates_core::permissions::Permissions;
let permissions: Permissions = ["read:profile", "write:profile", "read:posts"]
.into_iter()
.collect();
assert!(permissions.has("read:profile"));
assert!(permissions.has("write:profile"));
assert!(permissions.has("read:posts"));Source§impl PartialEq for Permissions
impl PartialEq for Permissions
Source§fn eq(&self, other: &Permissions) -> bool
fn eq(&self, other: &Permissions) -> bool
self and other values to be equal, and is used by ==.