Skip to main content

Crate larust_shield

Crate larust_shield 

Source
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::Policy already defines. An app that genuinely needs restore/force_delete-style abilities can still reach larust-permissions directly with its own larust_permissions::PermissionName impl - this crate doesn’t have to be the only way in.

Structs§

ResourcePermission
A resource paired with one ability - a real larust_permissions::PermissionName, so every existing larust-permissions function and both @can/@role Blade 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 - what create_resource_permissions creates all five of at once.

Traits§

ResourceName
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 as larust_permissions::authorize_permission.
can
true if user has ability on resource - has_permission_to under a resource-shaped name.
create_resource_permissions
create_permissions all five ALL_ABILITIES for resource at once - Shield’s own “generate permissions for this resource” step, idempotent the same way create_permission itself already is.
grant_resource_abilities
grant_role_permission for exactly the abilities in abilities - 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 one grant_role_permission call per ability. Each named permission must already exist (via create_resource_permissions) - same NotFound-on-an-uncreated-name contract every other larust-permissions write already has.