vyuldashev / nova-permission
A Laravel Nova tool for Spatie's Permission library.
Installs: 2 171 584
Dependents: 5
Suggesters: 1
Security: 0
Stars: 419
Watchers: 15
Forks: 217
Open Issues: 44
Requires
- php: ^7.4|^8.0
- laravel/nova: ^4.0
- spatie/laravel-permission: ^3.0|^4.0|^5.0
- v3.2.0
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.1
- v3.0.0
- v2.11.1
- v2.11.0
- v2.10.0
- v2.9.0
- v2.8.0
- v2.7.0
- v2.6.0
- v2.5.0
- v2.4.1
- v2.4.0
- v2.3.1
- v2.3.0
- v2.2.1
- v2.2.0
- v2.1.0
- dev-master / 2.0.x-dev
- v2.0.0
- v1.x-dev
- v1.10.0
- v1.9.0
- v1.8.0
- v1.7.0
- v1.6.0
- v1.5.0
- v1.4.8
- v1.4.7
- v1.4.6
- v1.4.5
- v1.4.4
- v1.4.3
- v1.4.2
- v1.4.1
- v1.4.0
- v1.3.1
- v1.3.0
- v1.2.0
- v1.1.7
- v1.1.6
- v1.1.5
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-sp4r74cus-nova-4
This package is auto-updated.
Last update: 2024-11-24 03:50:55 UTC
README
Installation
You can install the package in to a Laravel app that uses Nova via composer:
composer require vyuldashev/nova-permission
Go through the Installation section in order to setup laravel-permission.
Next up, you must register the tool with Nova. This is typically done in the tools
method of the NovaServiceProvider
.
// in app/Providers/NovaServiceProvider.php // ... public function tools() { return [ // ... \Vyuldashev\NovaPermission\NovaPermissionTool::make(), ]; }
Next, add middleware to config/nova.php
// in config/nova.php 'middleware' => [ // ... \Vyuldashev\NovaPermission\ForgetCachedPermissions::class, ],
Finally, add MorphToMany
fields to you app/Nova/User
resource:
// ... use Laravel\Nova\Fields\MorphToMany; public function fields(Request $request) { return [ // ... MorphToMany::make('Roles', 'roles', \Vyuldashev\NovaPermission\Role::class), MorphToMany::make('Permissions', 'permissions', \Vyuldashev\NovaPermission\Permission::class), ]; }
Or if you want to attach multiple permissions at once, use RoleBooleanGroup
and PermissionBooleanGroup
fields (requires at least Nova 2.6.0):
// ... use Vyuldashev\NovaPermission\PermissionBooleanGroup; use Vyuldashev\NovaPermission\RoleBooleanGroup; public function fields(Request $request) { return [ // ... RoleBooleanGroup::make('Roles'), PermissionBooleanGroup::make('Permissions'), ]; }
If your User
could have a single role at any given time, you can use RoleSelect
field. This field will render a standard select where you can pick a single role from.
// ... use Vyuldashev\NovaPermission\PermissionBooleanGroup; use Vyuldashev\NovaPermission\RoleSelect; public function fields(Request $request) { return [ // ... RoleSelect::make('Role', 'roles'), ]; }
Customization
If you want to use custom resource classes you can define them when you register a tool:
// in app/Providers/NovaServiceProvider.php // ... public function tools() { return [ // ... \Vyuldashev\NovaPermission\NovaPermissionTool::make() ->roleResource(CustomRole::class) ->permissionResource(CustomPermission::class), ]; }
If you want to show your roles and policies with a custom label, you can set $labelAttribute
when instantiating your fields:
// ... use Vyuldashev\NovaPermission\PermissionBooleanGroup; use Vyuldashev\NovaPermission\RoleSelect; public function fields(Request $request) { return [ // ... RoleBooleanGroup::make('Roles', 'roles', null, 'description'), PermissionBooleanGroup::make('Permissions', 'permissions', null, 'description'), RoleSelect::make('Role', 'roles', null, 'description'), ]; }
Define Policies
// in app/Providers/NovaServiceProvider.php // ... public function tools() { return [ // ... \Vyuldashev\NovaPermission\NovaPermissionTool::make() ->rolePolicy(RolePolicy::class) ->permissionPolicy(PermissionPolicy::class), ]; }
Usage
A new menu item called "Permissions & Roles" will appear in your Nova app after installing this package.