Laravel框架實現的rbac權限管理操作示例

這篇文章主要介紹了Laravel框架實現的rbac權限管理操作,結合實例形式分析了Laravel框架權限控制rbac相關數據庫創建、讀寫及權限判斷等操作技巧,需要的朋友可以參考下

本文實例講述了Laravel框架實現的rbac權限管理操作。分享給大家供大家參考,具體如下:

介紹:根據不同的權限,在菜單欄顯示不同的功能,只對菜單進行了限制,若對路由也進行限制,請自行完善

1、建表(用戶表、角色表、權限表、用戶角色表、角色權限表)

CREATE TABLE IF NOT EXISTS mr_role
(
id int(11) PRIMARY KEY AUTO_INCREMENT COMMENT '自增id',
name varchar(30) NOT NULL COMMENT '角色名'
)ENGINE=innodb DEFAULT CHARSET=utf8 COMMENT='角色表';
CREATE TABLE IF NOT EXISTS mr_privilege
(
id int(11) PRIMARY KEY AUTO_INCREMENT COMMENT '自增id',
name varchar(30) NOT NULL COMMENT '權限名',
route varchar(50) NOT NULL COMMENT '權限所有的路由',
description varchar(100) NOT NULL COMMENT '權限的描述'
)ENGINE=innodb DEFAULT CHARSET=utf8 COMMENT='權限表';
CREATE TABLE IF NOT EXISTS mr_user_role
(
id int(11) PRIMARY KEY AUTO_INCREMENT COMMENT '自增id',
user_id int(11) NOT NULL COMMENT '用戶id',
role_id int(11) NOT NULL COMMENT '角色id'
)ENGINE=innodb DEFAULT CHARSET=utf8 COMMENT='用戶角色表';
CREATE TABLE IF NOT EXISTS mr_role_privilege
(
id int(11) PRIMARY KEY AUTO_INCREMENT COMMENT '自增id',
role_id int(11) NOT NULL COMMENT '角色id',
privilege_id int(11) NOT NULL COMMENT '權限id'
)ENGINE=innodb DEFAULT CHARSET=utf8 COMMENT='角色權限表';

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

class User extends Model
{
  protected $primaryKey = 'id';
  protected $table = 'user';
  public $timestamps = false;
  public $guarded = [];
  public function roles()
  {
    return $this->belongsToMany('App\Model\Role', 'user_role', 'user_id', 'role_id')->withPivot('user_id', 'role_id');
  }
}
class Role extends Model
{
  protected $table = 'role';
  protected $primaryKey = 'id';
  public $timestamps = false;
  public $guarded = [];
  public function privileges()
  {
    return $this->belongsToMany('App\Model\Privilege', 'role_privilege', 'role_id', 'privilege_id')->withPivot(['role_id', 'privilege_id']);
  }
}

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

public function boot()
{
    \View::composer('layout.slide', function($view) {
      $roles_id = User::find(session('user')['id'])->roles->map(function ($role) {
        return $role->id;
      });  // 使用map,最終得到的結果$roles_id = [1, 2, ...]
      $privileges = [];
      foreach ($roles_id as $role) {
        $privileges = array_merge($privileges, Role::find($role)->privileges->map(function ($privilege) {
          return [$privilege->name, $privilege->route];
        })->toArray());
      }  // 得到的結果,$prpvileges = [['index/..', '列表'], ['', '']]
      $view->with('privileges', $privileges);
    });
}

4、菜單的實現(可以直接遍歷一個div,我這裏因爲有不同的樣式,便用了判斷)

@foreach ($privileges as $privilege)
      @if ($privilege[1] == 'key/index' && $privilege[0] == '鍵名列表')
        <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>
      @endif
      @if ($privilege[1] == 'key/create' && $privilege[0] == '添加鍵名')
          <div class="slide__left__key"><a href="{{ url('key/create') }}" rel="external nofollow" ><span class="glyphicon glyphicon-plus"></span> 添加鍵名</a></div>
      @endif
      @if ($privilege[1] == 'project/index' && $privilege[0] == '項目列表')
          <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>
      @endif
      @if ($privilege[1] == 'project/create' && $privilege[0] == '添加項目')
          <div class="slide__left__key"><a href="{{ url('project/create') }}" rel="external nofollow" ><span class="glyphicon glyphicon-edit"></span> 添加項目</a></div>
      @endif
      @if ($privilege[1] == 'user/index' && $privilege[0] == '用戶列表')
          <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>
      @endif
      @if ($privilege[1] == 'user/create' && $privilege[0] == '添加用戶')
          <div class="slide__left__key"><a href="{{ url('user/create') }}" rel="external nofollow" ><span class="glyphicon glyphicon-plus-sign"></span> 添加用戶</a></div>
      @endif
    @endforeach

更多關於Laravel相關內容感興趣的讀者可查看本站專題:《Laravel框架入門與進階教程》、《php優秀開發框架總結》、《php面向對象程序設計入門教程》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧彙總

希望本文所述對大家基於Laravel框架的PHP程序設計有所幫助。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章