Example of rbac Privilege Management Operation Implemented by Laravel Framework

  • 2021-11-13 01:06:30
  • OfStack

In this paper, an example is given to describe the rbac privilege management operation implemented by Laravel framework. Share it for your reference, as follows:

Introduction: According to different permissions, different functions are displayed in the menu bar, and only the menu is restricted. If the route is also restricted, please improve it yourself

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


CREATE TABLE IF NOT EXISTS mr_role
(
id int(11) PRIMARY KEY AUTO_INCREMENT COMMENT ' Self-increasing id',
name varchar(30) NOT NULL COMMENT ' Role name '
)ENGINE=innodb DEFAULT CHARSET=utf8 COMMENT=' Role table ';
CREATE TABLE IF NOT EXISTS mr_privilege
(
id int(11) PRIMARY KEY AUTO_INCREMENT COMMENT ' Self-increasing id',
name varchar(30) NOT NULL COMMENT ' Permission name ',
route varchar(50) NOT NULL COMMENT ' Routes owned by permissions ',
description varchar(100) NOT NULL COMMENT ' Description of permissions '
)ENGINE=innodb DEFAULT CHARSET=utf8 COMMENT=' Permission table ';
CREATE TABLE IF NOT EXISTS mr_user_role
(
id int(11) PRIMARY KEY AUTO_INCREMENT COMMENT ' Self-increasing id',
user_id int(11) NOT NULL COMMENT ' Users id',
role_id int(11) NOT NULL COMMENT ' Role id'
)ENGINE=innodb DEFAULT CHARSET=utf8 COMMENT=' User role table ';
CREATE TABLE IF NOT EXISTS mr_role_privilege
(
id int(11) PRIMARY KEY AUTO_INCREMENT COMMENT ' Self-increasing id',
role_id int(11) NOT NULL COMMENT ' Role id',
privilege_id int(11) NOT NULL COMMENT ' Authority id'
)ENGINE=innodb DEFAULT CHARSET=utf8 COMMENT=' Role permission table ';

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


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. Think of the menu as a common area and write it in 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;
      });  //  Use map The final result $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());
      }  //  The results obtained, $prpvileges = [['index/..', ' List '] ,  ['', '']]
      $view->with('privileges', $privileges);
    });
}

4. The realization of menu (you can directly traverse 1 div, and I use judgment here because there are different styles)


@foreach ($privileges as $privilege)
      @if ($privilege[1] == 'key/index' && $privilege[0] == ' List of key names ')
        <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>
      @endif
      @if ($privilege[1] == 'key/create' && $privilege[0] == ' Add key name ')
          <div class="slide__left__key"><a href="{{ url('key/create') }}" rel="external nofollow" ><span class="glyphicon glyphicon-plus"></span>  Add key name </a></div>
      @endif
      @if ($privilege[1] == 'project/index' && $privilege[0] == ' List of items ')
          <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>
      @endif
      @if ($privilege[1] == 'project/create' && $privilege[0] == ' Add Project ')
          <div class="slide__left__key"><a href="{{ url('project/create') }}" rel="external nofollow" ><span class="glyphicon glyphicon-edit"></span>  Add Project </a></div>
      @endif
      @if ($privilege[1] == 'user/index' && $privilege[0] == ' User list ')
          <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>
      @endif
      @if ($privilege[1] == 'user/create' && $privilege[0] == ' Add User ')
          <div class="slide__left__key"><a href="{{ url('user/create') }}" rel="external nofollow" ><span class="glyphicon glyphicon-plus-sign"></span>  Add User </a></div>
      @endif
    @endforeach

More readers interested in Laravel can check the topics of this site: "Introduction and Advanced Tutorial of Laravel Framework", "Summary of Excellent Development Framework of php", "Introduction Tutorial of php Object-Oriented Programming", "Introduction Tutorial of php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"

I hope this article is helpful to PHP programming based on Laravel framework.


Related articles: