All Projects → pktharindu → nova-permissions

pktharindu / nova-permissions

Licence: MIT license
Add Permissions based authorization for your Nova installation via User-based Roles and Permissions. Roles are defined in the database whereas Permissions are defined in the code base.

Programming Languages

PHP
23972 projects - #3 most used programming language
Vue
7211 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to nova-permissions

Accesscontrol
Role and Attribute based Access Control for Node.js
Stars: ✭ 1,723 (+1398.26%)
Mutual labels:  permissions, acl, roles, authorization, access-control
Think Authz
An authorization library that supports access control models like ACL, RBAC, ABAC in ThinkPHP 6.0 .
Stars: ✭ 155 (+34.78%)
Mutual labels:  permissions, acl, roles, authorization, access-control
rbac-tool
Rapid7 | insightCloudSec | Kubernetes RBAC Power Toys - Visualize, Analyze, Generate & Query
Stars: ✭ 546 (+374.78%)
Mutual labels:  permissions, acl, authorization, access-control
Php Casbin
An authorization library that supports access control models like ACL, RBAC, ABAC in PHP .
Stars: ✭ 865 (+652.17%)
Mutual labels:  acl, roles, authorization, access-control
Vakt
Attribute-based access control (ABAC) SDK for Python
Stars: ✭ 92 (-20%)
Mutual labels:  permissions, acl, authorization, access-control
Brandenburg
Laravel Authentication Package
Stars: ✭ 79 (-31.3%)
Mutual labels:  permissions, acl, roles, authorization
Bouncer
Eloquent roles and abilities.
Stars: ✭ 2,763 (+2302.61%)
Mutual labels:  permissions, acl, roles, authorization
Casbin4D
An authorization library that supports access control models like ACL, RBAC, ABAC in Delphi
Stars: ✭ 25 (-78.26%)
Mutual labels:  permissions, acl, authorization, access-control
Ngx Permissions
Permission and roles based access control for your angular(angular 2,4,5,6,7,9+) applications(AOT, lazy modules compatible
Stars: ✭ 749 (+551.3%)
Mutual labels:  permissions, acl, roles, access-control
Laravel Authz
An authorization library that supports access control models like ACL, RBAC, ABAC in Laravel.
Stars: ✭ 136 (+18.26%)
Mutual labels:  permissions, acl, authorization, access-control
Laratrust
Handle roles and permissions in your Laravel application
Stars: ✭ 1,799 (+1464.35%)
Mutual labels:  permissions, acl, roles, authorization
Simpleacl
Simple ACL for PHP
Stars: ✭ 105 (-8.7%)
Mutual labels:  permissions, acl, authorization
Rbac
Hierarchical Role-Based Access Control for Node.js
Stars: ✭ 254 (+120.87%)
Mutual labels:  permissions, acl, authorization
Sentinel
A framework agnostic authentication & authorization system.
Stars: ✭ 1,354 (+1077.39%)
Mutual labels:  permissions, roles, authorization
Drf Access Policy
Declarative access policies/permissions modeled after AWS' IAM policies.
Stars: ✭ 200 (+73.91%)
Mutual labels:  permissions, authorization, access-control
Adonis Acl
demo app: https://github.com/enniel/adonis-acl-blog-demo
Stars: ✭ 195 (+69.57%)
Mutual labels:  permissions, acl, roles
ngx-access
Add access control to your components using hierarchical configuration with logical expressions.
Stars: ✭ 21 (-81.74%)
Mutual labels:  permissions, roles, access-control
Rbac.dev
A collection of good practices and tools for Kubernetes RBAC
Stars: ✭ 115 (+0%)
Mutual labels:  permissions, authorization, access-control
Unix Permissions
Swiss Army knife for Unix permissions
Stars: ✭ 106 (-7.83%)
Mutual labels:  permissions, acl, access-control
Vue Router User Roles
A Vue.js plugin that protects routes based on user roles. Add your own authentication.
Stars: ✭ 237 (+106.09%)
Mutual labels:  permissions, roles, authorization

Laravel Nova Grouped Permissions (RBAC)

banner that says Nova Permissions

GitHub Packagist Packagist

Add Permissions based authorization for your Nova installation via Role-Based Access Control (RBAC). Roles are defined in the database whereas Permissions are defined in the code base. It allows you to group your Permissions into Groups and attach it to Users.

Nova 4 v3.x
<= Nova 3 v2.x

If you like this package, show some love by starring the repo. 🙏

This package is inspired by Silvanite\Brandenburg as it has clear separation of concerns.

Roles are defined in the Database

and

Permissions are defined in the Codebase

As a result, you won't see any Permissions resource. The Roles resource will get the permissions from the Gates defined in your code.

Tool Demo

Installation

You can install the package in to a Laravel app that uses Nova via composer:

composer require pktharindu/nova-permissions

Publish the Configuration with the following command:

php artisan vendor:publish --provider="Pktharindu\NovaPermissions\ToolServiceProvider" --tag="config"

Configuration file includes some dummy permissions for your refference. Feel free to remove them and add your own permissions.

// in config/nova-permissions.php

<?php

return [
    /*
    |--------------------------------------------------------------------------
    | User model class
    |--------------------------------------------------------------------------
    */

    'user_model' => 'App\User',

    /*
    |--------------------------------------------------------------------------
    | Nova User resource tool class
    |--------------------------------------------------------------------------
    */

    'user_resource' => 'App\Nova\User',

    /*
    |--------------------------------------------------------------------------
    | The group associated with the resource
    |--------------------------------------------------------------------------
    */

    'role_resource_group' => 'Other',

    /*
    |--------------------------------------------------------------------------
    | Database table names
    |--------------------------------------------------------------------------
    | When using the "HasRoles" trait from this package, we need to know which
    | table should be used to retrieve your roles. We have chosen a basic
    | default value but you may easily change it to any table you like.
    */

    'table_names' => [
        'roles' => 'roles',

        'role_permission' => 'role_permission',

        'role_user' => 'role_user',
        
        'users' => 'users',
    ],

    /*
    |--------------------------------------------------------------------------
    | Application Permissions
    |--------------------------------------------------------------------------
    */

    'permissions' => [
        'view users' => [
            'display_name' => 'View users',
            'description'  => 'Can view users',
            'group'        => 'User',
        ],

        'create users' => [
            'display_name' => 'Create users',
            'description'  => 'Can create users',
            'group'        => 'User',
        ],

        // ...
    ],
];

Publish the Migration with the following command:

php artisan vendor:publish --provider="Pktharindu\NovaPermissions\ToolServiceProvider" --tag="migrations"

Migrate the Database:

php artisan migrate

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 [
        // ...
        new \Pktharindu\NovaPermissions\NovaPermissions(),
    ];
}

Create a new policy:

php artisan make:policy RolePolicy --model=\Pktharindu\NovaPermissions\Role

After that, register the RolePolicy along with any other policies you may have and define the gates in the boot method of the AuthServiceProvider like below.

// in app/Providers/AuthServiceProvider.php

use Illuminate\Support\Facades\Gate;
use Pktharindu\NovaPermissions\Traits\ValidatesPermissions;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    use ValidatesPermissions;

    protected $policies = [
        \Pktharindu\NovaPermissions\Role::class => \App\Policies\RolePolicy::class,
    ];

    public function boot()
    {
        $this->registerPolicies();

        foreach (config('nova-permissions.permissions') as $key => $permissions) {
            Gate::define($key, function (User $user) use ($key) {
                if ($this->nobodyHasAccess($key)) {
                    return true;
                }

                return $user->hasPermissionTo($key);
            });
        }
    }
}

Then, use HasRoles Traits in your User model.

// in app/User.php

use Illuminate\Notifications\Notifiable;
use Pktharindu\NovaPermissions\Traits\HasRoles;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasRoles,
        Notifiable;

    // ...
}

Finally, add BelongsToMany fields to you app/Nova/User resource:

use Laravel\Nova\Fields\BelongsToMany;

public function fields(Request $request)
{
    return [
        // ...
        BelongsToMany::make('Roles', 'roles', \Pktharindu\NovaPermissions\Nova\Role::class),
    ];
}

A new resource called Roles will appear in your Nova app after installing this package.

Permissions with Groups

Index View

Detail View

Detail View

Detail View

Edit View

Edit View

Usage

Create a Model Policy

To check permissions, you can create Model Policies that works with Laravel Nova.

Note: This package doesn't come with any Model Policies built-in. The dummy permissions defined in the config are for your reference only. For each Nova Resource including the Role and User resources, that you want to authorize user actions against, you need to create a Model Policy. Please refer to the Laravel Docs and Laravel Nova Docs for additional information.

For Example: Create a new Post Policy with php artisan make:policy PostPolicy with the following code:

<?php

namespace App\Policies;

use App\Post;
use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class PostPolicy
{
    use HandlesAuthorization;

    public function view(User $user, Post $post)
    {
        if ($user->hasPermissionTo('view own posts')) {
            return $user->id === $post->user_id;
        }

        return $user->hasPermissionTo('view posts');
    }

    public function create(User $user)
    {
        return $user->hasAnyPermission(['manage posts', 'manage own posts']);
    }

    public function update(User $user, Post $post)
    {
        if ($user->hasPermissionTo('manage own posts')) {
            return $user->id == $post->user_id;
        }
        return $user->hasPermissionTo('manage posts');
    }

    public function delete(User $user, Post $post)
    {
        if ($user->hasPermissionTo('manage own posts')) {
            return $user->id === $post->user_id;
        }

        return $user->hasPermissionTo('manage posts');
    }
}

It should now work as exptected. Just create a Role, modify its Permissions and the Policy should take care of the rest.

Note: Don't forget to add your Policy to your $policies in App\Providers\AuthServiceProvider and define the permissions in config\nova-permissions.php.

hasPermissionTo() method determine if any of the assigned roles to this user have a specific permission.

hasAnyPermission() method determine if the model has any of the given permissions.

hasAllPermissions() method determine if the model has all of the given permissions.

view own posts is superior to view posts and allows the User to only view his own posts.

manage own posts is superior to manage posts and allows the User to only manage his own posts.

Customization

Use your own Resources

If you want to use your own role resource, you can define it when you register the tool:

// in app/Providers/NovaServiceProvider.php

// ...

use App\Nova\Role;

public function tools()
{
    return [
        // ...
        \Pktharindu\NovaPermissions\NovaPermissions::make()
            ->roleResource(Role::class),
    ];
}

Then extend the Pktharindu\NovaPermissions\Nova\Role in your role resource:

// in app/Nova/Role.php

use Pktharindu\NovaPermissions\Nova\Role as RoleResource;

class Role extends RoleResource
{
    // ...
}

Support

If you require any support please contact me on Twitter or open an issue on this repository.

Credits

This Package is inspired by eminiarts/nova-permissions and silvanite/novatoolpermissions. I wanted to have a combination of both. Thanks to both authors.

License

Copyright © 2018-2020 P. K. Tharindu and contributors

Licensed under the MIT license, see LICENSE for details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].