激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

香港云服务器
服務(wù)器之家 - 編程語言 - PHP教程 - Laravel5.1框架自帶權(quán)限控制系統(tǒng) ACL用法分析

Laravel5.1框架自帶權(quán)限控制系統(tǒng) ACL用法分析

2021-09-22 16:47luyaran PHP教程

這篇文章主要介紹了Laravel5.1框架自帶權(quán)限控制系統(tǒng) ACL用法,結(jié)合實例形式分析了laravel5.1相關(guān)的角色創(chuàng)建、權(quán)限分配等相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了Laravel5.1框架自帶權(quán)限控制系統(tǒng) ACL用法。分享給大家供大家參考,具體如下:

Laravel在5.1.11版本中加入了Authorization,可以讓用戶自定義權(quán)限,今天分享一種定義權(quán)限系統(tǒng)的方法。

1. 創(chuàng)建角色與權(quán)限表

使用命令行創(chuàng)建角色與權(quán)限表:

?
1
php artisan make:migration create_permissions_and_roles --create=permissions

之后打開剛剛創(chuàng)建的文件,填入下面的代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('label');
$table->string('description')->nullable();
$table->timestamps();
});
Schema::create('permissions', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('label');
$table->string('description')->nullable();
$table->timestamps();
});
Schema::create('permission_role', function (Blueprint $table) {
$table->integer('permission_id')->unsigned();
$table->integer('role_id')->unsigned();
$table->foreign('permission_id')
->references('id')
->on('permissions')
->onDelete('cascade');
$table->foreign('role_id')
->references('id')
->on('roles')
->onDelete('cascade');
$table->primary(['permission_id', 'role_id']);
});
Schema::create('role_user', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
$table->integer('role_id')->unsigned();
$table->foreign('role_id')
->references('id')
->on('roles')
->onDelete('cascade');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->primary(['role_id', 'user_id']);
});
}
public function down()
{
Schema::drop('roles');
Schema::drop('permissions');
Schema::drop('permission_role');
Schema::drop('role_user');
}

上面的代碼會創(chuàng)建角色表、權(quán)限表、角色與權(quán)限的中間表以及角色與用戶的中間表。

2. 創(chuàng)建模型

接下來使用命令行分別創(chuàng)建角色與權(quán)限模型:

?
1
2
php artisan make:model Permission
php artisan make:model Role

然后分別打開Permission.php、Role.php 以及 User.php ,加入下面的代碼:

?
1
2
3
4
5
// Permissions.php
public function roles()
{
return $this->belongsToMany(Role::class);
}
?
1
2
3
4
5
6
7
8
9
10
// Role.php
public function permissions()
{
return $this->belongsToMany(Permission::class);
}
//給角色添加權(quán)限
public function givePermissionTo($permission)
{
return $this->permissions()->save($permission);
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// User.php
public function roles()
{
return $this->belongsToMany(Role::class);
}
// 判斷用戶是否具有某個角色
public function hasRole($role)
{
if (is_string($role)) {
return $this->roles->contains('name', $role);
}
return !! $role->intersect($this->roles)->count();
}
// 判斷用戶是否具有某權(quán)限
public function hasPermission($permission)
{
return $this->hasRole($permission->roles);
}
// 給用戶分配角色
public function assignRole($role)
{
return $this->roles()->save(
Role::whereName($role)->firstOrFail()
);
}

上面的代碼實現(xiàn)了給角色分配權(quán)限及給用戶分配角色,然后還提供了判斷用戶是否具有某角色及某權(quán)限的方法。

之后就給使用Laravel提供的Authorization來定義權(quán)限控制了,打開 /app/Providers/AuthServiceProvider.php 文件,在 boot() 中添加代碼:

?
1
2
3
4
5
6
7
8
9
10
public function boot(GateContract $gate)
{
parent::registerPolicies($gate);
$permissions = \App\Permission::with('roles')->get();
foreach ($permissions as $permission) {
$gate->define($permission->name, function($user) use ($permission) {
return $user->hasPermission($permission);
});
}
}

通過上面的方法就定義好了各個權(quán)限。下面就該填充數(shù)據(jù)了。

3. 填充數(shù)據(jù)

為方便起見,這里使用 tinker 命令行工具來添加幾條測試數(shù)據(jù):

?
1
php artisan tinker

之后進(jìn)入命令行,依次輸入下列命令:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 改變命名空間位置,避免下面每次都要輸入 App
namespace App
// 創(chuàng)建權(quán)限
$permission_edit = new Permission
$permission_edit->name = 'edit-post'
$permission_edit->label = 'Can edit post'
$permission_edit->save()
$permission_delete = new Permission
$permission_delete->name = 'delete-post'
$permission_delete->label = 'Can delete post'
$permission_delete->save()
// 創(chuàng)建角色
$role_editor = new Role
$role_editor->name = 'editor';
$role_editor->label = 'The editor of the site';
$role_editor->save()
$role_editor->givePermissionTo($permission_edit)
$role_admin = new Role
$role_admin->name = 'admin';
$role_admin->label = 'The admin of the site';
$role_admin->save()
// 給角色分配權(quán)限
$role_admin->givePermissionTo($permission_edit)
$role_admin->givePermissionTo($permission_delete)
// 創(chuàng)建用戶
$editor = factory(User::class)->create()
// 給用戶分配角色
$editor->assignRole($role_editor->name)
$admin = factory(User::class)->create()
$admin->assignRole($role_admin->name)

上面我們創(chuàng)建了兩個權(quán)限:edit-post 和 delete-post,然后創(chuàng)建了 editor 和 admin 兩個角色,editor 角色擁有 edit-post 的權(quán)限,而 admin 兩個權(quán)限都有。之后生成了兩個用戶,分別給他們分配了 editor 和 admin 的角色,即:ID 1 用戶擁有 editor 角色,因此只有 edit-post 權(quán)限,而 ID 2 用戶擁有 admin 角色,因此具有 edit-post 和 delete-post 權(quán)限。下面我們來驗證下是否正確。

打開 routes.php 文件:

?
1
2
3
4
Route::get('/', function () {
$user = Auth::loginUsingId(1);
return view('welcome');
})

上面我們先驗證 ID 1 用戶的權(quán)限,然后修改 /resources/views/welcome.blade.php 文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE html>
<html>
<head>
<title>Laravel</title>
</head>
<body>
<h1>權(quán)限測試</h1>
<p>
@can('edit-post')
<a href="#" rel="external nofollow" rel="external nofollow" >Edit Post</a>
@endcan
</p>
<p>
@can('delete-post')
<a href="#" rel="external nofollow" rel="external nofollow" >Delete Post</a>
@endcan
</p>
</body>
</html>

在視圖中我們通過 Laravel 提供的 @can 方法來判斷用戶是否具有某權(quán)限。

打開瀏覽器,訪問上面定義的路由,可以看到視圖中只出現(xiàn)了 Edit Post 鏈接。之后我們修改路由中用戶ID為 2 ,然后再次刷新瀏覽器,可以看到,這次同時出現(xiàn)了 Edit Post 和 Delete Post 兩個鏈接,說明我們定義的權(quán)限控制起作用了。

Laravel5.1框架自帶權(quán)限控制系統(tǒng) ACL用法分析

希望本文所述對大家基于Laravel框架的PHP程序設(shè)計有所幫助。

原文鏈接:https://blog.csdn.net/luyaran/article/details/53539022

延伸 · 閱讀

精彩推薦
956
主站蜘蛛池模板: 羞羞视频一区 | 免费一级特黄做受大片 | 一区二区三区日韩视频在线观看 | 91色一区二区三区 | 久久精品视频12 | 国产精品久久在线观看 | 黑人一区二区三区四区五区 | 在线视频1区 | 国产精品美女久久久久久不卡 | 国产精品视频免费看 | 国产91九色视频 | 男男成人高潮片免费视频欧美 | 久久九九热re6这里有精品 | 久久久久久久国产a∨ | 日产精品久久久一区二区开放时间 | 国产91小视频在线观看 | 日本在线观看视频网站 | 久久久久一区二区三区四区五区 | 中文字幕一区在线观看视频 | 一级黄色av电影 | 天天鲁在线视频免费观看 | 精品一区二区久久久久久久网精 | 中文字幕一区二区三区四区 | www.mitao| 国产一级免费在线视频 | 久久久国产精品电影 | 综合国产在线 | 亚洲小视频在线 | 久色亚洲| 国产一级一区二区三区 | 羞羞视频在线免费 | 欧美一区久久久 | 日韩视频观看 | 在线观看国产网站 | 奇米影视四色7777 | 色妞欧美 | 久草在线最新 | 视频一区二区三区中文字幕 | av在线大全| 国产精品色综合 | 成人爽a毛片免费啪啪红桃视频 |