安全(P0) - 发布包签名:新增 scripts/package_keygen.php、scripts/package_verify.php、 ywxapp\service\PackageVerifier;FrameworkService::apply() 解压前强制验签 (ywxapp.framework_package_verify,默认开启) - JWT 收紧:config/jwt.php 移除公开默认密钥,JwtService::assertStrongSecret() 校验非空 / 长度≥32 / 已知弱值;.env.production.example 键名由 JWT_SECRET_KEY 修正为 JWT_SECRET - /api/v1/Article:免登录白名单收窄为只读接口,写操作字段白名单 + assertOwner() 越权校验 - 开发者令牌:查找接口不再回显明文、统一失败文案 + 限流 - TemplateInstaller:文件名白名单 + realpath 归一化 + 目录内断言,修复 Zip Slip;补 use ZipArchive - 后台登录:接入验证码、失败锁定与登录限流(config/ratelimit.php) - 缓存 / Session 驱动可由 .env 覆盖(CACHE_DRIVER / SESSION_TYPE),集群部署改 redis - paycenter 支付日志开关改为 PAY_LOG,不再跟随 APP_DEBUG 工程化 / 交付 - 新增 CHANGELOG.md 与 .env.production.example;composer.lock 入库锁定依赖版本 - 插件离线安装 CLI 增加 --uid,后台上传安装透传 uid,付费插件授权校验可通过 - 授权拒绝文案区分「未携带 uid」与「未找到有效授权」,便于排障 - .gitignore 放行 .env.production.example / .env.addon.example / .gitattributes / public/.htaccess 结构 - appmall 由 addon/appmall 迁移为独立应用 app/appmall(市场 / 开发者中心 / 审核 / 分润)
323 lines
10 KiB
PHP
323 lines
10 KiB
PHP
<?php
|
|
/*
|
|
* @Author: YwxApp <ywx@ywxapp.cn>
|
|
* @Date: 2026-04-29 02:32:33
|
|
* @LastEditors: YwxApp <ywx@ywxapp.cn>
|
|
* @LastEditTime: 2026-07-23 18:17:36
|
|
* @Description:
|
|
* @FilePath: \ywxapp_dev\app\backend\controller\Power.php
|
|
* @CustomString: Copyright (c) 2026 YwxApp
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\backend\controller;
|
|
|
|
use app\backend\service\PowerService;
|
|
use app\backend\validate\Power as PowerValidate;
|
|
use think\exception\ValidateException;
|
|
use think\facade\Db;
|
|
use think\facade\Log;
|
|
use think\facade\View;
|
|
use think\Request;
|
|
use think\Response;
|
|
use ywxapp\controller\BackendBase;
|
|
use ywxapp\model\BackendPower as PowerModel;
|
|
use ywxapp\model\BackendRolePower;
|
|
|
|
/**
|
|
* Power 类
|
|
*
|
|
* @author ywxapp <admin@ywxapp.cn>
|
|
*/
|
|
class Power extends BackendBase
|
|
{
|
|
|
|
/**
|
|
* Summary of needLogin
|
|
* @var array
|
|
*/
|
|
protected $noNeedLogin = [];
|
|
|
|
/**
|
|
* Summary of needRight
|
|
* @var array
|
|
*/
|
|
protected $noNeedVerify = [];
|
|
|
|
/**
|
|
* 控制器初始化 initialize
|
|
* @return void
|
|
*/
|
|
protected function initialize()
|
|
{
|
|
$this->model = new PowerModel();
|
|
$this->view->assign('title', '权限管理');
|
|
}
|
|
|
|
/**
|
|
* 显示资源列表
|
|
*
|
|
* @param \think\Request $request
|
|
* @return \think\Response
|
|
*/
|
|
public function index()
|
|
{
|
|
$page = $this->request->param('page', 1);
|
|
$size = $this->request->param('size',300);
|
|
if ($this->request->isAjax()) {
|
|
$data = PowerModel::cache(false)->paginate([
|
|
'page' => $page,
|
|
'list_rows' => $size,
|
|
]);
|
|
$this->result->success($data->items());
|
|
}
|
|
return view('index', [
|
|
'name' => 'ThinkPHP',
|
|
'email' => 'thinkphp@qq.com'
|
|
]);
|
|
return $this->view->fetch();
|
|
}
|
|
|
|
/**
|
|
* 创建权限
|
|
*
|
|
* @param \think\Request $request
|
|
* @return \think\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
if ($this->request->isAjax()) {
|
|
$power = PowerModel::cateTree(PowerModel::select()->toArray());
|
|
$this->result->success(['power' => $power]);
|
|
}
|
|
//
|
|
}
|
|
|
|
/**
|
|
* 保存权限
|
|
*
|
|
* @param \think\Request $request
|
|
* @return \think\Response
|
|
*/
|
|
public function save()
|
|
{
|
|
if ($this->request->isPost()) {
|
|
$params = $this->request->post();
|
|
validate(PowerValidate::class)->check($params);
|
|
try {
|
|
Db::transaction(function () use ($params) {
|
|
$data = PowerModel::create($params);
|
|
});
|
|
$this->result->success($params, '保存成功');
|
|
} catch (ValidateException $e) {
|
|
$this->result->error('保存失败: ' . $e->getMessage(), 2, $params);
|
|
} catch (\Throwable $th) {
|
|
$this->result->error('保存失败: ' . $th->getMessage(), 1, $params);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 显示编辑权限表单页.
|
|
*
|
|
* @param \think\Request $request
|
|
* @return \think\Response
|
|
*/
|
|
public function edit($id = null)
|
|
{
|
|
$id = $this->request->param('id');
|
|
$power = PowerModel::find($id);
|
|
if (! $power) {
|
|
$this->result->error('权限不存在');
|
|
}
|
|
if ($this->request->isAjax()) {
|
|
|
|
$powers = PowerModel::cateTree(PowerModel::select()->toArray());
|
|
$this->result->success(['power' => $powers, 'info' => $power]);
|
|
}
|
|
//View::assign('power', $power);
|
|
//return View::fetch();
|
|
}
|
|
|
|
/**
|
|
* 显示编辑权限表单页.
|
|
*
|
|
* @param \think\Request $request
|
|
* @return \think\Response
|
|
*/
|
|
public function update($id = null)
|
|
{
|
|
$id = $id ? $id : $this->request->param('id');
|
|
if ($this->request->isAjax() && $this->request->isPut()) {
|
|
$params = $this->request->param();
|
|
validate(PowerValidate::class)->check($params);
|
|
try {
|
|
$res = Db::transaction(function () use ($params, $id) {
|
|
$power = PowerModel::find($id);
|
|
if (! $power) {
|
|
throw new ValidateException('权限不存在');
|
|
}
|
|
$power->save($params);
|
|
});
|
|
$this->result->success($res, '更新成功');
|
|
} catch (ValidateException $e) {
|
|
$this->result->error('更新失败: ' . $e->getMessage());
|
|
} catch (\Throwable $th) {
|
|
$this->result->error('更新失败: ' . $th->getMessage());
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 显示回收站列表
|
|
*
|
|
* @param \think\Request $request
|
|
* @return \think\Response
|
|
*/
|
|
public function recyclebin()
|
|
{
|
|
$page = $this->request->param('page', 1);
|
|
$size = $this->request->param('size', 15);
|
|
if ($this->request->isAjax()) {
|
|
$data = PowerModel::onlyTrashed()->paginate([
|
|
'page' => $page,
|
|
'list_rows' => $size,
|
|
]);
|
|
$this->result->success($data->items());
|
|
}
|
|
View::assign('title', '回收站');
|
|
return View::fetch('power/index');
|
|
}
|
|
|
|
/**
|
|
* 删除权限
|
|
*
|
|
* @param \think\Request $request
|
|
* @return \think\Response
|
|
*/
|
|
public function delete()
|
|
{
|
|
if ($this->request->isAjax() && $this->request->isDelete()) {
|
|
$ids = $this->request->param('ids', '');
|
|
$force = $this->request->param('force', false);
|
|
if (empty($ids)) {
|
|
$this->result->error('请选择要删除的数据');
|
|
}
|
|
$ids = is_array($ids) ? $ids : explode(',', (string) $ids);
|
|
$ids = array_values(array_filter(array_map('intval', $ids)));
|
|
if (empty($ids)) {
|
|
$this->result->error('参数错误:ids');
|
|
}
|
|
try {
|
|
Db::transaction(function () use ($ids, $force) {
|
|
// 取出待删行:软删取正常行,强制删取回收站行
|
|
$rows = $force
|
|
? PowerModel::onlyTrashed()->whereIn('id', $ids)->select()
|
|
: PowerModel::whereIn('id', $ids)->select();
|
|
|
|
if ($rows->isEmpty()) {
|
|
return;
|
|
}
|
|
|
|
// 防护①:核心后台菜单/权限节点(addon 为空)禁止删除。
|
|
// 这些节点是后台菜单树与权限校验的基础,误删会导致后台菜单消失、
|
|
// 权限节点丢失(历史上出现过整表被清空、登录后 menus 为空的事故)。
|
|
$coreNames = [];
|
|
foreach ($rows as $row) {
|
|
if (empty($row->addon)) {
|
|
$coreNames[] = (string) $row->name;
|
|
}
|
|
}
|
|
if ($coreNames) {
|
|
throw new \RuntimeException(
|
|
'核心后台菜单禁止删除(会导致后台菜单与权限节点丢失):' . implode('、', $coreNames)
|
|
);
|
|
}
|
|
|
|
// 防护②:删除前落快照日志,误删后可按日志恢复
|
|
Log::warning('[Power] 删除权限节点', [
|
|
'admin_id' => $this->auth->info['id'] ?? 0,
|
|
'account' => $this->auth->info['account'] ?? '',
|
|
'force' => (bool) $force,
|
|
'count' => count($rows),
|
|
'ip' => $this->request->ip(),
|
|
'snapshot' => $rows->toArray(),
|
|
]);
|
|
|
|
foreach ($rows as $item) {
|
|
$force ? $item->force()->delete() : $item->delete();
|
|
}
|
|
});
|
|
$this->result->success();
|
|
} catch (\think\exception\HttpResponseException $e) {
|
|
throw $e; // 不要 return,不要吞掉!
|
|
} catch (\Throwable $th) {
|
|
$this->result->error('删除失败: ' . $th->getMessage());
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 还原权限
|
|
*
|
|
* @param \think\Request $request
|
|
* @return \think\Response
|
|
*/
|
|
public function restore($ids = null)
|
|
{
|
|
if ($this->request->isAjax() && $this->request->isPut()) {
|
|
$ids = $this->request->param('ids', '');
|
|
if (empty($ids)) {
|
|
$this->result->error('请选择要还原的数据');
|
|
}
|
|
$idsArray = explode(',', $ids);
|
|
try {
|
|
Db::transaction(function () use ($idsArray) {
|
|
PowerModel::onlyTrashed()->whereIn('id', $idsArray)->select()->each(function ($item) {
|
|
$item->restore();
|
|
});
|
|
});
|
|
$this->result->success();
|
|
} catch (\think\exception\HttpResponseException $e) {
|
|
throw $e; // 不要 return,不要吞掉!
|
|
} catch (\Throwable $th) {
|
|
$this->result->error('还原失败: ' . $th->getMessage());
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// 更新角色权限
|
|
|
|
public function updateRolePowers($roleId): Response
|
|
{
|
|
$powerIds = $this->request->post('powerIds', []);
|
|
|
|
Db::startTrans();
|
|
try {
|
|
// 删除旧的角色权限关联
|
|
BackendRolePower::where('role_id', $roleId)->delete();
|
|
|
|
// 添加新的角色权限关联
|
|
if (! empty($powerIds)) {
|
|
$rolePowerData = [];
|
|
foreach ($powerIds as $powerId) {
|
|
$rolePowerData[] = [
|
|
'role_id' => $roleId,
|
|
'power_id' => $powerId,
|
|
'create_at' => date('Y-m-d H:i:s'),
|
|
];
|
|
}
|
|
(new BackendRolePower())->saveAll($rolePowerData);
|
|
}
|
|
|
|
Db::commit();
|
|
return json(['code' => 200, 'message' => '权限分配成功']);
|
|
} catch (\Exception $e) {
|
|
Db::rollback();
|
|
return json(['code' => 500, 'message' => '权限分配失败: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
}
|