Skip to content
A Laravel Nova resource tool for spatie/laravel-permission.
PHP Vue HTML JavaScript
Branch: master
Clone or download
2 authors and faustbrian Update dependency laravel-mix to v5 (#5)

Co-authored-by: Renovate Bot <bot@renovateapp.com>
Latest commit 5549f30 Sep 28, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist
resources chore: new icon Jul 14, 2019
src fix: show a computed name if no group property is available Jul 15, 2019
.editorconfig first commit Jul 14, 2019
.gitattributes first commit Jul 14, 2019
.gitignore first commit Jul 14, 2019
.php_cs first commit Jul 14, 2019
LICENSE first commit Jul 14, 2019
README.md docs(readme): invalid tool import Jul 14, 2019
composer.json laravel 6 support Sep 11, 2019
package.json Update dependency laravel-mix to v5 (#5) Sep 28, 2019
renovate.json Update renovate.json Sep 28, 2019
webpack.mix.js
yarn.lock Update dependency laravel-mix to v5 (#5) Sep 28, 2019

README.md

Laravel Nova - Permission

Latest Version Build Status Codecov License: MPL-2.0

Installation

Require this package, with Composer, in the root directory of your project.

composer require artisanry/nova-permission

Next you'll need to register the tool with Nova. This is done in the tools method of the NovaServiceProvider.

<?php

namespace App\Providers;

use Artisanry\NovaPermission\NovaPermissionTool;
use Laravel\Nova\Cards\Help;
use Laravel\Nova\Nova;
use Laravel\Nova\NovaApplicationServiceProvider;

class NovaServiceProvider extends NovaApplicationServiceProvider
{
    // ...

    public function tools()
    {
        return [
            (new NovaPermissionTool())->canSee(function ($request) {
                return $request->user()->hasRole('super-admin');
            }),
        ];
    }

    // ...
}

Next you'll need to add the middleware to the middleware list in config/nova.php.

<?php

return [

    // ...

    'middleware' => [
        'web',
        Authenticate::class,
        DispatchServingNovaEvent::class,
        BootTools::class,
        Authorize::class,
        // ...
        \Artisanry\NovaPermission\ForgetCachedPermissions::class,
    ],

    // ...

];

Usage with Models

The Artisanry\NovaPermission\Fields\Permissions field can be used with any model that uses the Spatie\Permission\Traits\HasRoles trait.

It will automatically load permissions from a model that uses this trait and render them inside the Nova UI based on your configured preferences.

Preparing your User model and resource

If you wish to manage permissions directly on a user resource you will have to apply the same code to the User model as you did to the Role model and then add below code to the fields of your User resource.

use Artisanry\NovaPermission\Fields\Permissions;
use Artisanry\NovaPermission\Helpers;
use Laravel\Nova\Fields\MorphToMany;

Permissions::make('Permissions'),

MorphToMany::make('Permissions', 'permissions', \Artisanry\NovaPermission\Resources\Permission::class),

Drivers

Artisanry\NovaPermission\Drivers\Name (Default)

Used with Permissions::make('Permissions')->fromNameWithGroup() and Permissions::make('Permissions')->fromNameWithoutGroup().

The Name driver will determine the group based on the name of the permission.

If you have a permission called view posts it will assume that posts is the group of the permission.

Artisanry\NovaPermission\Drivers\Eloquent

Used with Permissions::make('Permissions')->fromEloquentWithGroup() and Permissions::make('Permissions')->fromEloquentWithoutGroup().

The Eloquent driver will determine the group based on group attribute of the model.

Preparing the permissions table

php artisan make:migration add_group_to_permissions_table --table=permissions
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddGroupToPermissionsTable extends Migration
{
    /**
     * Run the migrations.
     */
    public function up()
    {
        Schema::table('permissions', function (Blueprint $table) {
            $table->string('group');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down()
    {
        Schema::table('permissions', function (Blueprint $table) {
            $table->dropColumn('group');
        });
    }
}

Testing

phpunit

Security

If you discover a security vulnerability within this package, please send an e-mail to hello@basecode.sh. All security vulnerabilities will be promptly addressed.

Credits

This project exists thanks to all the people who contribute.

License

Mozilla Public License Version 2.0 (MPL-2.0).

You can’t perform that action at this time.