bbs-lab / nova-permission
一个Laravel Nova权限工具。
Requires
- php: >=7.1.0
- guzzlehttp/guzzle: ~6.0 || ^7.0
- illuminate/notifications: ~5.5 || ~6.0 || ~7.0 || ~8.0
- illuminate/support: ~5.5 || ~6.0 || ~7.0 || ~8.0
- spatie/laravel-permission: ^3.0
This package is auto-updated.
Last update: 2024-09-15 10:16:39 UTC
README
基于 spatie/permission,此工具为您提供管理角色和权限的能力。该工具提供权限构建器。
内容
安装
您可以使用composer将nova工具安装到使用Nova的Laravel应用程序中
composer require bbs-lab/nova-permission
服务提供程序将自动注册。或者,您也可以手动在您的config/app.php
文件中添加服务提供程序
'providers' => [ // ... BBSLab\NovaPermission\NovaPermissionServiceProvider::class, ],
您应该使用以下命令发布迁移
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="migrations" php artisan vendor:publish --provider="BBSLab\NovaPermission\NovaPermissionServiceProvider" --tag="migrations"
您可以使用以下命令发布配置文件
php artisan vendor:publish --provider="BBSLab\NovaPermission\NovaPermissionServiceProvider" --tag="config"
这将发布包含以下内容的config/permission.php
和config/nova-permission.php
文件
// config/permission.php return [ 'models' => [ /* * When using the "HasPermissions" trait from this package, we need to know which * Eloquent model should be used to retrieve your permissions. Of course, it * is often just the "Permission" model but you may use whatever you like. * * The model you want to use as a Permission model needs to implement the * `BBSLab\NovaPermission\Contracts\Permission` contract. */ 'permission' => BBSLab\NovaPermission\Models\Permission::class, /* * When using the "HasRoles" trait from this package, we need to know which * Eloquent model should be used to retrieve your roles. Of course, it * is often just the "Role" model but you may use whatever you like. * * The model you want to use as a Role model needs to implement the * `BBSLab\NovaPermission\Contracts\Role` contract. */ 'role' => BBSLab\NovaPermission\Models\Role::class, ], 'table_names' => [ /* * When using the "HasRoles" trait from this package, we need to know which * table should be used to retrieve your roles. We have chosen a basic * default value but you may easily change it to any table you like. */ 'roles' => 'roles', /* * When using the "HasPermissions" trait from this package, we need to know which * table should be used to retrieve your permissions. We have chosen a basic * default value but you may easily change it to any table you like. */ 'permissions' => 'permissions', /* * When using the "HasPermissions" trait from this package, we need to know which * table should be used to retrieve your models permissions. We have chosen a * basic default value but you may easily change it to any table you like. */ 'model_has_permissions' => 'model_has_permissions', /* * When using the "HasRoles" trait from this package, we need to know which * table should be used to retrieve your models roles. We have chosen a * basic default value but you may easily change it to any table you like. */ 'model_has_roles' => 'model_has_roles', /* * When using the "HasRoles" trait from this package, we need to know which * table should be used to retrieve your roles permissions. We have chosen a * basic default value but you may easily change it to any table you like. */ 'role_has_permissions' => 'role_has_permissions', ], 'column_names' => [ /* * Change this if you want to name the related model primary key other than * `model_id`. * * For example, this would be nice if your primary keys are all UUIDs. In * that case, name this `model_uuid`. */ 'model_morph_key' => 'model_id', ], /* * When set to true, the required permission/role names are added to the exception * message. This could be considered an information leak in some contexts, so * the default setting is false here for optimum safety. */ 'display_permission_in_exception' => false, 'cache' => [ /* * By default all permissions are cached for 24 hours to speed up performance. * When permissions or roles are updated the cache is flushed automatically. */ 'expiration_time' => \DateInterval::createFromDateString('24 hours'), /* * The cache key used to store all permissions. */ 'key' => 'spatie.permission.cache', /* * When checking for a permission against a model by passing a Permission * instance to the check, this key determines what attribute on the * Permissions model is used to cache against. * * Ideally, this should match your preferred way of checking permissions, eg: * `$user->can('view-posts')` would be 'name'. */ 'model_key' => 'name', /* * You may optionally indicate a specific cache driver to use for permission and * role caching using any of the `store` drivers listed in the cache.php config * file. Using 'default' here means to use the `default` set in cache.php. */ 'store' => 'default', ], ];
// config/nova-permission return [ 'authorizable_models' => [ // \App\Models\Post::class, ], 'generate_without_resources' => [ \Laravel\Nova\Actions\ActionResource::class, \BBSLab\NovaPermission\Resources\Role::class, \BBSLab\NovaPermission\Resources\Permission::class, ] ];
迁移发布后,您可以通过运行迁移来创建角色和权限表
php artisan migrate
使用
您必须使用Nova注册此工具。这通常在NovaServiceProvider
的tools
方法中完成
// app/Providers/NovaServiceProvider.php public function tools() { return [ // ... new BBSLab\NovaPermission\PermissionBuilder(), ]; }
生成权限
该工具允许生成资源权限。您的资源必须实现BBSLab\NovaPermission\Contracts\HasAbilities
并定义公共静态变量$permissionsForAbilities
namespace App\Nova; use BBSLab\NovaPermission\Contracts\HasAbilities; use BBSLab\NovaPermission\Traits\Authorizable; class Post extends Resource implements HasAbilities { use Authorizable; public static $permissionsForAbilities = [ 'create' => 'create post', ]; }
此配置将生成以下权限
[ 'name' => 'create post', 'group' => 'Post', 'guard_name' => 'web', // the nova guard or default auth guard ]
您可以使用“生成权限”按钮或Artisan命令从权限构建器工具生成权限
php artisan nova-permission:generate
保护资源
您可以使用Laravel策略如常使用
namespace App\Policies; use App\User; use App\Post; use Illuminate\Auth\Access\HandlesAuthorization; class PostPolicy { use HandlesAuthorization; /** * Determine whether the user can view any post. * * @param \App\User $user * @return mixed */ public function viewAny(User $user) { if ($user->hasPermissionTo('viewAny post')) { return true; } } /** * Determine whether the user can update the post. * * @param \App\User $user * @param \App\Post $post * @return mixed */ public function update(User $user, Post $post) { if ($user->hasPermissionTo('update post')) { return true; } } }
有时您可能想要保护某个特定资源。首先,模型必须实现BBSLab\NovaPermission\Contracts\HasAuthorizations
namespace App\Models; use BBSLab\NovaPermission\Contracts\HasAuthorizations; use BBSLab\NovaPermission\Traits\Authorizations; class Post extends Model implements HasAuthorizations { use Authorizations; }
您需要在config/nova-permission.php
中添加资源
'authorizable_models' => [ \App\Nova\Post::class, ],
现在,您可以为特定帖子创建一个权限
并更新帖子策略
/** * Determine whether the user can update the post. * * @param \App\User $user * @param \App\Post $post * @return mixed */ public function update(User $user, Post $post) { if ($user->hasPermissionToOnModel('update post', $post)) { return true; } }
超级管理员
您可以使用override_permission
属性创建超级管理员角色。
您可以使用BBSLab\NovaPermission\Resources\Role::canSeeOverridePermmission
方法防止修改override_permission
属性
// in a service provider BBSLab\NovaPermission\Resources\Role::canSeeOverridePermmission(function (Request $request) { // implement your logic });
变更日志
请参阅CHANGELOG获取有关最近更改的更多信息。
安全性
如果您发现任何与安全性相关的问题,请通过paris@big-boss-studio.com发送电子邮件,而不是使用问题跟踪器。
贡献
有关详细信息,请参阅CONTRIBUTING
鸣谢
许可协议
MIT许可(MIT)。有关更多信息,请参阅许可文件