mradang / fly
This package is abandoned and no longer maintained.
No replacement package was suggested.
lumen extend
v2.5.0
2019-07-18 17:27 UTC
Requires
- firebase/php-jwt: ^5.0
README
实现基于路由的权限控制、跨域请求控制、添加助手函数、添加Service异常类、添加快捷脚本
安装
composer require mradang/fly
配置
- 修改 bootstrap\app.php 文件
// 开启 Facades 和 Eloquent $app->withFacades(); $app->withEloquent(); // 注册 ServiceProvider $app->register(mradang\LumenFly\LumenFlyServiceProvider::class);
- 修改 app\Exceptions\Handler.php 文件
protected $dontReport = [ // ...... \App\Services\Exception::class, ]; public function render($request, Exception $exception) { // 将App异常改为http400错误输出 if ($exception instanceof \App\Services\Exception) { return response($exception->getMessage(), 400); } return parent::render($request, $exception); }
- 添加 .env 环境变量,使用默认值时可省略
# 指定 token 的有效时间(单位秒),默认 24 小时(60*60*24=86400)
FLY_JWT_TTL=86400
# 指定 LumenFly 的 route 路径前缀,默认 fly(http://hostname/fly/路由)
FLY_ROUTE_URI=fly
# 指定允许跨域请求的站点,多个站点用 | 分隔
FLY_CORS_ALLOW_ORIGIN=http://localhost
# 指定用户模型类,实现 RBAC 的关键配置
FLY_USER_MODEL=\App\User
# 记录 SQL 日志,默认 false
FLY_SQL_LOG=false
# 可信任的反向代理地址(CIDR),多个地址用 | 分隔,默认为空字符串
FLY_TRUSTED_PROXIES=
- user 数据表必须包含字段:id, name, secret
$table->increments('id'); $table->string('name'); $table->string('secret')->nullable();
- user 模型需引入 UserModelTrait
use mradang\LumenFly\Traits\UserModelTrait;
增加以下内容:
- belongsToMany rbacRoles 角色关联(多对多)
- array getAccessAttribute 权限属性 access,user 模型需实现 getIsAdminAttribute(超级管理员)属性
- string rbacMakeToken(array $fields = ['id', 'name']) 生成用户访问令牌
- bool rbacResetSecret() 重置用户安全码
- void rbacSyncRoles(array $roles) 同步用户与角色的关联,$roles 为角色 id 数组
- void rbacDeleteUser() 删除用户权限信息
- 手动添加日志迁移到文件的任务
修改 lumen 工程 app\Console\Kernel.php 文件,在 schedule 函数中增加
try { $schedule ->call(function () { \mradang\LumenFly\Services\LogService::migrateToFile(); }) ->cron('0 0 2 * *') ->name('LogService::migrateToFile') ->withoutOverlapping() ->after(function () { L('Kernel.schedule 迁移日志到文件成功', 'sys'); }); } catch (\Exception $e) { L(sprintf('Kernel.schedule 迁移日志到文件失败:%s', $e->getMessage()), 'sys'); }
- 刷新数据库迁移
php artisan migrate:refresh
- 拷贝快捷脚本到 lumen 工程目录
php artisan fly:publish --force
添加的数据表迁移
- rbac_access
- rbac_node
- rbac_role_user
- rbac_role
- logs
- options
添加的路由
- get /fly/rbac/allNodes
- get /fly/rbac/allNodesWithRole
- get /fly/rbac/refreshNodes
- get /fly/rbac/allRoles
- post /fly/rbac/createRole
- post /fly/rbac/findRoleWithNodes
- post /fly/rbac/syncRoleNodes
- post /fly/rbac/updateRole
- post /fly/rbac/deleteRole
- post /fly/rbac/saveRoleSort
- post /fly/log/lists
添加的路由中间件
- auth 需要授权的路由
- auth.basic 只需登录的路由
二选一即可
添加的助手函数
- 数据库日志,用于记录用户操作
void L($msg, $username = null)
- 调试函数,使用 LOG 类输出 debug 级别日志
void debug(mixed $value1[, mixed $value2[, mixed $...]])
- 生成模型更改信息,模型数据变更但未保存时调用
string change_log($model)
- key/value 读写
mixed option(string $key[, mixed $value])
添加的命令
- 生成路由描述文件:storage/app/route_desc.json
php artisan fly:MakeRouteDescFile
- 刷新路由节点及描述
php artisan fly:RefreshRbacNode