An example of RBAC permission management operation implemented by PHP Laravel5

Posted by Rangel on Mon, 18 Nov 2019 16:16:25 +0100

According to different permissions, different functions are displayed in the menu bar, only the menu is limited. If the route is also limited, please improve and develop according to the example of the menu. Next, please study carefully the RBAC design of laravel

1. Create table (user table, role table, permission table, user role table, role permission table)

 1 CREATE TABLE IF NOT EXISTS mr_role
 2 (
 3 id int(11) PRIMARY KEY AUTO_INCREMENT COMMENT 'Self increment id',
 4 name varchar(30) NOT NULL COMMENT 'Role name'
 5 )ENGINE=innodb DEFAULT CHARSET=utf8 COMMENT='Role table';
 6 
 7 
 8 CREATE TABLE IF NOT EXISTS mr_privilege
 9 (
10 id int(11) PRIMARY KEY AUTO_INCREMENT COMMENT 'Self increment id',
11 name varchar(30) NOT NULL COMMENT 'Permission name',
12 route varchar(50) NOT NULL COMMENT 'Permission all routes',
13 description varchar(100) NOT NULL COMMENT 'Description of permissions'
14 )ENGINE=innodb DEFAULT CHARSET=utf8 COMMENT='Permission table';
15 
16 
17 CREATE TABLE IF NOT EXISTS mr_user_role
18 (
19 id int(11) PRIMARY KEY AUTO_INCREMENT COMMENT 'Self increment id',
20 user_id int(11) NOT NULL COMMENT 'user id',
21 role_id int(11) NOT NULL COMMENT 'role id'
22 )ENGINE=innodb DEFAULT CHARSET=utf8 COMMENT='User role table';
23 
24 
25 CREATE TABLE IF NOT EXISTS mr_role_privilege
26 (
27 id int(11) PRIMARY KEY AUTO_INCREMENT COMMENT 'Self increment id',
28 role_id int(11) NOT NULL COMMENT 'role id',
29 privilege_id int(11) NOT NULL COMMENT 'Jurisdiction id'
30 )ENGINE=innodb DEFAULT CHARSET=utf8 COMMENT='Role permission table';

 

2. Implement many to many in user model and role model

 1 class User extends Model
 2 {
 3   protected $primaryKey = 'id';
 4   protected $table = 'user';
 5   public $timestamps = false;
 6   public $guarded = [];
 7   public function roles()
 8   {
 9     return $this->belongsToMany('App\Model\Role', 'user_role', 'user_id', 'role_id')->withPivot('user_id', 'role_id');
10   }
11 }
12 
13  
14 
15 class Role extends Model
16 {
17   protected $table = 'role';
18   protected $primaryKey = 'id';
19   public $timestamps = false;
20   public $guarded = [];
21   public function privileges()
22   {
23     return $this->belongsToMany('App\Model\Privilege', 'role_privilege', 'role_id', 'privilege_id')->withPivot(['role_id', 'privilege_id']);
24   }

 

}

 

3. Treat the menu as a public area and write in app\Providers\AppServiceProvider.php

 1 public function boot()
 2 {
 3     \View::composer('layout.slide', function($view) {
 4       $roles_id = User::find(session('user')['id'])->roles->map(function ($role) {
 5         return $role->id;
 6       });  // Use map,The final result $roles_id = [1, 2, ...]
 7       $privileges = [];
 8       foreach ($roles_id as $role) {
 9         $privileges = array_merge($privileges, Role::find($role)->privileges->map(function ($privilege) {
10           return [$privilege->name, $privilege->route];
11         })->toArray());
12       }  // The results, $prpvileges = [['index/..', 'list'], ['', '']]
13       $view->with('privileges', $privileges);
14     });
15 }

 

 

4. Implementation of the menu (you can directly traverse a div, because I have different styles here, I use judgment)

 1 @foreach ($privileges as $privilege)
 2       @if ($privilege[1] == 'key/index' && $privilege[0] == 'List of key names')
 3         <div class="slide__left__key" style="margin-top: 10px;"><a href="{{ url('key/index') }}" rel="external nofollow" ><span class="glyphicon glyphicon-th"></span> List of key names</a></div>
 4       @endif
 5       @if ($privilege[1] == 'key/create' && $privilege[0] == 'Add key name')
 6           <div class="slide__left__key"><a href="{{ url('key/create') }}" rel="external nofollow" ><span class="glyphicon glyphicon-plus"></span> Add key name</a></div>
 7       @endif
 8       @if ($privilege[1] == 'project/index' && $privilege[0] == 'List of items')
 9           <div class="slide__left__key" style="margin-top: 20px;"><a href="{{ url('project/index') }}" rel="external nofollow" ><span class="glyphicon glyphicon-th-list"></span> List of items</a></div>
10       @endif
11       @if ($privilege[1] == 'project/create' && $privilege[0] == 'add item')
12           <div class="slide__left__key"><a href="{{ url('project/create') }}" rel="external nofollow" ><span class="glyphicon glyphicon-edit"></span> add item</a></div>
13       @endif
14       @if ($privilege[1] == 'user/index' && $privilege[0] == 'User list')
15           <div class="slide__left__key" style="margin-top: 20px;"><a href="{{ url('user/index') }}" rel="external nofollow" ><span class="glyphicon glyphicon-th-large"></span> User list</a></div>
16       @endif
17       @if ($privilege[1] == 'user/create' && $privilege[0] == 'Add user')
18           <div class="slide__left__key"><a href="{{ url('user/create') }}" rel="external nofollow" ><span class="glyphicon glyphicon-plus-sign"></span> Add user</a></div>
19       @endif
20 @endforeach

 

Topics: PHP Laravel Session