ferdiunal/nova-shield
Composer 安装命令:
composer require ferdiunal/nova-shield
包简介
A Laravel Nova package to manage permissions and roles.
关键字:
README 文档
README
Nova Shield simplifies permission management for your Laravel Nova resources using Spatie Permission. Easily grant or revoke access to specific resources and actions, streamlining your workflow and improving security.
Installation
composer require ferdiunal/nova-shield
Configuration
Optionally, you can publish the configuration file using this command:
php artisan vendor:publish --tag nova-shield-config
In the config file, you can control resource permissions by specifying the resources parameter with either the path to the Nova resources folder or a string class path.
For example, in the code below, we define the resources and their corresponding policies:
Show Code
<?php return [ /** * Specify the resources that will be used in the project. * If you want to use custom resources, you can add them to the list. */ 'resources' => [ app_path('Nova'), \Ferdiunal\NovaShield\Http\Nova\ShieldResource::class, // Custom resource: For custom menu items // [ // "name" => "Custom Menu Item", // "prefix" => "customMenuItem::", // "policies" => ["CustomMenuPolicy"] // Add custom menu policies here // ] ], // 'teamFields' => \App\Lib\TeamField::class, /** * Constant policies of Laravel Nova */ 'policies' => [ 'viewAny', 'view', 'create', 'update', 'replicate', 'delete', 'restore', 'forceDelete', 'runAction', 'runDestructiveAction', 'canImpersonate', 'canBeImpersonated', 'add{Model}', 'attach{Model}', 'attachAny{Model}', 'detach{Model}', ], /** * Specify the file path of each language files for authorisations. */ 'langs' => [ // lang_path('en/nova-shield.json'), // base_path('langs/en/nova-shield.json'), ], /** * Default Super admin role name and guard */ 'superAdmin' => [ 'name' => 'super-admin', 'guard' => 'web', ], 'hooks' => [ /** * When matching permissions with roles, upsert is used by default. * If you are using custom ID types like UUID or ULID, you need to include them in the upsert operation. * Therefore, you can write and use a query that suits your project needs. */ 'permission' => \Ferdiunal\NovaShield\Lib\DefaultPermissionHook::class, ], ];
Custom Menu Configuration
The main purpose of the package was to manage permissions for Nova Resources, but I realized there was a need for Custom Menu support as well. The necessary development has been completed. You can refer to the usage below for implementing Custom Menus.
// app/Providers/NovaServiceProvider.php Nova::mainMenu(function (Request $request) { return [ MenuItem::make('Custom Menu Item') ->path('/custom-menu') ->canSee( function ($request) { return $request->user()->can('CustomMenuPolicy'); } ) ]; }); // ---- // config/nova-shield.php return [ "resources" => [ ... // Custom resource: For custom menu items [ "name" => "Custom Menu Item", "prefix" => "customMenuItem::", "policies" => ['CustomMenuPolicy'] // Add custom menu policies here ] ] ];
Then edit App\Nova\Resource.php file as follows.
<?php namespace App\Nova; use Ferdiunal\NovaShield\PermissionAuthorizable; use Laravel\Nova\Resource as NovaResource; abstract class Resource extends NovaResource { use PermissionAuthorizable; .... }
Teams Support
I assume you have done the Teams integration for Spatie Permission and you can follow the setup below for NovaShield.
Model Integrations
Teams feature differs in each project, please fill in the relevant method according to your project.
// App\Models\User use Ferdiunal\NovaShield\Contracts\HasShieldTeam; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable implements HasShieldTeam { public function getTeamIdAttribute(): mixed { // return $this->team_id } }
Add Team Field
Considering that the Teams feature will differ in each project, you need to add the team field yourself.
namespace App\Lib; use Ferdiunal\NovaShield\Lib\NovaTeamHelperField; use Laravel\Nova\Fields\Field; use Laravel\Nova\Fields\Select; class TeamField extends NovaTeamHelperField { public static function field(): Field { return Select::make('Teams', 'team_id') ->options([]) ->displayUsingLabels() ->searchable() ->sortable(); } }
Add Middleware to Nova Routes
Add TeamMiddleware to nova routes
return [ /* |-------------------------------------------------------------------------- | Nova Route Middleware |-------------------------------------------------------------------------- | | These middleware will be assigned to every Nova route, giving you the | chance to add your own middleware to this stack or override any of | the existing middleware. Or, you can just stick with this stack. | */ 'middleware' => [ .... \Ferdiunal\NovaShield\Http\Middleware\TeamMiddleware::class, ], ];
Commands
The package contains two different commands. The first command checks whether the Super Admin role exists, creates it if it doesn’t, and assigns permissions to the resources specified in the configuration file. The second command is used to assign the Super Admin role to users.
Sync Super Admin role and permissions command:
php artisan artisan nova-shield:sync-super-admin
Assign Super Admin role to user(s) command:
php artisan nova-shield:super-admin
Hook Sync Permissions
When mapping permissions to roles, upsert is used by default. If you use custom ID types such as UUID or ULID, upsert does not identify these ids. Therefore, you can write and use a query that suits your project needs.
Example hook:
// app/SyncPermissionHook.php <?php namespace App; use Ferdiunal\NovaShield\Contracts\SyncPermissionHook as SyncPermissionHookContract; use Illuminate\Support\LazyCollection; use Illuminate\Support\Str; use Spatie\Permission\Contracts\Role; use Spatie\Permission\Models\Permission; class SyncPermissionHook implements SyncPermissionHookContract { /** * Sync permissions to a role * * @param array<int,string> $permissions * @return void */ public function __invoke(Role $role, $permissions) { Permission::query()->upsert( LazyCollection::make($permissions) ->map(function ($permission) use (&$role) { return [ 'id' => Str::uuid(), 'name' => $permission, 'guard_name' => $role->guard_name, 'created_at' => now(), 'updated_at' => now(), ]; })->toArray(), ['name', 'guard_name'], ['name', 'guard_name'], ); $role->syncPermissions($permissions); } }
You will then need to replace the class you created in the configuration file with the default.
Example:
// config/nova-shield.php return [ 'hooks' => [ /** * When matching permissions with roles, upsert is used by default. * If you are using custom ID types like UUID or ULID, you need to include them in the upsert operation. * Therefore, you can write and use a query that suits your project needs. */ 'permission' => \App\SyncPermissionHook::class, ], ];
ScreenShots
License This package is open-sourced software licensed under the MIT license.
ferdiunal/nova-shield 适用场景与选型建议
ferdiunal/nova-shield 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 37 次下载、GitHub Stars 达 11, 最近一次更新时间为 2024 年 09 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「nova」 「laravel」 「roles」 「permissions」 「shield」 「spatie」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ferdiunal/nova-shield 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ferdiunal/nova-shield 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ferdiunal/nova-shield 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Nova card that shows you your system information.
A Laravel Nova card.
Laravel Multiauth package
A Laravel Nova package for publishable fields
A Laravel Nova package for translatable fields
This package provides a flexible way to add Role-based Permissions to Laravel 6.x
统计信息
- 总下载量: 37
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 30
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-09-02





