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-postsmiddleware-string recognition inxr convert.crates/larust-convert/src/routes.rsalready blanket-defers everyRoute::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§
- Admin
Role - Names which of an app’s own
RoleNamevariants 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 baseUser); this is the compile-checked equivalent, one implementation per app instead of one per project reinventing the samehasRole('admin')one-liner. Deliberately a trait, not a hardcoded"admin"string literal baked intois_adminitself - this crate has no way to know an app’s own role variant is actually namedAdmin(the demo app’s own top-level role, for instance, isRole::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: - Permission
Name - 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_TYPEalready 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: - Role
Name - A caller-defined role name - see
PermissionName’s own doc comment, same shape and same reasoning.
Functions§
- assign_
role - Assigns
roletouser- Laravel’s$user->assignRole('admin').NotFoundifrolehasn’t beencreate_roled yet, same reasoning asgrant_role_permission. - authorize_
admin is_admin, converted into a 403 on failure - same shape asauthorize_permission.- authorize_
permission has_permission_to, converted into a 403 on failure - the same “reuse the primitive, don’t reinvent the check” patternlarust_auth::Policy’s ownauthorize_*sugar methods already use forlarust_auth::authorize.- create_
permission - Creates
permissionif it doesn’t already exist - seecreate_role, same idempotent shape. - create_
role - Creates
roleif it doesn’t already exist - idempotent (INSERT OR IGNORE), matching theRole::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’sRoleNameenum. - give_
permission_ to - Grants
permissiontouserdirectly, bypassing roles entirely - Laravel’s$user->givePermissionTo('edit-posts').NotFoundifpermissionhasn’t beencreate_permissiond yet. - grant_
role_ permission - Grants
rolethe ability to dopermission- every user later assignedroleinherits it (seehas_permission_to).NotFoundif eitherroleorpermissionhasn’t beencreate_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 trueifuserhaspermission- granted directly viagive_permission_to, or inherited through any role assigned viaassign_rolethat was in turn granted it viagrant_role_permission. One query, not two round trips. Same “unrecognized name reads asfalse” reasoning ashas_role.- has_
role trueifusercurrently hasroleassigned. An unrecognized role name (nevercreate_roled) simply reads asfalse, 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_roleagainst whichever roleRnames as itsAdminRole::admin.Ris turbofished at the call site (is_admin::<User, Role>(&user)), or wrapped in a zero-argument app-level helper the same waydemo‘s ownPost::can_managewrapshas_permission_to- seelarust-permissions’ own crate-level doc comment for that pattern. - remove_
role - Removes
rolefromuser, if they had it - a legal no-op if they didn’t (same “no meaningful double-removal error” reasoninglarust-notifications’smark_as_readalready applies to marking an already-read notification read again). A nonexistent role name is stillNotFound, distinct from “existing role, not currently assigned”.