您好,欢迎访问一九零五行业门户网

如何使用ThinkPHP6实现用户角色权限管理

随着业务的不断发展,很多中小型公司都拥有了自己的用户维护系统,用户权限管理是其中重要的一环。为了保护系统中的敏感信息、保障业务的正常运行,我们需要使用角色权限管理机制,保证不同角色的用户只能访问指定的资源和数据。
本文将以thinkphp6框架为例,介绍如何使用其提供的权限控制中间件和扩展扩展包实现用户角色权限管理。
创建角色表和权限表首先我们需要定义两个数据库表,一个是角色表,用来存储系统角色信息;另一个是权限表,用来存储系统权限信息。
create table role (
id int(11) not null auto_increment comment '主键',
name varchar(20) not null comment '角色名称',
description varchar(50) not null comment '角色描述',
primary key (id)
) engine=innodb default charset=utf8mb4 comment='角色表';
create table permission (
id int(11) not null auto_increment comment '主键',
name varchar(20) not null comment '权限名称',
description varchar(50) not null comment '权限描述',
primary key (id)
) engine=innodb default charset=utf8mb4 comment='权限表';
我们可以使用thinkphp提供的数据库迁移工具来创建表:php think migrate:run。
创建角色和权限模型接下来,我们需要创建角色和权限的模型。在app/model目录下创建role.php和permission.php文件,代码如下:
namespace appmodel;
use thinkmodel;
class role extends model
{
protected $table = 'role';
}
<?php
namespace appmodel;
use thinkmodel;
class permission extends model
{
protected $table = 'permission';
}
创建角色和权限关联表由于一个用户可能拥有多个角色,一个角色也可能对应多个权限,所以我们需要创建一个角色和权限的关联表。在数据库中创建一个role_permission表。
create table role_permission (
id int(11) not null auto_increment comment '主键',
role_id int(11) not null comment '角色id',
permission_id int(11) not null comment '权限id',
primary key (id),
key role_id (role_id),
key permission_id (permission_id)
) engine=innodb default charset=utf8mb4 comment='角色-权限关联表';
在模型中定义角色和权限的多对多关系:
<?php
namespace appmodel;
use thinkmodel;
class role extends model
{
protected $table = 'role';public function permissions(){ return $this->belongstomany( permission::class, 'role_permission', 'role_id', 'permission_id' );}
}
<?php
namespace appmodel;
use thinkmodel;
class permission extends model
{
protected $table = 'permission';public function roles(){ return $this->belongstomany( role::class, 'role_permission', 'permission_id', 'role_id' );}
}
定义中间件在thinkphp6中,中间件是处理请求的强大工具,我们可以通过中间件来实现权限控制。创建一个checkauth中间件,用于判断用户是否有权限进行当前的操作。在appmiddleware目录下创建checkauth.php文件,代码如下:
<?php
namespace appmiddleware;
use think acadedb;
use think acadesession;
use think acadeconfig;
class checkauth
{
public function handle($request, closure $next){ if (session::has('user')) { $roles = db::table('user') ->alias('u') ->leftjoin('role_user ru', 'u.id = ru.user_id') ->leftjoin('role r', 'ru.role_id = r.id') ->where('u.id', '=', session::get('user')->id) ->field('r.id') ->select(); $permissions = config::get('permissions'); foreach ($roles as $role) { $rolepermissions = db::table('role_permission') ->where('role_id', '=', $role->id) ->field('permission_id') ->select(); foreach ($rolepermissions as $rolepermission) { if (in_array($rolepermission->permission_id, $permissions)) { return $next($request); } } } } abort(403, '没有权限');}
}
该中间件会先查询当前用户所拥有的所有角色,在遍历角色时,查询每个角色所拥有的权限,如果有符合当前请求的权限,就允许继续执行,否则返回403错误。
创建权限配置文件为了方便管理系统的权限,我们可以使用thinkphp提供的config功能,将所有的权限写入配置文件中。在config目录下创建一个permissions.php文件,代码如下:
<?php
return [
1 => 'user.create',2 => 'user.read',3 => 'user.update',4 => 'user.delete',
];
我们可以通过key/value的形式来记录系统所有的权限,key为一个整数,value为一个字符串,表示权限的名称。
应用中间件最后,我们需要实际应用上述的中间件。打开config目录下的middleware.php文件,添加checkauth中间件。
<?php
return [
// ...'check_auth' => appmiddlewarecheckauth::class,
];
中间件的应用顺序是按照数组的键名从前到后依次执行的,我们可以通过数组下标来调整中间件的执行顺序。
在需要进行权限控制的控制器或方法上,可以使用middleware方法来绑定checkauth中间件。
<?php
namespace appcontroller;
use think acadeview;
class usercontroller
{
public function create(){ $this->middleware('check_auth'); // ...}
}
至此,我们已经完成了使用thinkphp6实现用户角色权限管理的所有步骤,您可以根据实际业务需求来扩展和完善上述示例代码。
以上就是如何使用thinkphp6实现用户角色权限管理的详细内容。
其它类似信息

推荐信息