Roles and permissions | laravel-permission-rust | Spatie       

 SPATIE  

laravel-permission-rust
=======================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-permission-rust](https://spatie.be/docs/laravel-permission-rust/v1)  Roles and permissions

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-permission-rust/v1) 

  Roles and permissions    
- [ Introduction ](https://spatie.be/docs/laravel-permission-rust/v1/introduction)
- [ Install and connect the application ](https://spatie.be/docs/laravel-permission-rust/v1/installation-laravel)
- [ Roles and permissions ](https://spatie.be/docs/laravel-permission-rust/v1/basic-usage)
- [ Authenticated routes and policies ](https://spatie.be/docs/laravel-permission-rust/v1/authorization)
- [ Querying users and bulk assignment ](https://spatie.be/docs/laravel-permission-rust/v1/querying-users)
- [ Console commands ](https://spatie.be/docs/laravel-permission-rust/v1/commands)
- [ Custom models and IDs ](https://spatie.be/docs/laravel-permission-rust/v1/models-and-ids)
- [ Cache and events ](https://spatie.be/docs/laravel-permission-rust/v1/cache-and-events)
- [ Integration tests ](https://spatie.be/docs/laravel-permission-rust/v1/testing)

 Roles and permissions
=====================

Roles and permissions
=======================================================================================================================

```
use spatie_permission::{Identifier, PermissionConfig, PermissionRegistrar, Subject};

let registrar = PermissionRegistrar::new(PermissionConfig { teams: true, ..Default::default() })?;
let edit = registrar.create_permission("edit articles", "web").await?;
let writer = registrar.create_role("writer", "web", Some("17")).await?;
registrar.role(&writer).await?.give_permission_to(&edit).await?;

let user = registrar.user(Subject::new("account", 42, "web").for_team(17))?;
user.assign_role(&writer).await?;
user.give_permission_to(&edit).await?;
assert!(user.has_role("writer").await?);
assert!(user.has_permission_to(&edit).await?);
assert!(user.has_all_permissions(["edit articles"]).await?);
assert_eq!(user.get_all_permissions().await?.len(), 1);

user.revoke_permission_to(&edit).await?;
assert!(user.has_permission_to(&edit).await?); // Still inherited from writer.
registrar.role(&writer).await?.revoke_permission_to(&edit).await?;
assert!(!user.has_permission_to(&edit).await?);
user.sync_roles(Vec::<Identifier>::new()).await?;
```

- Role/Permission arguments are reloaded. Integers are IDs; strings, including `"123"`, are names.
- An unknown permission errors in `has_permission_to`; `check_permission_to` returns false only for that missing-permission case. It does not swallow database errors.
- Sync resolves every input before changing assignments and runs in a native transaction/savepoint.
- Empty any-checks return false, empty all-checks return true, and exact-role checks compare distinct role identities without depending on input order.

Unknown role names return false from role predicates. Assignment still requires an existing role. `get_permission_names`, `has_any_direct_permission` and `has_all_direct_permissions` inspect direct grants only; they never include inherited permissions. `registrar.role(&writer).await?` also has `has_permission_to`, `has_any_permission` and `has_all_permissions`, including wildcard checks. Convenience methods on the supplied `Role` resolve the application's configured registrar; use `registrar.role` when working with an explicit registrar or custom model set.

Guards partition permissions and roles. `guard_name` is not a session-guard selector. Subject guard and persisted model guard must agree. Teams must be enabled in `PermissionConfig` before using `for_team`. Global roles have an empty `team_id`; team-specific roles are visible only in that team, and assignments of global roles are still scoped to the user's current team. When both share a name, lookup prefers the team-specific role. An explicit empty team ID is invalid. The application must validate team membership before constructing the context.

Set `enable_wildcard_permission: true` to use grants such as `articles.*.create,edit`. They match `articles.17.edit` but not `articles.17.delete`. Prefix grants imply descendants. Requested names need not have a separate row in wildcard mode. Empty dot/comma parts are invalid. Custom interpreters implement `Wildcard` and are selected with `PermissionConfig::wildcard`.

Use native model relations for inverse role-permission queries and eager loading, for example `writer.permissions().query().get().await?` or `permission.roles().query().get().await?`. Native relation writes are lower-level operations; use the package helpers when guard validation is needed. Application transactions can combine account changes and package calls using `laravel::db::transaction(async |_| { ... })`.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
