Download the PHP package edwinhuish/think-auth without Composer
On this page you can find all versions of the php package edwinhuish/think-auth. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package think-auth
Think Auth (ThinkPHP 6 Package)
基于角色的权限的简洁而灵活的方式。
Contents
- 安装
- 自定义模型
- Role
- Permission
- User
-
使用
- 概念
- 检查用户是否拥有权限
- 使用中间件
- 故障排除
- License
- Contribution guidelines
安装
-
运行
composer require edwinhuish/think-auth
-
使用
php think auth:publish
在项目内新增/config/auth.php
以及在/database/migrations/
内新增迁移文件。 -
配置
config/auth.php
,并按需修改迁移文件,user
表的迁移默认已注释,请根据实际需求修改。 - 运行
php think migrate:run
生成数据表。
自定义模型
Role
使用以下示例在app\model\Role.php
内创建角色模型:
角色模型的主要属性是 name
name
— 角色的唯一名称,用于在应用程序层查找角色信息。 例如:“管理员”,“所有者”,“员工”.-
description
— 该角色的人类可读名称。 不一定是唯一的和可选的。 例如:“用户管理员”,“项目所有者”,“Widget Co.员工”.description
字段是可选的; 他的字段在数据库中是可空的。
Permission
使用以下示例在app\model\Permission.php
内创建角色模型:
“权限”模型与“角色”具有相同的两个属性:
name
— 权限的唯一名称,用于在应用程序层查找权限信息。一般保存的是路由.-
description
— 该权限的描述。 例如:“创建文章”,“查看文件”,“文件管理”等权限.description
字段是可选的; 他的字段在数据库中是可空的。
User
接下来,在现有的User模型中使用UserTrait
特征。 例如:
这将启用与Role的关系,并在User模型中添加以下方法roles() 和 can( $permission )
不要忘记转储composer的自动加载
准备好了.
使用
概念
让我们从创建以下角色
和权限
开始:
接下来,让我们为刚刚创建的两个角色将它们分配给用户。 这很容易:
现在我们只需要为这些角色添加权限:
检查用户是否拥有权限
现在我们可以通过执行以下操作来检查角色和权限:
小提示:本功能在保存用户和角色之间的关系和角色与权限之间的关系,为了避免大量的sql查询是使用了tp的缓存的哦,
如果你更新了他们之间的关系,记得把缓存清理一下。
使用中间件
角色中间件
到目前为止,已经可以很大的满足到后台用户权限管理功能了。
本功能目前比较简单,现在作者正在扩展其他功能.
最后,如果你觉得不错,请start一个吧 你的鼓励是我创作的无限动力.
故障排查
如果你迁移和配置中遇到问题,可直接联企鹅号:171336747
License
think auth is free software distributed under the terms of the MIT license.
Contribution guidelines
Support follows PSR-1 and PSR-4 PHP coding standards, and semantic versioning.
Please report any issue you find in the issues page.
Pull requests are welcome.
All versions of think-auth with dependencies
topthink/framework Version ^6.0.0
topthink/think-orm Version ^2.0
topthink/think-migration Version ^3.0