Skip to content

Commit 6a4b882

Browse files
Added license and readme
1 parent 0d69451 commit 6a4b882

File tree

3 files changed

+130
-1
lines changed

3 files changed

+130
-1
lines changed

LICENSE.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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.

README.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# A Laravel Nova tool for the Spatie Permission library
2+
3+
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/insenseanalytics/laravel-nova-permission/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/insenseanalytics/laravel-nova-permission/?branch=master)
4+
![GitHub](https://img.shields.io/github/license/insenseanalytics/laravel-nova-permission.svg)
5+
[![Code Intelligence Status](https://scrutinizer-ci.com/g/insenseanalytics/laravel-nova-permission/badges/code-intelligence.svg?b=master)](https://scrutinizer-ci.com/code-intelligence)
6+
![Packagist](https://img.shields.io/packagist/dt/insenseanalytics/laravel-nova-permission.svg)
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.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"extra": {
2121
"laravel": {
2222
"providers": [
23-
"Insenseanalytics\\LaravelNovaPermission\\ToolServiceProvider"
23+
"Insenseanalytics\\LaravelNovaPermission\\NovaPermissionServiceProvider"
2424
]
2525
}
2626
},

0 commit comments

Comments
 (0)