PHP Laravel5實現的RBAC權限管理操做示例

根據不一樣的權限,在菜單欄顯示不一樣的功能,只對菜單進行了限制,若對路由也進行限制,能夠根據菜單的例子,請自行完善,開發。下面請認真學習一下laravel的RBAC設計php

一、建表(用戶表、角色表、權限表、用戶角色表、角色權限表)laravel

 1 CREATE TABLE IF NOT EXISTS mr_role
 2 (
 3 id int(11) PRIMARY KEY AUTO_INCREMENT COMMENT '自增id',
 4 name varchar(30) NOT NULL COMMENT '角色名'
 5 )ENGINE=innodb DEFAULT CHARSET=utf8 COMMENT='角色表';
 6 
 7 
 8 CREATE TABLE IF NOT EXISTS mr_privilege
 9 (
10 id int(11) PRIMARY KEY AUTO_INCREMENT COMMENT '自增id',
11 name varchar(30) NOT NULL COMMENT '權限名',
12 route varchar(50) NOT NULL COMMENT '權限全部的路由',
13 description varchar(100) NOT NULL COMMENT '權限的描述'
14 )ENGINE=innodb DEFAULT CHARSET=utf8 COMMENT='權限表';
15 
16 
17 CREATE TABLE IF NOT EXISTS mr_user_role
18 (
19 id int(11) PRIMARY KEY AUTO_INCREMENT COMMENT '自增id',
20 user_id int(11) NOT NULL COMMENT '用戶id',
21 role_id int(11) NOT NULL COMMENT '角色id'
22 )ENGINE=innodb DEFAULT CHARSET=utf8 COMMENT='用戶角色表';
23 
24 
25 CREATE TABLE IF NOT EXISTS mr_role_privilege
26 (
27 id int(11) PRIMARY KEY AUTO_INCREMENT COMMENT '自增id',
28 role_id int(11) NOT NULL COMMENT '角色id',
29 privilege_id int(11) NOT NULL COMMENT '權限id'
30 )ENGINE=innodb DEFAULT CHARSET=utf8 COMMENT='角色權限表';

 

二、在用戶模型和角色模型中實現多對多session

 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   }

 

}

 

三、將菜單視爲公共區域,在app\Providers\AppServiceProvider.php裏寫app

 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       });  // 使用map,最終獲得的結果$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       }  // 獲得的結果,$prpvileges = [['index/..', '列表'], ['', '']]
13       $view->with('privileges', $privileges);
14     });
15 }

 

 

四、菜單的實現(能夠直接遍歷一個div,我這裏由於有不一樣的樣式,便用了判斷)composer

 1 @foreach ($privileges as $privilege)
 2       @if ($privilege[1] == 'key/index' && $privilege[0] == '鍵名列表')
 3         <div class="slide__left__key" style="margin-top: 10px;"><a href="{{ url('key/index') }}" rel="external nofollow" ><span class="glyphicon glyphicon-th"></span> 鍵名列表</a></div>
 4       @endif
 5       @if ($privilege[1] == 'key/create' && $privilege[0] == '添加鍵名')
 6           <div class="slide__left__key"><a href="{{ url('key/create') }}" rel="external nofollow" ><span class="glyphicon glyphicon-plus"></span> 添加鍵名</a></div>
 7       @endif
 8       @if ($privilege[1] == 'project/index' && $privilege[0] == '項目列表')
 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> 項目列表</a></div>
10       @endif
11       @if ($privilege[1] == 'project/create' && $privilege[0] == '添加項目')
12           <div class="slide__left__key"><a href="{{ url('project/create') }}" rel="external nofollow" ><span class="glyphicon glyphicon-edit"></span> 添加項目</a></div>
13       @endif
14       @if ($privilege[1] == 'user/index' && $privilege[0] == '用戶列表')
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> 用戶列表</a></div>
16       @endif
17       @if ($privilege[1] == 'user/create' && $privilege[0] == '添加用戶')
18           <div class="slide__left__key"><a href="{{ url('user/create') }}" rel="external nofollow" ><span class="glyphicon glyphicon-plus-sign"></span> 添加用戶</a></div>
19       @endif
20 @endforeach

 

相關文章
相關標籤/搜索