Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to implement RBAC privilege Management Operation in Laravel

Shulou Source: shulou.com Published: 2022-06-02 06:46:37 09月20日 Update

Laravel in how to achieve RBAC rights management operation, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

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

CREATE TABLE IF NOT EXISTS mr_role (id int (11) PRIMARY KEY AUTO_INCREMENT COMMENT 'add 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 'add id',name varchar (30) NOT NULL COMMENT' permission name', route varchar (50) NOT NULL COMMENT 'permission all routes', description varchar (100) NOT NULL COMMENT 'permission description') 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-add id',user_id int (11) NOT NULL COMMENT' user 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-add id',role_id int (11) NOT NULL COMMENT' role id',privilege_id int (11) NOT NULL COMMENT 'permissions id') ENGINE=innodb DEFAULT CHARSET=utf8 COMMENT=' role permissions 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. Treat the menu as a public 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;}); / / using 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 (); $prpvileges = [['index/..',' list'], [',']] $view- > with ('privileges', $privileges);}) }

4. Implementation of the menu (you can directly traverse a div. I use judgment here because there are different styles)

@ foreach ($privileges as $privilege) @ if ($privilege [1] = = 'key/index' & & $privilege [0] = =' list of key names') list of key names @ endif @ if ($privilege [1] = = 'key/create' & & $privilege [0] =' add key name') add key name @ endif @ if ($privilege [1] = 'project/index' & $privilege [0]) ] = 'Project list') Project list @ endif @ if ($privilege [1] = = 'project/create' & & $privilege [0] = =' add Project') add Project @ endif @ if ($privilege [1] = = 'user/index' & & $privilege [0] = =' user list') user list @ endif @ If ($privilege [1] = = 'user/create' & & $privilege [0] = =' add user') does it help you to add the user @ endif@endforeach after reading the above? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Roles permissions users projects models results menus help management differences clarity content regions for this articles novices more styles knowledge industries Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Linux Shulou Tech Info MySQL Shulou Technology