├── .gitignore
├── LICENSE.txt
├── README.md
├── composer.json
├── resources
├── lang
│ └── en
│ │ ├── navigation.php
│ │ ├── permissions.php
│ │ ├── resources.php
│ │ └── roles.php
└── views
│ └── navigation.blade.php
└── src
├── ForgetCachedPermissions.php
├── LaravelNovaPermission.php
├── NovaPermissionServiceProvider.php
├── Permission.php
├── PermissionResourceTrait.php
├── PermissionsBasedAuthTrait.php
├── Role.php
└── RoleResourceTrait.php
/.gitignore:
--------------------------------------------------------------------------------
1 | /.idea
2 | /vendor
3 | /node_modules
4 | package-lock.json
5 | composer.phar
6 | composer.lock
7 | phpunit.xml
8 | .phpunit.result.cache
9 | .DS_Store
10 | Thumbs.db
11 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Insense Private Limited
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6 |
7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 |
9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # A Laravel Nova tool for the Spatie Permission package
2 |
3 | [](https://packagist.org/packages/insenseanalytics/laravel-nova-permission)
4 | [](https://packagist.org/packages/insenseanalytics/laravel-nova-permission)
5 | [](https://scrutinizer-ci.com/g/insenseanalytics/laravel-nova-permission/?branch=master)
6 | [](https://packagist.org/packages/insenseanalytics/laravel-nova-permission)
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 | ## Screenshots
13 |
14 |
15 | ## Requirements & Dependencies
16 | There are no PHP dependencies except the [Laravel Nova](https://nova.laravel.com) package and the [Spatie Permission](https://github.com/spatie/laravel-permission) package.
17 |
18 | ## Installation
19 | You can install this tool into a Laravel app that uses [Nova](https://nova.laravel.com) via composer:
20 |
21 | ```bash
22 | composer require insenseanalytics/laravel-nova-permission
23 | ```
24 |
25 | Next, if you do not have package discovery enabled, you need to register the provider in the `config/app.php` file.
26 | ```php
27 | 'providers' => [
28 | ...,
29 | Insenseanalytics\LaravelNovaPermission\NovaPermissionServiceProvider::class,
30 | ]
31 | ```
32 |
33 | Next, you must register the tool with Nova. This is typically done in the `tools` method of the `NovaServiceProvider`.
34 |
35 | ```php
36 | // in app/Providers/NovaServiceProvider.php
37 |
38 | public function tools()
39 | {
40 | return [
41 | // ...
42 | \Insenseanalytics\LaravelNovaPermission\LaravelNovaPermission::make(),
43 | ];
44 | }
45 | ```
46 |
47 | Next, add `MorphToMany` fields to your `app/Nova/User` resource:
48 |
49 | ```php
50 | use Laravel\Nova\Fields\MorphToMany;
51 |
52 | public function fields(Request $request)
53 | {
54 | return [
55 | // ...
56 | MorphToMany::make('Roles', 'roles', \Insenseanalytics\LaravelNovaPermission\Role::class),
57 | MorphToMany::make('Permissions', 'permissions', \Insenseanalytics\LaravelNovaPermission\Permission::class),
58 | ];
59 | }
60 | ```
61 |
62 | Finally, add the `ForgetCachedPermissions` class to your `config/nova.php` middleware like so:
63 |
64 | ```php
65 | // in config/nova.php
66 | 'middleware' => [
67 | 'web',
68 | Authenticate::class,
69 | DispatchServingNovaEvent::class,
70 | BootTools::class,
71 | Authorize::class,
72 | \Insenseanalytics\LaravelNovaPermission\ForgetCachedPermissions::class,
73 | ],
74 | ```
75 |
76 | ## Localization
77 |
78 | You can use the artisan command line tool to publish localization files:
79 |
80 | ```php
81 | php artisan vendor:publish --provider="Insenseanalytics\LaravelNovaPermission\NovaPermissionServiceProvider"
82 | ```
83 |
84 | ## Using Custom Role/Permission Resource Classes
85 |
86 | If you want to use custom resource classes you can define them when you register a tool:
87 |
88 | ```php
89 | // in app/Providers/NovaServiceProvider.php
90 |
91 | public function tools()
92 | {
93 | return [
94 | // ...
95 | \Insenseanalytics\LaravelNovaPermission\LaravelNovaPermission::make()
96 | ->roleResource(CustomRole::class)
97 | ->permissionResource(CustomPermission::class),
98 | ];
99 | }
100 | ```
101 |
102 | ## Permissions Based Authorization for Nova Resources
103 | 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.
104 |
105 | To do so, you can use the `PermissionsBasedAuthTrait` and define a `permissionsForAbilities` static array property in your Nova resource class like so:
106 |
107 | ```php
108 | // in app/Nova/YourNovaResource.php
109 |
110 | class YourNovaResource extends Resource
111 | {
112 | use \Insenseanalytics\LaravelNovaPermission\PermissionsBasedAuthTrait;
113 |
114 | public static $permissionsForAbilities = [
115 | 'all' => 'manage products',
116 | ];
117 | }
118 | ```
119 |
120 | 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:
121 |
122 | ```php
123 | public static $permissionsForAbilities = [
124 | 'viewAny' => 'view products',
125 | 'view' => 'view products',
126 | 'create' => 'create products',
127 | 'update' => 'update products',
128 | 'delete' => 'delete products',
129 | 'restore' => 'restore products',
130 | 'forceDelete' => 'forceDelete products',
131 | 'addAttribute' => 'add product attributes',
132 | 'attachAttribute' => 'attach product attributes',
133 | 'detachAttribute' => 'detach product attributes',
134 | ];
135 | ```
136 |
137 | ### Relationships
138 | To allow your users to specify a relationship on your model, you will need to add another permission on the Model.
139 | For example, if your `Product` belongs to `User`, add the following permission on `app/Nova/User.php`. :
140 |
141 | ```php
142 | public static $permissionsForAbilities = [
143 | 'addProduct' => 'add user on products'
144 | ];
145 | ```
146 |
147 | ## Contributing
148 |
149 | 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!
150 |
151 | ## License
152 |
153 | The MIT License (MIT). Please see [License File](LICENSE.txt) for more information.
154 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "insenseanalytics/laravel-nova-permission",
3 | "description": "A Laravel Nova tool for Spatie's Permission library.",
4 | "keywords": [
5 | "laravel",
6 | "nova",
7 | "spatie",
8 | "laravel-permission"
9 | ],
10 | "license": "MIT",
11 | "require": {
12 | "php": ">=7.1.0",
13 | "spatie/laravel-permission": "^2.16|^3.0"
14 | },
15 | "autoload": {
16 | "psr-4": {
17 | "Insenseanalytics\\LaravelNovaPermission\\": "src/"
18 | }
19 | },
20 | "extra": {
21 | "laravel": {
22 | "providers": [
23 | "Insenseanalytics\\LaravelNovaPermission\\NovaPermissionServiceProvider"
24 | ]
25 | }
26 | },
27 | "config": {
28 | "sort-packages": true
29 | },
30 | "minimum-stability": "dev",
31 | "prefer-stable": true
32 | }
--------------------------------------------------------------------------------
/resources/lang/en/navigation.php:
--------------------------------------------------------------------------------
1 | 'Roles & Permissions',
5 | ];
--------------------------------------------------------------------------------
/resources/lang/en/permissions.php:
--------------------------------------------------------------------------------
1 | 'Name',
5 | 'display_name' => 'Display Name',
6 | 'guard_name' => 'Guard Name',
7 | 'created_at' => 'Created at',
8 | 'updated_at' => 'Updated at',
9 | ];
--------------------------------------------------------------------------------
/resources/lang/en/resources.php:
--------------------------------------------------------------------------------
1 | 'Roles',
5 | 'Role' => 'Role',
6 | 'Permissions' => 'Permissions',
7 | 'Permission' => 'Permission',
8 | ];
--------------------------------------------------------------------------------
/resources/lang/en/roles.php:
--------------------------------------------------------------------------------
1 | 'Name',
5 | 'guard_name' => 'Guard Name',
6 | 'created_at' => 'Created at',
7 | 'updated_at' => 'Updated at',
8 | ];
--------------------------------------------------------------------------------
/resources/views/navigation.blade.php:
--------------------------------------------------------------------------------
1 |
5 |
6 | @if ((Nova::resourceForModel(app(PermissionRegistrar::class)->getRoleClass()))::authorizedToViewAny(request()) || (Nova::resourceForModel(app(PermissionRegistrar::class)->getPermissionClass()))::authorizedToViewAny(request()))
7 |