Download the PHP package wiseadmin/wise-auth without Composer

On this page you can find all versions of the php package wiseadmin/wise-auth. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package wise-auth

wiseadmin/wise-auth

为 ThinkPHP 6 设计的通用 RBAC 权限管理包,支持多应用、多用户类型、角色继承(RBAC1)与职责分离约束(RBAC2 扩展)。

PHP Version ThinkPHP Packagist


目录


核心特性


环境要求

依赖 版本
PHP >= 7.4
ThinkPHP >= 6.0

安装

1. 通过 Composer 安装

2. 初始化

安装完成后,执行初始化命令:

该命令会:

3. 执行数据库迁移

将创建以下 4 张数据表:

表名(默认) 说明
auth_roles 角色表
auth_permissions 权限规则表
auth_role_permission 角色-权限中间表
auth_user_role 用户-角色多态关联表

快速开始

第一步:给你的用户模型加上权限能力

第二步:配置超级管理员(可选)

在 config/wise-auth.php 中:

第三步:在路由中使用中间件

第四步:在控制器中检查权限


核心概念


配置说明

完整配置文件位于 config/wise-auth.php,以下是所有配置项的详细说明:

用户模型

配置项 类型 默认值 说明
default_user_model string \app\admin\model\Admin::class 默认用户模型(FQCN),需 use HasRoles
user_models array [] 多用户类型映射,key 为 guard/user_type,value 为模型类

数据表

配置项 类型 默认值 说明
table_names.roles string auth_roles 角色表名
table_names.permissions string auth_permissions 权限表名
table_names.role_permission string auth_role_permission 角色-权限中间表名
table_names.user_role string auth_user_role 用户-角色中间表名

超级管理员

配置项 类型 默认值 说明
super_role string|null super_admin 超级管理员角色 slug,设为 null 禁用
super_admin_ids array [] 超级管理员用户 ID 列表

缓存

配置项 类型 默认值 说明
cache_enabled bool true 是否启用权限缓存
cache_store string|null null 缓存驱动,null 使用默认(支持 redis)
cache_ttl int|null 3600 缓存过期时间(秒),null 为永久
cache_prefix string wiseauth: 缓存键前缀

权限来源

配置项 类型 默认值 说明
permission_sources array ['database'] 权限来源:database / annotation / config
annotation_dirs array [] 注解扫描目录
annotation_tag string @permission 注解标签名
permissions array [] 配置文件驱动的权限列表

中间件策略

配置项 类型 默认值 说明
default_mode string whitelist 默认检查模式:whitelist / blacklist
whitelist_routes array [...] 白名单路由(无需鉴权)
blacklist_routes array [] 黑名单路由(必须鉴权)
middleware_check_type string or 默认权限检查类型:and / or
middleware_auth_check bool true 中间件是否检查登录状态
user_resolver Closure|null null 自定义用户解析器

未授权处理

配置项 类型 默认值 说明
unauthorized_action string json exception / redirect / json / custom
unauthorized_redirect string /admin/login 重定向地址(redirect 模式)
unauthorized_handler Closure|null null 自定义处理器(custom 模式)

角色继承

配置项 类型 默认值 说明
inheritance_enabled bool true 是否启用角色继承
inheritance_max_depth int|null 10 最大继承深度,null/0 为无限制
cycle_check_enabled bool true 是否检测循环继承

RBAC2 约束

配置项 类型 默认值 说明
constraint_checker string|Closure|null null 自定义约束检查器
constraints array [] 内置约束规则(详见 RBAC2 章节)

数据权限

配置项 类型 默认值 说明
data_policy Closure|null null 数据权限策略(行级过滤)

使用指南

用户模型接入

HasRoles Trait 提供以下方法:

方法 说明
getRoles() 获取所有角色
hasRole($roles, $requireAll) 检查是否拥有指定角色(slug/ID)
assignRole($roleIds) 分配角色(支持 slug 或 ID)
removeRole($roleIds) 移除角色
syncRoles($roleIds) 同步角色(先清空再分配)
can($permission) 检查单个权限
hasPermission($permissions, $requireAll) 检查权限(支持 OR/AND 模式)
getAllPermissions() 获取用户所有权限标识
isSuperAdmin() 判断是否为超级管理员

HasRoles 还会自动注册 saved 和 deleted 模型事件,在用户信息变更时自动清除权限缓存。


权限检查

方式一:通过 Trait 实例方法

方式二:通过门面(静态调用)

方式三:模板中使用


角色管理

创建角色

赋予权限

获取角色树

返回格式示例:

检测循环继承


中间件集成

基础用法

在路由定义中注册中间件:

中间件会自动:

  1. 从 Session(admin_id)或自定义解析器获取当前用户
  2. 超级管理员直接放行
  3. 检查当前路由是否在白名单中(白名单命中则跳过鉴权)
  4. 将当前路由匹配到权限标识(如 /admin/admins/index → admin.admins.index)
  5. 执行 checkAny(OR 模式)或 checkAll(AND 模式)
  6. 根据 unauthorized_action 配置处理未授权情况

按路由指定权限

中间件注册

在 app/middleware.php 中注册中间件别名(TP8 仅支持静态配置):

自定义用户解析器

当用户信息不在 Session 时(如使用 JWT):

黑白名单策略

白名单模式(默认):未匹配路由默认需要鉴权,白名单内路由可跳过。

黑名单模式:未匹配路由默认允许访问,黑名单内路由必须鉴权。

未授权处理

四种模式可选:


超级管理员

支持两种超管判断方式:

方式一:用户 ID 列表

全局生效,不受 user_type 影响。通常与项目 config('app.super_admin_ids') 保持一致。

方式二:超级角色 slug

拥有 super_admin 角色的用户自动拥有全部权限。此方式受 user_type 多态关联约束。


角色继承(RBAC1)

通过 parent_id 字段建立角色父子关系,子角色自动继承父角色的所有权限。


约束检查(RBAC2)

内置三种约束规则,在 config/wise-auth.php 中配置:

互斥角色(mutual_exclusion)

某些角色不能同时分配给同一个用户:

基数限制(cardinality)

限制某个角色的最大用户数:

前置角色(prerequisite)

分配某角色前必须先拥有另一角色:

自定义约束检查器

需实现 \wise\auth\contract\ConstraintCheckerInterface 接口:


数据权限

通过 data_policy 配置行级数据过滤策略:

auth_user_role 表的 data_scope 字段支持为特定用户-角色关联附加数据范围(如部门 ID 列表)。


权限来源

WiseAuth 支持三种权限定义来源,可在 permission_sources 中组合使用:

数据库(database)

通过后台管理界面动态增删权限,数据持久化在 auth_permissions 表中。

配置文件(config)

在 wise-auth.php 的 permissions 节点定义:

注解(annotation)

在控制器方法的注释中声明:

扫描目录配置:

通过命令手动导入:


事件系统

WiseAuth 通过 ThinkPHP 事件系统提供了丰富的事件钩子:

事件名 触发时机 携带数据
RoleChanged 角色创建/更新/删除 action, roleId, data
PermissionChanged 权限创建/更新/删除 action, permissionId, data
RbacRoleAssigning 角色分配前 user_id, user_type, role_ids
RbacRoleAssigned 角色分配后 user_id, user_type, role_ids
RbacRoleRemoving 角色移除前 user_id, user_type, role_ids
RbacRoleRemoved 角色移除后 user_id, user_type, role_ids
RbacRoleSynced 角色同步后 user_id, user_type, role_ids

触发事件

监听事件

或在 app/event.php 中订阅:


命令行工具

wise:auth:publish

初始化 WiseAuth,发布配置并注册权限。

wise:auth:import

从不同来源批量导入权限。

JSON 文件格式:

wise:auth:clear

清除权限缓存。


数据库表结构

auth_roles(角色表)

字段 类型 说明
id int, PK 主键
name varchar(50) 角色名称
slug varchar(50), UNIQUE 角色唯一标识
description varchar(255) 描述
parent_id int, DEFAULT 0 父角色 ID(RBAC1 继承)
path varchar(500) 树形路径(如 0-1-3)
level int 层级深度
is_inherit tinyint, DEFAULT 1 是否继承父角色权限
sort int 排序
status tinyint, DEFAULT 1 状态(1:启用 0:禁用)
extend json 扩展属性
create_time dateTime 创建时间戳
update_time dateTime, nullable 更新时间戳
delete_time dateTime, nullable 软删除

auth_permissions(权限表)

字段 类型 说明
id int, PK 主键
ident varchar(100), UNIQUE 权限标识(如 admin.user.list)
name varchar(100) 权限名称
description varchar(255) 描述
type varchar(20), DEFAULT 'route' 类型:route/api/custom/menu
route varchar(200) 路由路径(支持通配符 *)
method varchar(20), DEFAULT '*' 请求方法(GET/POST/PUT/DELETE/*)
parent_id int, DEFAULT 0 父权限 ID
path varchar(500) 树形路径
level int 层级深度
plugin varchar(50) 所属插件标识
source varchar(20), DEFAULT 'database' 来源:database/config/annotation
conditions text, nullable 权限条件(JSON)
sort int 排序
status tinyint, DEFAULT 1 状态
extend json 扩展属性
create_time dateTime 创建时间戳
update_time dateTime, nullable 更新时间戳
delete_time dateTime, nullable 软删除

auth_role_permission(角色-权限中间表)

字段 类型 说明
role_id int 角色 ID(联合主键)
permission_id int 权限 ID(联合主键)

auth_user_role(用户-角色多态关联表)

字段 类型 说明
id int, PK 主键
user_type varchar(50), DEFAULT 'admin' 用户类型
user_id int 用户 ID
role_id int 角色 ID
data_scope json, nullable 数据权限范围
expired_at dateTime, nullable 角色过期时间戳
create_time dateTime 创建时间戳
update_time dateTime, nullable 更新时间戳

唯一索引:(user_id, role_id)


目录结构


最佳实践

1. 权限标识命名规范

推荐使用点分格式,体现层级关系:

2. 缓存策略建议

3. 路由通配符使用

权限的 route 字段支持 * 通配符:

4. 防范循环继承

5. 用户模型事件

HasRoles Trait 的 bootHasRoles 方法会在模型 saved/deleted 时自动清除缓存。如果需要在用户模型中定义自己的 boot 方法,务必调用 parent::boot() 或手动 static::bootHasRoles()。

6. 多用户类型场景

7. 与 WiseAdmin 框架集成

在 WiseAdmin 中,wise-auth 已作为核心依赖预装。默认用户模型指向 \app\admin\model\Admin::class,超管 ID 通常通过 config('app.super_admin_ids') 配置。


License

MIT License. 详见 LICENSE 文件。


All versions of wise-auth with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package wiseadmin/wise-auth contains the following files

Loading the files please wait ...