Skip to content

Nova Shield simplifies permission management for your Laravel Nova resources using Spatie Permission. Easily grant or revoke access to specific resources and actions, streamlining your workflow and improving security.

License

Notifications You must be signed in to change notification settings

operativeit/nova-shield

 
 

Repository files navigation

Nova Shield - WIP

Nova Shield simplifies permission management for your Laravel Nova resources using Spatie Permission. Easily grant or revoke access to specific resources and actions, streamlining your workflow and improving security.

Installation

composer require ferdiunal/nova-shield

Configuration

Optionally, you can publish the configuration file using this command:

php artisan vendor:publish --tag nova-shield-config

In the config file, you can control resource permissions by specifying the resources parameter with either the path to the Nova resources folder or a string class path.

For example, in the code below, we define the resources and their corresponding policies:

<?php

return [
    "resources" => [
        // Nova resources folder path
        app_path("Nova"),
        // Or a string class path
        \Ferdiunal\NovaShield\Http\Nova\ShieldResource::class,
        // Custom resource: For custom menu items
        // [
        //     "name" => "Custom Menu Item",
        //     "policies" => ['CustomMenuPolicy'] // Add custom menu policies here
        // ]
    ],
    "policies" => [
        // List of policies for the resources
        "viewAny",
        "view",
        "create",
        "update",
        "replicate",
        "delete",
        "restore",
        "forceDelete",
        "runAction",
        "runDestructiveAction",
        "canImpersonate",
        "canBeImpersonated",
        "add{Model}",
        "attach{Model}",
        "attachAny{Model}",
        "detach{Model}",
    ],
    "langs" => [
        // lang_path('en/nova-shield.json'),
        // base_path('langs/en/nova-shield.json'),
    ]
];

Custom Menu Configuration

The main purpose of the package was to manage permissions for Nova Resources, but I realized there was a need for Custom Menu support as well. The necessary development has been completed. You can refer to the usage below for implementing Custom Menus.

    // app/Providers/NovaServiceProvider.php
    Nova::mainMenu(function (Request $request) {
        return [
            MenuItem::make('Custom Menu Item')
                ->path('/custom-menu')
                ->canSee(
                    function ($request) {
                        return $request->user()->can('CustomMenuPolicy');
                    }
                )
        ];
    });

    // ----

    // config/nova-shield.php
    return [
        "resources" => [
            ...
            // Custom resource: For custom menu items
            [
                "name" => "Custom Menu Item",
                "prefix" => "customMenuItem::",
                "policies" => ['CustomMenuPolicy'] // Add custom menu policies here
            ]
        ]
    ];

Then edit App\Nova\Resource.php file as follows.

<?php

namespace App\Nova;

use Ferdiunal\NovaShield\PermissionAuthorizable;
use Laravel\Nova\Resource as NovaResource;

abstract class Resource extends NovaResource
{
    use PermissionAuthorizable;
    ....
}

Teams Support

I assume you have done the Teams integration for Spatie Permission and you can follow the setup below for NovaShield.

Model Integrations

Teams feature differs in each project, please fill in the relevant method according to your project.

// App\Models\User

use Ferdiunal\NovaShield\Contracts\HasShieldTeam;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements HasShieldTeam
{
    public function getTeamIdAttribute(): mixed
    {
        // return $this->team_id
    }
}

Add Team Field

Considering that the Teams feature will differ in each project, you need to add the team field yourself.

namespace App\Lib;

use Ferdiunal\NovaShield\Lib\NovaTeamHelperField;
use Laravel\Nova\Fields\Field;
use Laravel\Nova\Fields\Select;

class TeamField extends NovaTeamHelperField
{
    public static function field(): Field
    {
        return Select::make('Teams', 'team_id')
            ->options([])
            ->displayUsingLabels()
            ->searchable()
            ->sortable();
    }
}

Add Middleware to Nova Routes

Add TeamMiddleware to nova routes

return [

    /*
    |--------------------------------------------------------------------------
    | Nova Route Middleware
    |--------------------------------------------------------------------------
    |
    | These middleware will be assigned to every Nova route, giving you the
    | chance to add your own middleware to this stack or override any of
    | the existing middleware. Or, you can just stick with this stack.
    |
    */

    'middleware' => [
        ....
        \Ferdiunal\NovaShield\Http\Middleware\TeamMiddleware::class,
    ],
];

ScreenShots

License This package is open-sourced software licensed under the MIT license.

About

Nova Shield simplifies permission management for your Laravel Nova resources using Spatie Permission. Easily grant or revoke access to specific resources and actions, streamlining your workflow and improving security.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 65.3%
  • Vue 29.8%
  • JavaScript 4.8%
  • CSS 0.1%