Laravel5.1框架自帶權(quán)限控制系統(tǒng) ACL用法分析
本文實(shí)例講述了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
之后打開(kāi)剛剛創(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'); }
上面的代碼會(huì)創(chuàng)建角色表、權(quán)限表、角色與權(quán)限的中間表以及角色與用戶的中間表。
2. 創(chuàng)建模型
接下來(lái)使用命令行分別創(chuàng)建角色與權(quán)限模型:
php artisan make:model Permission php artisan make:model Role
然后分別打開(kāi)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); } // 判斷用戶是否具有某個(gè)角色 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() ); }
上面的代碼實(shí)現(xiàn)了給角色分配權(quán)限及給用戶分配角色,然后還提供了判斷用戶是否具有某角色及某權(quán)限的方法。
之后就給使用Laravel提供的Authorization來(lái)定義權(quán)限控制了,打開(kāi) /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); }); } }
通過(guò)上面的方法就定義好了各個(gè)權(quán)限。下面就該填充數(shù)據(jù)了。
3. 填充數(shù)據(jù)
為方便起見(jiàn),這里使用 tinker 命令行工具來(lái)添加幾條測(cè)試數(shù)據(jù):
php artisan tinker
之后進(jìn)入命令行,依次輸入下列命令:
// 改變命名空間位置,避免下面每次都要輸入 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)建了兩個(gè)權(quán)限:edit-post 和 delete-post,然后創(chuàng)建了 editor 和 admin 兩個(gè)角色,editor 角色擁有 edit-post 的權(quán)限,而 admin 兩個(gè)權(quán)限都有。之后生成了兩個(gè)用戶,分別給他們分配了 editor 和 admin 的角色,即:ID 1 用戶擁有 editor 角色,因此只有 edit-post 權(quán)限,而 ID 2 用戶擁有 admin 角色,因此具有 edit-post 和 delete-post 權(quán)限。下面我們來(lái)驗(yàn)證下是否正確。
打開(kāi) routes.php 文件:
Route::get('/', function () { $user = Auth::loginUsingId(1); return view('welcome'); })
上面我們先驗(yàn)證 ID 1 用戶的權(quán)限,然后修改 /resources/views/welcome.blade.php 文件:
<!DOCTYPE html> <html> <head> <title>Laravel</title> </head> <body> <h1>權(quán)限測(cè)試</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>
在視圖中我們通過(guò) Laravel 提供的 @can 方法來(lái)判斷用戶是否具有某權(quán)限。
打開(kāi)瀏覽器,訪問(wèn)上面定義的路由,可以看到視圖中只出現(xiàn)了 Edit Post 鏈接。之后我們修改路由中用戶ID為 2 ,然后再次刷新瀏覽器,可以看到,這次同時(shí)出現(xiàn)了 Edit Post 和 Delete Post 兩個(gè)鏈接,說(shuō)明我們定義的權(quán)限控制起作用了。
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
欄 目:PHP編程
下一篇:Ubuntu下如何升級(jí)到PHP7.4的方法步驟
本文標(biāo)題:Laravel5.1框架自帶權(quán)限控制系統(tǒng) ACL用法分析
本文地址:http://mengdiqiu.com.cn/a1/PHPbiancheng/11046.html
您可能感興趣的文章
- 01-11thinkphp框架類庫(kù)擴(kuò)展操作示例
- 01-11關(guān)于Yii2框架跑腳本時(shí)內(nèi)存泄漏問(wèn)題的分析與解決
- 01-11php 實(shí)現(xiàn)簡(jiǎn)單的登錄功能示例【基于thinkPHP框架】
- 01-11Laravel框架Blade模板簡(jiǎn)介及模板繼承用法分析
- 01-11Laravel框架基礎(chǔ)語(yǔ)法與知識(shí)點(diǎn)整理【模板變量、輸出、include引入
- 01-11Laravel框架Eloquent ORM刪除數(shù)據(jù)操作示例
- 01-11Laravel框架Eloquent ORM修改數(shù)據(jù)操作示例
- 01-11Laravel框架Eloquent ORM簡(jiǎn)介、模型建立及查詢數(shù)據(jù)操作詳解
- 01-11laravel5.1框架下的批量賦值實(shí)現(xiàn)方法分析
- 01-11laravel5.5框架的上傳圖片功能實(shí)例分析【僅傳到服務(wù)器端】


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02php本站才可以請(qǐng)求數(shù)據(jù) php本地?cái)?shù)據(jù)庫(kù)
- 04-02關(guān)于txt數(shù)據(jù)庫(kù)php的信息
- 04-02php打印請(qǐng)求數(shù)據(jù) php打印輸出結(jié)果
- 04-02網(wǎng)頁(yè)里php操作數(shù)據(jù)庫(kù) php網(wǎng)頁(yè)例子
- 04-02php插入數(shù)據(jù)庫(kù)為亂碼 php連接數(shù)據(jù)庫(kù)亂
- 04-02php數(shù)據(jù)庫(kù)地址 phpstudy 數(shù)據(jù)庫(kù)
- 04-02php數(shù)據(jù)庫(kù)數(shù)據(jù)相加 php數(shù)據(jù)庫(kù)添加數(shù)據(jù)
- 04-02數(shù)據(jù)權(quán)限架構(gòu)思路php 數(shù)據(jù)權(quán)限設(shè)計(jì)方
- 04-02php數(shù)據(jù)庫(kù)輸入變量 php里輸出數(shù)據(jù)庫(kù)數(shù)
- 04-02php如何用導(dǎo)入數(shù)據(jù) php用來(lái)導(dǎo)入其他文
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開(kāi)原生自帶讀寫NTFS功能(圖文
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10C#中split用法實(shí)例總結(jié)