如何在Laravel5.1框架中使用ACL權(quán)限控制系統(tǒng)-創(chuàng)新互聯(lián)

這篇文章將為大家詳細講解有關(guān)如何在Laravel5.1框架中使用 ACL權(quán)限控制系統(tǒng),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

10年積累的做網(wǎng)站、網(wǎng)站建設(shè)經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先網(wǎng)站設(shè)計后付款的網(wǎng)站建設(shè)流程,更有富源免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

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


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

php artisan make:migration create_permissions_and_roles --create=permissions

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

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)限模型:

php artisan make:model Permission
php artisan make:model Role

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

// Permissions.php
public function roles()
{
return $this->belongsToMany(Role::class);
}
// Role.php
public function permissions()
{
return $this->belongsToMany(Permission::class);
}
//給角色添加權(quán)限
public function givePermissionTo($permission)
{
return $this->permissions()->save($permission);
}
// 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() 中添加代碼:

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ù):

php artisan tinker

之后進入命令行,依次輸入下列命令:

// 改變命名空間位置,避免下面每次都要輸入 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 文件:

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

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

<!DOCTYPE html>
<html>
<head>
<title>Laravel</title>
</head>
<body>
<h2>權(quán)限測試</h2>
<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框架中使用 ACL權(quán)限控制系統(tǒng)

關(guān)于如何在Laravel5.1框架中使用 ACL權(quán)限控制系統(tǒng)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

當前題目:如何在Laravel5.1框架中使用ACL權(quán)限控制系統(tǒng)-創(chuàng)新互聯(lián)
文章出自:http://bm7419.com/article12/dgdcgc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、網(wǎng)站排名做網(wǎng)站、網(wǎng)站設(shè)計、軟件開發(fā)、網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機網(wǎng)站建設(shè)