Laravel Community Tools by Tighten
Laravel Nova icon Nova Packages
N

Tool for Spatie's Permission library

Readme

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

Latest Version on Packagist Total Downloads

screenshot 1

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.

Reviews

By Lukas Guretzke on December 2, 2022

Works great! Thank your for this super tool!

By Lloric Mayuga Garcia on October 14, 2021

Grreat, but still waiting for spatie permission v5 support

By Bryan Ingram on July 3, 2021

Installed and played around with this last night. It's well written, great package for situations where your role and permissions models are fairly simple. The UI for listing permissions and role seems to stack vertically, meaning if you have more than a dozen or so items for each things could get messy. Maybe some vue tweaking would help. Overall, fantastic package!

By Ricky Grimaldi on June 8, 2021

Really useful tool. NOTE - You'll need to implement the step from the comment below by BIRDY-DANIEL to get this working.

By Mateus Galasso on May 20, 2021

Making my life easier

By peanut24 on February 23, 2021

The go to package for managing spatie roles and permissions, use it on almost every project. Thanks :)

By BIRDY-DANIEL on June 19, 2020

Very good package, all is excellent! Thanks a lot, LOVE IT!!! ... after reviewing some information, you MUST ADD IN THE USER MODEL - This code is from spatie permission so IS BEFORE This Nova Package... that's why is not included okay: add use Spatie\Permission\Traits\HasRoles; then inside the: class User extends Authenticatable { use HasRoles; // That's it nothing else. this way my errors were solved, hope this helps another person

By Mohamed Zayed on March 25, 2020

Thanks For Your Efforts ;)

By Inode srl on March 25, 2020

Very useful !

By Eugene van der Merwe on February 26, 2020

This package is exceptional, and what I would consider the gold standard of Laravel Nova and permissions. Behind the scenes it uses Spatie which makes some of the greatest software on the internet. What's really re-assuring is I stepped away from six months and six months later I can see how this package has evolved. Really exceptional software that makes implementing roles and permissions, structured, organized, and fun. Extremely highly recommended.

By Daniel Schweiger on January 13, 2020

thank you man, helped me out a lot :)

Back to Top
Added 7 years ago
Last updated 3 Years Ago
Version v3.2.0
Nova Version ^4.0
Composer
vyuldashev/nova-permission
GitHub stars 429
Packagist downloads 2,505,599

Favorites

79 users favorited

Rating

4.70
(out of 5)
★★★★★
★★★★
★★★
★★
55 ratings

Brought to you by Tighten

Issues/Feature Requests Stats Package Ideas