Laravel框架實(shí)現(xiàn)的rbac權(quán)限管理操作示例
本文實(shí)例講述了Laravel框架實(shí)現(xiàn)的rbac權(quán)限管理操作。分享給大家供大家參考,具體如下:
介紹:根據(jù)不同的權(quán)限,在菜單欄顯示不同的功能,只對(duì)菜單進(jìn)行了限制,若對(duì)路由也進(jìn)行限制,請(qǐng)自行完善
1、建表(用戶表、角色表、權(quán)限表、用戶角色表、角色權(quán)限表)
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 '權(quán)限名', route varchar(50) NOT NULL COMMENT '權(quán)限所有的路由', description varchar(100) NOT NULL COMMENT '權(quán)限的描述' )ENGINE=innodb DEFAULT CHARSET=utf8 COMMENT='權(quán)限表'; 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 '權(quán)限id' )ENGINE=innodb DEFAULT CHARSET=utf8 COMMENT='角色權(quán)限表';
2、在用戶模型和角色模型中實(shí)現(xiàn)多對(duì)多
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、將菜單視為公共區(qū)域,在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,最終得到的結(jié)果$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());
} // 得到的結(jié)果,$prpvileges = [['index/..', '列表'], ['', '']]
$view->with('privileges', $privileges);
});
}
4、菜單的實(shí)現(xiàn)(可以直接遍歷一個(gè)div,我這里因?yàn)橛胁煌臉邮?,便用了判斷?/p>
@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] == '項(xiàng)目列表')
<div class="slide__left__key" style="margin-top: 20px;"><a href="{{ url('project/index') }}" rel="external nofollow" ><span class="glyphicon glyphicon-th-list"></span> 項(xiàng)目列表</a></div>
@endif
@if ($privilege[1] == 'project/create' && $privilege[0] == '添加項(xiàng)目')
<div class="slide__left__key"><a href="{{ url('project/create') }}" rel="external nofollow" ><span class="glyphicon glyphicon-edit"></span> 添加項(xiàng)目</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
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
- 在Laravel5中正確設(shè)置文件權(quán)限的方法
- Laravel5權(quán)限管理方法詳解
- laravel實(shí)現(xiàn)簡單用戶權(quán)限的示例代碼
- 解決laravel中日志權(quán)限莫名變成了root的問題
- laravel利用中間件做防非法登錄和權(quán)限控制示例
- Laravel5.1數(shù)據(jù)庫連接、創(chuàng)建數(shù)據(jù)庫、創(chuàng)建model及創(chuàng)建控制器的方法
- laravel5.1框架基礎(chǔ)之Blade模板繼承簡單使用方法分析
- Laravel5.1框架注冊(cè)中間件的三種場(chǎng)景詳解
- laravel5.1框架基礎(chǔ)之路由詳解
- laravel5.1框架model類查詢的實(shí)現(xiàn)方法
- Laravel5.1框架自帶權(quán)限控制系統(tǒng) ACL用法分析
相關(guān)文章
thinkPHP通用控制器實(shí)現(xiàn)方法示例
這篇文章主要介紹了thinkPHP通用控制器實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了thinkPHP針對(duì)數(shù)據(jù)庫的基本CURD操作方法的封裝實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-11-11
老生常談PHP中的數(shù)據(jù)結(jié)構(gòu):DS擴(kuò)展
下面小編就為大家?guī)硪黄仙U凱HP中的數(shù)據(jù)結(jié)構(gòu):DS擴(kuò)展。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
PHP中的數(shù)組分頁實(shí)現(xiàn)(非數(shù)據(jù)庫)實(shí)例講解
這篇文章主要介紹了PHP中的數(shù)組分頁實(shí)現(xiàn)(非數(shù)據(jù)庫)實(shí)例講解,實(shí)例講解的很清楚,有對(duì)這方面有需要的同學(xué)可以借鑒下2021-01-01
使用laravel的Eloquent模型如何獲取數(shù)據(jù)庫的指定列
今天小編就為大家分享一篇使用laravel的Eloquent模型如何獲取數(shù)據(jù)庫的指定列,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10
PHP使用Session遇到的一個(gè)Permission denied Notice解決辦法
這篇文章主要介紹了PHP使用Session遇到的一個(gè)Permission denied Notice解決辦法,本文系統(tǒng)環(huán)境是ubuntu、Debian系統(tǒng),有很小的概率會(huì)遇到這個(gè)提示,需要的朋友可以參考下2014-07-07
php實(shí)現(xiàn)socket推送技術(shù)的示例
下面小編就為大家分享一篇php實(shí)現(xiàn)socket推送技術(shù)的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12

