编程学习网 > PHP技术 > laravel > 代码直接告诉你Laravel5如何实现RBAC权限管理操作
2021
06-04

代码直接告诉你Laravel5如何实现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

phper在进阶的时候总会遇到一些问题和瓶颈,业务代码写多了没有方向感,不知道该从那里入手去提升,欢迎关注编程学习网,更多教程和资讯

扫码二维码 获取免费视频学习资料

Python编程学习

查 看2022高级编程视频教程免费获取