Skip to main content

Crate larust_permissions

Crate larust_permissions 

Source
Expand description

Roles and permissions - Laravel’s spatie/laravel-permission, the single most commonly-installed third-party Laravel package (see crates/larust-convert/src/composer.rs’s TIER_1 table, which points here for it), narrowed to its core role/permission-assignment shape.

A hybrid design, not a straight port - worth explaining up front, since it deliberately bends a rule this codebase otherwise holds hard. larust_auth::Policy’s own doc comment states the house style plainly: “a typo in an ability name is a compile error, not a silently-always- false runtime lookup.” Spatie’s package is the opposite by design - an admin edits roles and permissions from a settings screen, with no redeploy, which is the entire point of the package existing instead of everyone just writing Policy methods. That’s genuinely runtime data, not a corner this crate is cutting.

So the split is: names are compile-checked, assignment is not. An app defines its own permission/role set as a plain Rust type implementing PermissionName/RoleName - a typo in Permission::EditPosts is caught by rustc, same as any other misspelled identifier. What is runtime data, stored in SQLite, is purely the assignment graph: which roles exist, which permissions each role carries, and which users have which roles/permissions - exactly the part an admin actually needs to change without a redeploy.

Re-exported through larust_support::permission (see crates/larust-support/src/lib.rs) so generated apps depend only on larust-support, never on this crate directly.

@can(expr)/@role(expr) template directives now exist (see larust_view::ast::Node::Can/Node::Role, and their codegen in larust-macros/src/view.rs) - a .blade.xr template checks has_permission_to/has_role directly, without a route handler pre-computing a bool and passing it in as a plain context variable. expr is a raw Rust expression (@can(Permission::EditPosts), not a quoted string), carrying this crate’s own “names are compile-checked” half of its hybrid design all the way into templates - a typo’d variant name is a rustc error at the template’s own call site, the same guarantee this crate’s Rust-side callers already have. Requires a user: &U (U: Authenticatable) binding in the view! context and an async, Result-returning call site, checked eagerly at macro-expansion time - the same shape @wire(...)’s own session requirement already established.

AdminRole/is_admin/authorize_admin are Laravel’s $user->isAdmin() - a compile-checked marker trait naming which of an app’s own RoleName variants is the “admin” one, rather than a hardcoded "admin" string that would silently never match an app whose top role happens to be spelled differently (this repo’s own demo app calls its top role Role::Moderator).

§Deliberately out of scope for this version

  • No role:admin/permission:edit-posts middleware-string recognition in xr convert. crates/larust-convert/src/routes.rs already blanket-defers every Route::middleware(...)->group(...) call, deliberately (its own doc comment: “exactly the kind of semantic judgment call this phase avoids”) - this crate doesn’t special-case spatie’s own aliases within that existing boundary.

Traits§

AdminRole
Names which of an app’s own RoleName variants is its “admin” one - Laravel’s own $user->isAdmin() is usually a hand-rolled method apps write for themselves (there’s no such method on Laravel’s base User); this is the compile-checked equivalent, one implementation per app instead of one per project reinventing the same hasRole('admin') one-liner. Deliberately a trait, not a hardcoded "admin" string literal baked into is_admin itself - this crate has no way to know an app’s own role variant is actually named Admin (the demo app’s own top-level role, for instance, is Role::Moderator), and a magic string would be exactly the “silently-always-false on a typo” failure mode this crate’s own module doc comment says it avoids everywhere else. One method, returning the app’s own designated variant:
PermissionName
A caller-defined permission name - implemented on an app’s own enum (or unit structs), the same “typo is a compile error” precedent larust_auth::Policy/larust_queue::Job::JOB_TYPE/ larust_notifications::Notification::NOTIFICATION_TYPE already set. A method, not an associated const, because an app’s permission set is naturally one enum with many variants, and Rust has no per-variant associated consts:
RoleName
A caller-defined role name - see PermissionName’s own doc comment, same shape and same reasoning.

Functions§

assign_role
Assigns role to user - Laravel’s $user->assignRole('admin'). NotFound if role hasn’t been create_roled yet, same reasoning as grant_role_permission.
authorize_admin
is_admin, converted into a 403 on failure - same shape as authorize_permission.
authorize_permission
has_permission_to, converted into a 403 on failure - the same “reuse the primitive, don’t reinvent the check” pattern larust_auth::Policy’s own authorize_* sugar methods already use for larust_auth::authorize.
create_permission
Creates permission if it doesn’t already exist - see create_role, same idempotent shape.
create_role
Creates role if it doesn’t already exist - idempotent (INSERT OR IGNORE), matching the Role::firstOrCreate-style call a spatie-backed app’s own seeder would make. Typically called once, at app boot or from a seeder, for every variant of the app’s RoleName enum.
give_permission_to
Grants permission to user directly, bypassing roles entirely - Laravel’s $user->givePermissionTo('edit-posts'). NotFound if permission hasn’t been create_permissiond yet.
grant_role_permission
Grants role the ability to do permission - every user later assigned role inherits it (see has_permission_to). NotFound if either role or permission hasn’t been create_role/ create_permissiond yet - deliberately not auto-created from this call, so a typo’d name here fails loudly instead of silently seeding a stray row nothing else references.
has_permission_to
true if user has permission - granted directly via give_permission_to, or inherited through any role assigned via assign_role that was in turn granted it via grant_role_permission. One query, not two round trips. Same “unrecognized name reads as false” reasoning as has_role.
has_role
true if user currently has role assigned. An unrecognized role name (never create_roled) simply reads as false, not an error - unlike the write-side functions above, a read has no “which row did you mean” ambiguity to fail loudly about.
is_admin
Laravel’s $user->isAdmin(), without a magic "admin" string on either side: has_role against whichever role R names as its AdminRole::admin. R is turbofished at the call site (is_admin::<User, Role>(&user)), or wrapped in a zero-argument app-level helper the same way demo‘s own Post::can_manage wraps has_permission_to - see larust-permissions’ own crate-level doc comment for that pattern.
remove_role
Removes role from user, if they had it - a legal no-op if they didn’t (same “no meaningful double-removal error” reasoning larust-notifications’s mark_as_read already applies to marking an already-read notification read again). A nonexistent role name is still NotFound, distinct from “existing role, not currently assigned”.