Expand description
A lighter-weight take on bezhansalleh/filament-shield (a popular
FilamentPHP plugin that auto-generates one CRUD-shaped permission bundle
per admin-panel resource, then lets roles pick which abilities they
carry) - requested directly, with an explicit “not to the degree they
went on it” scope: no auto-scanning of #[derive(Model)] structs (this
framework has no resource-registry to scan, and building one just for
this would be new, load-bearing magic for a purely additive feature),
and only the five abilities larust_auth::Policy already established
(view_any/view/create/update/delete) - not Shield’s fuller
delete_any/restore/force_delete/… list, most of which have
nothing real backing them in this codebase (migrations are
forward-only, there’s no soft-delete convention).
An app declares its own resources exactly the way it already declares roles/permissions - a plain enum implementing one small marker trait:
#[derive(Copy, Clone)]
enum Resource { Posts, Comments }
impl larust_support::shield::ResourceName for Resource {
fn name(&self) -> &'static str {
match self {
Resource::Posts => "posts",
Resource::Comments => "comments",
}
}
}ResourcePermission then implements larust_permissions::PermissionName
directly - a Resource paired with an Ability is a permission, so
every existing larust-permissions function
(create_permission/grant_role_permission/has_permission_to/…)
and both @can/@role Blade directives already understand it, with
zero changes needed to either. This crate adds no new storage, no new
tables, and no new checking primitive - only a second, resource-shaped
way to name a permission larust-permissions already knows how to
store and check.
shield::create_resource_permissions(Resource::Posts).await?; // all 5 at once
shield::grant_resource_abilities(Role::Editor, Resource::Posts, &[Ability::View, Ability::Update]).await?;
shield::can(&user, Resource::Posts, Ability::Update).await?; // bool
shield::authorize_ability(&user, Resource::Posts, Ability::Update).await?; // straight to a 403§Deliberately out of scope for this version
- No auto-discovery of resources. An app lists its own resources by
hand, the same way it already lists its own roles/permissions -
consistent with this framework having no reflection-based registry
anywhere else (see
docs/coming-from-laravel.md’s “no service container, no reflection-based dependency injection”). - No admin UI for editing role/resource grants at runtime. Shield’s own headline feature is a settings screen; this crate is the underlying primitive an app could build one on top of, not the screen itself.
- No abilities beyond the five
larust_auth::Policyalready defines. An app that genuinely needsrestore/force_delete-style abilities can still reachlarust-permissionsdirectly with its ownlarust_permissions::PermissionNameimpl - this crate doesn’t have to be the only way in.
Structs§
- Resource
Permission - A resource paired with one ability - a real
larust_permissions::PermissionName, so every existinglarust-permissionsfunction and both@can/@roleBlade directives already accept it directly.
Enums§
- Ability
- The five abilities every resource gets, one-for-one with
larust_auth::Policy’s own method names - deliberately not Shield’s fuller list, see this crate’s own doc comment for why.
Constants§
- ALL_
ABILITIES - Every
Ability, in a fixed order - whatcreate_resource_permissionscreates all five of at once.
Traits§
- Resource
Name - A caller-defined resource name - same shape and reasoning as
larust_permissions::PermissionName/larust_permissions::RoleName: a plain enum, so a typo’d resource is a compile error, not a string that silently never matches anything.
Functions§
- authorize_
ability can, converted into a 403 on failure - same shape aslarust_permissions::authorize_permission.- can
trueifuserhasabilityonresource-has_permission_tounder a resource-shaped name.- create_
resource_ permissions create_permissions all fiveALL_ABILITIESforresourceat once - Shield’s own “generate permissions for this resource” step, idempotent the same waycreate_permissionitself already is.- grant_
resource_ abilities grant_role_permissionfor exactly the abilities inabilities- Shield’s own per-resource ability checkboxes on a role, as a single call:grant_resource_abilities(Role::Editor, Resource::Posts, &[Ability::View, Ability::Update])grants a role “can view and update posts, nothing else” in one line instead of onegrant_role_permissioncall per ability. Each named permission must already exist (viacreate_resource_permissions) - sameNotFound-on-an-uncreated-name contract every otherlarust-permissionswrite already has.