Laravel框架下用户自定义权限的方法用什么,怎么实现
Admin 2022-06-13 群英技术资�
本文实例讲述了Laravel5.1框架自带权限控制系统 ACL用法。分享给大家供大家参考,具体如下�
Laravel�5.1.11版本中加入了Authorization,可以让用户自定义权限,今天分享一种定义权限系统的方法�
1. 创建角色与权限表
使用命令行创建角色与权限表:
php artisan make:migration create_permissions_and_roles --create=permissions
之后打开刚刚创建的文件,填入下面的代码:
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'); }
上面的代码会创建角色表、权限表、角色与权限的中间表以及角色与用户的中间表�
2. 创建模型
接下来使用命令行分别创建角色与权限模型:
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); } //给角色添加权� 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(); } // 判断用户是否具有某权� public function hasPermission($permission) { return $this->hasRole($permission->roles); } // 给用户分配角� public function assignRole($role) { return $this->roles()->save( Role::whereName($role)->firstOrFail() ); }
上面的代码实现了给角色分配权限及给用户分配角色,然后还提供了判断用户是否具有某角色及某权限的方法�
之后就给使用Laravel提供的Authorization来定义权限控制了,打开 /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); }); } }
通过上面的方法就定义好了各个权限。下面就该填充数据了�
3. 填充数据
为方便起见,这里使用 tinker 命令行工具来添加几条测试数据�
php artisan tinker
之后进入命令行,依次输入下列命令�
// 改变命名空间位置,避免下面每次都要输� App namespace App // 创建权限 $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() // 创建角色 $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() // 给角色分配权� $role_admin->givePermissionTo($permission_edit) $role_admin->givePermissionTo($permission_delete) // 创建用户 $editor = factory(User::class)->create() // 给用户分配角� $editor->assignRole($role_editor->name) $admin = factory(User::class)->create() $admin->assignRole($role_admin->name)
上面我们创建了两个权限:edit-post � delete-post,然后创建了 editor � admin 两个角色,editor 角色拥有 edit-post 的权限,� admin 两个权限都有。之后生成了两个用户,分别给他们分配� editor � admin 的角色,即:ID 1 用户拥有 editor 角色,因此只� edit-post 权限,� ID 2 用户拥有 admin 角色,因此具� edit-post � delete-post 权限。下面我们来验证下是否正确�
打开 routes.php 文件�
Route::get('/', function () { $user = Auth::loginUsingId(1); return view('welcome'); })
上面我们先验� ID 1 用户的权限,然后修改 /resources/views/welcome.blade.php 文件�
<!DOCTYPE html> <html> <head> <title>Laravel</title> </head> <body> <h1>权限测试</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 方法来判断用户是否具有某权限�
打开浏览器,访问上面定义的路由,可以看到视图中只出现� Edit Post 链接。之后我们修改路由中用户ID� 2 ,然后再次刷新浏览器,可以看到,这次同时出现� Edit Post � Delete Post 两个链接,说明我们定义的权限控制起作用了�
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:[email protected]进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容�
猜你喜欢
我们要做项目开发,连接数据库是需要掌握的内容。对此,这篇文章给大家分享php下使用pdo连接数据库操作,小编觉得比较实用,感兴趣的朋友可以了解了解�
本文主要给大家分享的是laravel服务容器的内容,服务容器是用于管理类(服务)的实例化的机制,下文对laravel服务容器及使用,以及服务绑定与解析都有详细的介绍,感兴趣的朋友就继续往下看吧�
这篇文章主要介绍了Yii框架 session 数据库存储操作方�,结合实例形式分析了使用Yii框架session组件配置与数据库存储相关操作技�,需要的朋友可以参考下
许多人在学习C语言的时候都写过一些有趣的算法,其实这些算法在PHP中也同样可以实现,甚至有些算法的代码比C语言中还要简�,需要的朋友可以参考下
对于PHP初学者,可能不太清楚超全局变量是什么,这篇文章就给大家介绍一下php超全局变量以及功能,有这方面学习需求的朋友就继续往下看吧�
推荐内容
联系我们
24小时售后 � 24小时售后TEL�0668-2555666 售前咨询TEL�400-678-4567 投诉建议TEL�0668-2555999 投诉建议邮箱:t[email protected] 信息安全TEL�0668-2555118 域名空间客服 � 公司总机�0668-2555555 公司传真�0668-2555000Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所� 茂名市群英网络有限公�
增值电信经营许可证 : B1.B2-20140078