Skip to main content

Permissions

Struct Permissions 

Source
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

Source

pub fn new() -> Self

Creates a new empty permission set.

§Examples
use webgates_core::permissions::Permissions;

let permissions = Permissions::new();
assert!(permissions.is_empty());
Source

pub fn grant<P>(&mut self, permission: P) -> &mut Self
where 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")));
Source

pub fn revoke<P>(&mut self, permission: P) -> &mut Self
where 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"));
Source

pub fn has<P>(&self, permission: P) -> bool
where 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"));
Source

pub fn has_all<I, P>(&self, permissions: I) -> bool
where I: IntoIterator<Item = P>, P: Into<PermissionId>,

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"]));
Source

pub fn has_any<I, P>(&self, permissions: I) -> bool
where I: IntoIterator<Item = P>, P: Into<PermissionId>,

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"]));
Source

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);
Source

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());
Source

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());
Source

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"));
Source

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"));
Source

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"));
Source

pub fn with<P>(self, permission: P) -> Self
where 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"));
Source

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();
Source

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

Source§

fn as_ref(&self) -> &RoaringTreemap

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Permissions

Source§

fn clone(&self) -> Permissions

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Permissions

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Permissions

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Permissions

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Permissions

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<Permissions> for RoaringTreemap

Source§

fn from(permissions: Permissions) -> Self

Converts to this type from the input type.
Source§

impl From<RoaringTreemap> for Permissions

Source§

fn from(bitmap: RoaringTreemap) -> Self

Converts to this type from the input type.
Source§

impl<P> FromIterator<P> for Permissions
where P: Into<PermissionId>,

Source§

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

Source§

fn eq(&self, other: &Permissions) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Permissions

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Permissions

Auto Trait Implementations§

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<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> 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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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<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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,