Role Based Permission in Laravel - Zizaco Entrust Package

ROLE BASED PERMISSION IN LARAVEL - ZIZACO ENTRUST

In this tutorial we are going to learn how to use zizaco entrust package in laravel for controlling role management .

  1. In order to install Laravel 5 Entrust, just add the following to your composer.json. Then run composer update:
"zizaco/entrust": "5.2.x-dev"
  1. Open your config/app.php and add the following to the providers array:
Zizaco\Entrust\EntrustServiceProvider::class,
  1. In the same config/app.php and add the following to the aliases array:
'Entrust'   => Zizaco\Entrust\EntrustFacade::class,
  1. Run the command below to publish the package config file config/entrust.php:
php artisan vendor:publish
  1. Open your config/auth.php and add the following to it:
'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => Namespace\Of\Your\User\Model\User::class,
        'table' => 'users',
    ],
],
  1. If you want to use Middleware (requires Laravel 5.1 or later) you also need to add the following:
    'role' => \Zizaco\Entrust\Middleware\EntrustRole::class,
    'permission' => \Zizaco\Entrust\Middleware\EntrustPermission::class,
    'ability' => \Zizaco\Entrust\Middleware\EntrustAbility::class,

Now generate the Entrust migration:
php artisan entrust:migration
It will generate the <timestamp>_entrust_setup_tables.php migration. You may now run it with the artisan migrate command:
php artisan migrate
Upto This i refereed Github about this package.
https://www.youtube.com/watch?v=ucG3NOKBe_8 
For creating views and install package use the above link for your reference ,
Now i'm going to tell you what i learned in this package
You have to write code like below for restrict users in views
 @permission(['add-role','edit-role','delete-role'])
       <li><a href="{{url('roles') }}"><i class="fa fa-circle-o"></i>List of Roles</a></li>
@endrole
@permission(['user-view','user-edit/track','user-create'])
      <li><a href="{{ url('users') }}"><i class="fa fa-circle-o"></i>Users</a></li>
@endrole
 You can restrict the url in middleware like below
function __construct()
     {
        $this->middleware('auth');
        $this->middleware('permission:show-client')->only('index');
        $this->middleware('permission:add-client')->only('create');
     }
Permission is mypackage , show-client is in my database and index is my function ,
what this do is it will check the Auth users permissions and based on that it will execute the function , if it not there it will show the laravel error message of this you have to add error blade in errors folder.
Hope it is useful for you ... If any doubt about this make a comment..
Role Based Permission in Laravel - Zizaco Entrust Package Role Based Permission in Laravel - Zizaco Entrust Package Reviewed by AD on December 02, 2017 Rating: 5

No comments:

Powered by Blogger.