|
| 1 | +# A Laravel Nova tool for the Spatie Permission library |
| 2 | + |
| 3 | +[](https://scrutinizer-ci.com/g/insenseanalytics/laravel-nova-permission/?branch=master) |
| 4 | + |
| 5 | +[](https://scrutinizer-ci.com/code-intelligence) |
| 6 | + |
| 7 | + |
| 8 | +This [Nova](https://nova.laravel.com) tool lets you: |
| 9 | +- manage roles and permissions on the Nova dashboard |
| 10 | +- use permissions based authorization for Nova resources |
| 11 | + |
| 12 | +## Nova Dashboard Screenshots |
| 13 | + |
| 14 | +## Requirements & Dependencies |
| 15 | +There are no PHP dependencies except the Laravel Nova package and the Spatie Permission library. |
| 16 | + |
| 17 | +## Installation |
| 18 | +You can install this tool into a Laravel app that uses [Nova](https://nova.laravel.com) via composer: |
| 19 | + |
| 20 | +```bash |
| 21 | +composer require insenseanalytics/laravel-nova-permission |
| 22 | +``` |
| 23 | + |
| 24 | +Next, if you do not have package discovery enabled, you need to register the provider in the `config/app.php` file. |
| 25 | +```php |
| 26 | +'providers' => [ |
| 27 | + ..., |
| 28 | + Insenseanalytics\LaravelNovaPermission\NovaPermissionServiceProvider::class, |
| 29 | +] |
| 30 | +``` |
| 31 | + |
| 32 | +Next, you must register the tool with Nova. This is typically done in the `tools` method of the `NovaServiceProvider`. |
| 33 | + |
| 34 | +```php |
| 35 | +// in app/Providers/NovaServiceProvider.php |
| 36 | + |
| 37 | +public function tools() |
| 38 | +{ |
| 39 | + return [ |
| 40 | + // ... |
| 41 | + \Insenseanalytics\LaravelNovaPermission\LaravelNovaPermission::make(), |
| 42 | + ]; |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +Finally, add `MorphToMany` fields to you `app/Nova/User` resource: |
| 47 | + |
| 48 | +```php |
| 49 | +use Laravel\Nova\Fields\MorphToMany; |
| 50 | + |
| 51 | +public function fields(Request $request) |
| 52 | +{ |
| 53 | + return [ |
| 54 | + // ... |
| 55 | + MorphToMany::make('Roles', 'roles', \InsenseAnalytics\LaravelNovaPermission\Role::class), |
| 56 | + MorphToMany::make('Permissions', 'permissions', \InsenseAnalytics\LaravelNovaPermission\Permission::class), |
| 57 | + ]; |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +## Using Custom Role/Permission Resource Classes |
| 62 | + |
| 63 | +If you want to use custom resource classes you can define them when you register a tool: |
| 64 | + |
| 65 | +```php |
| 66 | +// in app/Providers/NovaServiceProvider.php |
| 67 | + |
| 68 | +public function tools() |
| 69 | +{ |
| 70 | + return [ |
| 71 | + // ... |
| 72 | + \Insenseanalytics\LaravelNovaPermission\LaravelNovaPermission::make() |
| 73 | + ->roleResource(CustomRole::class) |
| 74 | + ->permissionResource(CustomPermission::class), |
| 75 | + ]; |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +## Permissions Based Authorization for Nova Resources |
| 80 | +By default, Laravel Nova uses Policy based authorization for Nova resources. If you are using the Spatie Permission library, it is very likely that you would want to swap this out to permission based authorization without the need to define Authorization policies. |
| 81 | + |
| 82 | +To do so, you can use the `PermissionsBasedAuthTrait` and define a `permissionsForAbilities` static array property in your Nova resource class like so: |
| 83 | + |
| 84 | +```php |
| 85 | +// in app/Nova/YourNovaResource.php |
| 86 | + |
| 87 | +class YourNovaResource extends Resource |
| 88 | +{ |
| 89 | + use \Insenseanalytics\LaravelNovaPermission\PermissionsBasedAuthTrait; |
| 90 | + |
| 91 | + public static $permissionsForAbilities = [ |
| 92 | + 'all' => 'manage products', |
| 93 | + ]; |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +The example above means that all actions on this resource can be performed by users who have the "manage products" permission. You can also define separate permissions for each action like so: |
| 98 | + |
| 99 | +```php |
| 100 | + public static $permissionsForAbilities = [ |
| 101 | + 'viewAny' => 'view products', |
| 102 | + 'view' => 'view products', |
| 103 | + 'create' => 'create products', |
| 104 | + 'update' => 'update products', |
| 105 | + 'delete' => 'delete products', |
| 106 | + 'restore' => 'restore products', |
| 107 | + 'forceDelete' => 'forceDelete products', |
| 108 | + 'addAttribute' => 'add product attributes', |
| 109 | + 'attachAttribute' => 'attach product attributes', |
| 110 | + 'detachAttribute' => 'detach product attributes', |
| 111 | + ]; |
| 112 | +``` |
| 113 | + |
| 114 | +## Contributing |
| 115 | + |
| 116 | +Contributions are welcome and will be fully credited as long as you use PSR-2, explain the issue/feature that you want to solve/add and back your code up with tests. Happy coding! |
| 117 | + |
| 118 | +## License |
| 119 | + |
| 120 | +The MIT License (MIT). Please see [License File](LICENSE.txt) for more information. |
0 commit comments