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

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

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

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='角色權限表';

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

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']);
  }
}

 

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

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);
    });
}

 

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

@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

以上內容但願幫助到你們, 不少PHPer在進階的時候總會遇到一些問題和瓶頸,業務代碼寫多了沒有方向感,不知道該從那裏入手去提高,對此我整理了一些資料,包括但不限於:分佈式架構、高可擴展、高性能、高併發、服務器性能調優、TP6,laravel,Redis,Swoole、Swoft、Kafka、Mysql優化、shell腳本、Docker、微服務、Nginx等多個知識點高級進階乾貨須要的能夠免費分享給你們 ,須要戳這裏     PHP進階架構師>>>實戰視頻、大廠面試文檔免費獲取       服務器

相關文章
相關標籤/搜索