最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2019年12月09日 08:46:54   作者:luyaran  
這篇文章主要介紹了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)限表:

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>
<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)限控制起作用了。

更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

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

相關(guān)文章

  • php技術(shù)實現(xiàn)加載字體并保存成圖片

    php技術(shù)實現(xiàn)加載字體并保存成圖片

    這篇文章主要介紹了php技術(shù)實現(xiàn)加載字體并保存成圖片,需要的朋友可以參考下
    2015-07-07
  • app判斷鏈接參數(shù)后綴跳轉(zhuǎn)不同地址的方法

    app判斷鏈接參數(shù)后綴跳轉(zhuǎn)不同地址的方法

    下面小編就為大家?guī)硪黄猘pp判斷鏈接參數(shù)后綴跳轉(zhuǎn)不同地址的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • php頭像上傳預覽實例代碼

    php頭像上傳預覽實例代碼

    本篇文章主要介紹了php頭像上傳預覽實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • Laravel框架實現(xiàn)model層的增刪改查(CURD)操作示例

    Laravel框架實現(xiàn)model層的增刪改查(CURD)操作示例

    這篇文章主要介紹了Laravel框架實現(xiàn)model層的增刪改查(CURD)操作,結(jié)合實例形式分析了Laravel框架模型model層進行數(shù)據(jù)庫的增刪改查操作具體實現(xiàn)技巧,需要的朋友可以參考下
    2018-05-05
  • php權(quán)限調(diào)整強制用戶退出的解決步驟

    php權(quán)限調(diào)整強制用戶退出的解決步驟

    這篇文章主要介紹了php權(quán)限調(diào)整強制用戶退出的解決步驟,當用戶登錄時,將用戶的登錄狀態(tài)和其他相關(guān)信息存儲在服務器端,本文結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2023-09-09
  • Laravel獲取當前請求的控制器和方法以及中間件的例子

    Laravel獲取當前請求的控制器和方法以及中間件的例子

    今天小編就為大家分享一篇Laravel獲取當前請求的控制器和方法以及中間件的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • php后臺多用戶權(quán)限組思路與實現(xiàn)程序代碼分享

    php后臺多用戶權(quán)限組思路與實現(xiàn)程序代碼分享

    很多時候我們再開發(fā)過程中需要考慮到多用戶權(quán)限問題,這篇文章大家可以參考下
    2012-02-02
  • PHP中SSO Cookie登錄分析和實現(xiàn)

    PHP中SSO Cookie登錄分析和實現(xiàn)

    單點登錄SSO(Single Sign-On)是身份管理中的一部分。SSO的一種較為通俗的定義是:SSO是指訪問同一服務器不同應用中的受保護資源的同一用戶,只需要登錄一次,即通過一個應用中的安全驗證后,再訪問其他應用中的受保護資源時,不再需要重新登錄驗證
    2015-11-11
  • PHP下的浮點運算不準的解決方法

    PHP下的浮點運算不準的解決方法

    下面小編就為大家?guī)硪黄狿HP下的浮點運算不準的解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-10-10
  • ThinkPHP5.1表單令牌Token失效問題的解決

    ThinkPHP5.1表單令牌Token失效問題的解決

    這篇文章主要給大家介紹了關(guān)于ThinkPHP5.1表單令牌Token失效問題的解決方法,文中通過示例代碼介紹的非常詳細,對大家學習或者使用ThinkPHP具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-03-03

最新評論

崇阳县| 双峰县| 麻栗坡县| 赣榆县| 胶州市| 通城县| 洪江市| 洛宁县| 谢通门县| 两当县| 小金县| 泸溪县| 舒城县| 泽州县| 伽师县| 十堰市| 江源县| 石泉县| 淅川县| 武城县| 京山县| 闻喜县| 巫溪县| 绥棱县| 玛纳斯县| 漯河市| 阿克| 武功县| 长乐市| 宜川县| 怀仁县| 神农架林区| 德兴市| 乌恰县| 无锡市| 沐川县| 阿巴嘎旗| 永福县| 吉安县| 河北区| 建湖县|