Skip to content

Commit 0d69451

Browse files
v0.1
0 parents  commit 0d69451

11 files changed

+621
-0
lines changed

.gitignore

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

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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"
14+
},
15+
"autoload": {
16+
"psr-4": {
17+
"Insenseanalytics\\LaravelNovaPermission\\": "src/"
18+
}
19+
},
20+
"extra": {
21+
"laravel": {
22+
"providers": [
23+
"Insenseanalytics\\LaravelNovaPermission\\ToolServiceProvider"
24+
]
25+
}
26+
},
27+
"config": {
28+
"sort-packages": true
29+
},
30+
"minimum-stability": "dev",
31+
"prefer-stable": true
32+
}

resources/views/navigation.blade.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
use Laravel\Nova\Nova;
3+
use Spatie\Permission\PermissionRegistrar;
4+
?>
5+
6+
@if ((Nova::resourceForModel(app(PermissionRegistrar::class)->getRoleClass()))::authorizedToViewAny(request()) || (Nova::resourceForModel(app(PermissionRegistrar::class)->getPermissionClass()))::authorizedToViewAny(request()))
7+
<h3 class="flex items-center font-normal text-white mb-6 text-base no-underline">
8+
<svg class="sidebar-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
9+
<path fill="var(--sidebar-icon)"
10+
d="M7 10V7a5 5 0 1 1 10 0v3h2a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-8c0-1.1.9-2 2-2h2zm2 0h6V7a3 3 0 0 0-6 0v3zm-4 2v8h14v-8H5zm7 2a1 1 0 0 1 1 1v2a1 1 0 0 1-2 0v-2a1 1 0 0 1 1-1z"/>
11+
</svg>
12+
<span class="sidebar-label">
13+
Roles & Permissions
14+
</span>
15+
</h3>
16+
17+
<ul class="list-reset mb-8">
18+
19+
@if((Nova::resourceForModel(app(PermissionRegistrar::class)->getRoleClass()))::authorizedToViewAny(request()))
20+
<li class="leading-wide mb-4 text-sm">
21+
<router-link :to="{
22+
name: 'index',
23+
params: {
24+
resourceName: 'roles'
25+
}
26+
}" class="text-white ml-8 no-underline dim">
27+
Roles
28+
</router-link>
29+
</li>
30+
@endif
31+
32+
@if((Nova::resourceForModel(app(PermissionRegistrar::class)->getPermissionClass()))::authorizedToViewAny(request()))
33+
<li class="leading-wide mb-4 text-sm">
34+
<router-link :to="{
35+
name: 'index',
36+
params: {
37+
resourceName: 'permissions'
38+
}
39+
}" class="text-white ml-8 no-underline dim">
40+
Permissions
41+
</router-link>
42+
</li>
43+
@endif
44+
45+
</ul>
46+
@endif

src/ForgetCachedPermissions.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Insenseanalytics\LaravelNovaPermission;
4+
5+
use Laravel\Nova\Nova;
6+
use Spatie\Permission\PermissionRegistrar;
7+
8+
class ForgetCachedPermissions
9+
{
10+
/**
11+
* Handle the incoming request.
12+
*
13+
* @param \Illuminate\Http\Request $request
14+
* @param \Closure $next
15+
*
16+
* @return \Illuminate\Http\Response
17+
*/
18+
public function handle($request, $next)
19+
{
20+
$response = $next($request);
21+
22+
if ($request->is('nova-api/*/detach') ||
23+
$request->is('nova-api/*/*/attach/*')) {
24+
$permissionKey = (Nova::resourceForModel(app(PermissionRegistrar::class)->getPermissionClass()))::uriKey();
25+
26+
if ($request->viaRelationship === $permissionKey) {
27+
app(PermissionRegistrar::class)->forgetCachedPermissions();
28+
}
29+
}
30+
31+
return $response;
32+
}
33+
}

src/LaravelNovaPermission.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace Insenseanalytics\LaravelNovaPermission;
4+
5+
use Laravel\Nova\Tool;
6+
use Laravel\Nova\Nova;
7+
8+
class LaravelNovaPermission extends Tool
9+
{
10+
public $roleResource = Role::class;
11+
public $permissionResource = Permission::class;
12+
13+
public $registerCustomResources = false;
14+
15+
/**
16+
* Perform any tasks that need to happen when the tool is booted.
17+
*/
18+
public function boot()
19+
{
20+
if ((Role::class === $this->roleResource && Permission::class === $this->permissionResource)
21+
|| $this->registerCustomResources) {
22+
Nova::resources([
23+
$this->roleResource,
24+
$this->permissionResource,
25+
]);
26+
}
27+
}
28+
29+
/**
30+
* Set a custom Role resource class.
31+
*
32+
* @param Role resource class
33+
*
34+
* @return $this
35+
*/
36+
public function roleResource(string $roleResource)
37+
{
38+
$this->roleResource = $roleResource;
39+
40+
return $this;
41+
}
42+
43+
/**
44+
* Set a custom Permission resource class.
45+
*
46+
* @param Permission resource class
47+
*
48+
* @return $this
49+
*/
50+
public function permissionResource(string $permissionResource)
51+
{
52+
$this->permissionResource = $permissionResource;
53+
54+
return $this;
55+
}
56+
57+
/**
58+
* Register the custom resource classes.
59+
*
60+
* @param bool
61+
*
62+
* @return $this
63+
*/
64+
public function withRegistration()
65+
{
66+
$this->registerCustomResources = true;
67+
68+
return $this;
69+
}
70+
71+
/**
72+
* Build the view that renders the navigation links for the tool.
73+
*
74+
* @return \Illuminate\View\View
75+
*/
76+
public function renderNavigation()
77+
{
78+
return view('laravel-nova-permission::navigation');
79+
}
80+
}

src/NovaPermissionServiceProvider.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Insenseanalytics\LaravelNovaPermission;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class NovaPermissionServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Bootstrap any application services.
11+
*/
12+
public function boot()
13+
{
14+
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'laravel-nova-permission');
15+
}
16+
17+
/**
18+
* Register any application services.
19+
*/
20+
public function register()
21+
{
22+
}
23+
}

src/Permission.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Insenseanalytics\LaravelNovaPermission;
4+
5+
use Laravel\Nova\Resource;
6+
use Spatie\Permission\Models\Permission as SpatiePermission;
7+
8+
class Permission extends Resource
9+
{
10+
use PermissionResourceTrait;
11+
12+
/**
13+
* The model the resource corresponds to.
14+
*
15+
* @var string
16+
*/
17+
public static $model = SpatiePermission::class;
18+
19+
/**
20+
* The single value that should be used to represent the resource when being displayed.
21+
*
22+
* @var string
23+
*/
24+
public static $title = 'name';
25+
26+
/**
27+
* The columns that should be searched.
28+
*
29+
* @var array
30+
*/
31+
public static $search = [
32+
'name',
33+
];
34+
35+
/**
36+
* Indicates if the resource should be displayed in the sidebar.
37+
*
38+
* @var bool
39+
*/
40+
public static $displayInNavigation = false;
41+
}

src/PermissionResourceTrait.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Insenseanalytics\LaravelNovaPermission;
4+
5+
use Laravel\Nova\Nova;
6+
use Laravel\Nova\Fields\ID;
7+
use Illuminate\Http\Request;
8+
use Laravel\Nova\Fields\Text;
9+
use Illuminate\Validation\Rule;
10+
use Laravel\Nova\Fields\Select;
11+
use Laravel\Nova\Fields\DateTime;
12+
use Laravel\Nova\Fields\MorphToMany;
13+
use Laravel\Nova\Fields\BelongsToMany;
14+
use Spatie\Permission\PermissionRegistrar;
15+
16+
trait PermissionResourceTrait
17+
{
18+
public static function getModel()
19+
{
20+
return app(PermissionRegistrar::class)->getPermissionClass();
21+
}
22+
23+
/**
24+
* Get the fields displayed by the resource.
25+
*
26+
* @param \Illuminate\Http\Request $request
27+
*
28+
* @return array
29+
*/
30+
public function fields(Request $request)
31+
{
32+
$guardOptions = collect(config('auth.guards'))->mapWithKeys(function ($value, $key) {
33+
return [$key => $key];
34+
});
35+
36+
$userResource = Nova::resourceForModel(getModelForGuard($this->guard_name));
37+
38+
$roleResource = Nova::resourceForModel(app(PermissionRegistrar::class)->getRoleClass());
39+
40+
return [
41+
ID::make()->sortable(),
42+
43+
Text::make('Name', 'name')
44+
->rules(['required', 'string', 'max:255'])
45+
->creationRules('unique:' . config('permission.table_names.permissions'))
46+
->updateRules('unique:' . config('permission.table_names.permissions') . ',name,{{resourceId}}'),
47+
48+
Select::make('Guard Name', 'guard_name')
49+
->options($guardOptions->toArray())
50+
->rules(['required', Rule::in($guardOptions)]),
51+
52+
DateTime::make('Created At', 'created_at')->exceptOnForms(),
53+
54+
DateTime::make('Updated At', 'updated_at')->exceptOnForms(),
55+
56+
BelongsToMany::make($roleResource::label(), 'roles', $roleResource)->searchable(),
57+
58+
MorphToMany::make($userResource::label(), 'users', $userResource)->searchable(),
59+
];
60+
}
61+
}

0 commit comments

Comments
 (0)