Polyscope - The agent-first dev environment for Laravel

Nova Permission

vyuldashev/nova-permission image

Nova Permission stats

Downloads
1.7M
Stars
397
Open Issues
42
Forks
205

View on GitHub →

A Laravel Nova tool for Spatie's Permission library.

A Laravel Nova tool for Spatie's laravel-permission library

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.

vyuldashev photo

Passionate Developer.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Vyuldashev Nova Permission Related Articles

Useful Laravel Packages Video Course (sponsor) image

Useful Laravel Packages Video Course (sponsor)

Read article
Subdomain Multitenency Package for Laravel image

Subdomain Multitenency Package for Laravel

Read article
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech