In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.