Giter Club home page Giter Club logo

laravel-role-manager's Introduction

Laravel Role Manager

Installation

This package can be used in Laravel 5.4 or higher. You can install the package via composer:

composer require 4msar/laravel-role-manager

In Laravel 5.5 the service provider will automatically get registered. In older versions of the framework just add the service provider in config/app.php file:

'providers' => [
    // ...
    MSAR\RoleManager\RoleManagerServiceProvider::class,
];

You can publish the migrations & config file with:

php artisan vendor:publish --provider="MSAR\RoleManager\RoleManagerServiceProvider"

After the migration has been published you can create the role- and permission-tables by running the migrations:

php artisan migrate

When published, the config/role_manager.php config file contains:

return [

    /**
    * Permission List 
    * Here, you can declare your needed permission, the package will be add this permission automatically
    * to the database( via middle-ware).
    * key name is the permission name and value is the details name showing for management.
    */
    "add_permission_dynamically" => false, // Make true if you want to add permission from bellow permissions array
    "permissions" => [
        // 'permission_name' => 'permission_details'
        'view_home' => 'View Admin Dashboard',
        'view_users' => 'View Admin Users',
    ],

    'unauthorized_action' => '',

    'unauthorized_action_type' =>  [
        'route' => [
            'name' => 'welcome',
            'data' => [],
        ],
        'url' => [
            'name' => '/login',
            'data' => [],
        ],
        'abort' => [
            'type' => '403',
        ],
        'dump' => [
            'data' => 'Dumping a unauthorized message.',
        ]
    ],

    "database" => [
        "role_table" => "roles",
        "permission_table" => "permissions",
        "role_permission_table" => "permission_role",
    ],
];

Usage

First, add the MSAR\RoleManager\Traits\HasPermission; trait to your User model(s):

use Illuminate\Foundation\Auth\User as Authenticatable;
use MSAR\RoleManager\Traits\HasPermission;;

class User extends Authenticatable
{
    use HasPermission;

    // ...
}

Then, add the \MSAR\RoleManager\Middlewares\RoleMiddleware::class middlwware to your Karnel.php :

// .....
'has_permission' => \MSAR\RoleManager\Middlewares\RoleMiddleware::class,

NB: You have to add a column to your users table name is role and the users role is save to this column

This package allows for users to be associated with permissions and roles. Every role is associated with multiple permissions. A Role and a Permission are regular Eloquent models. They require a name and can be created like this:

use MSAR\RoleManager\Models\Role;
use MSAR\RoleManager\Models\Permission;

$role = Role::create(['name' => 'writer']);
$permission = Permission::create(['name' => 'edit_articles', 'title' => 'Edit Articles']);

To create permission you can use the config/role_manager.php file and the permissions array. just make the config value add_permission_dynamically to true then add permission to the array(permissions).

For updating role and permission from front-end, use the code bellow

$role = \MSAR\RoleManager\Models\Role::where('name', 'Admin')->first();
$permissions = $request->permissions; // [1,2] | checkbox array input | permissions ids
$role->permissions()->sync($permissions);

For check the user has permission or not use the middleware to the route

Route::get('/users', 'UserController@index')->middleware('has_permission:view_users')->name('home');

You can use multiple permission by using the pipe(|) eg: has_permission:view_users|update_users

Use Blade Directives You can use those blade extension Has Role : Has Role

@hasrole(role)
@elsehasrole(another_role)
@endhasrole

Has Permission : User can

@ucan(permission)
@elseucan(another_permission)
@enducan

Use Helper Functions

rmHasRole($role); // pipe string | array | string
rmHasPermission($permission); // pipe string | array | string

Todo

  • Improve code base
  • Add Command to add a role or assign permission

laravel-role-manager's People

Contributors

4msar avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.