Files
YwxAppThink/ywxapp/library/AdminLogger.php
T
yangwx 18101cf11a feat: 上线前安全加固与发布工程化;appmall 由插件迁移为独立应用
安全(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(市场 / 开发者中心 / 审核 / 分润)
2026-10-22 00:53:39 +08:00

118 lines
3.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// +----------------------------------------------------------------------
// | YwxApp [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: ywxapp<admin@ywxapp.cn>
// +----------------------------------------------------------------------
declare(strict_types=1);
namespace ywxapp\library;
use think\facade\Db;
/**
* 管理员操作日志(backend_log)
*
* 表结构(install.sql 为事实源):id / aid / action / ip / remark / create_at。
* 字段很精简,用法约定:
* - action:`controller/action`(如 `admin/add`)或语义动作(如 `登录`);
* - remark:可读摘要或 JSON 详情(如 `新增管理员:test`);
* - aid :0 表示「未能确定操作人」(如登录失败)。
*
* 所有写入都吞异常:审计日志绝不能反过来阻断业务。
*
* @author ywxapp <admin@ywxapp.cn>
*/
class AdminLogger
{
/**
* 当前登录管理员 ID(后台容器 auth 为 AdminAuth)
* @return int
*/
public static function currentAid(): int
{
try {
return (int) (app()->auth->info['id'] ?? 0);
} catch (\Throwable $e) {
return 0;
}
}
/**
* 写一条操作日志
*
* @param string $action 动作标识,如 admin/add
* @param string $remark 摘要 / JSON 详情
* @param int $aid 操作人;0=自动取当前登录管理员
* @return void
*/
public static function write(string $action, string $remark = '', int $aid = 0): void
{
try {
$aid = $aid > 0 ? $aid : self::currentAid();
Db::name('backend_log')->insert([
'aid' => $aid,
'action' => mb_substr($action, 0, 100),
'ip' => mb_substr(self::ip(), 0, 50),
'remark' => mb_substr($remark, 0, 255),
'create_at' => time(),
]);
} catch (\Throwable $e) {
// 日志写入失败不阻断业务
}
}
/**
* 登录审计(成功 / 失败都记,失败时 aid=0)
*
* @param string $account 账号
* @param bool $ok 是否成功
* @param string $msg 失败原因(成功可留空)
* @param int $aid 成功后的管理员 ID
* @return void
*/
public static function login(string $account, bool $ok, string $msg = '', int $aid = 0): void
{
self::write(
$ok ? '登录成功' : '登录失败',
$ok ? ('账号:' . $account) : ('账号:' . $account . ',原因:' . $msg),
$aid
);
}
/**
* 按天数清理旧日志
*
* @param int $days 保留最近 N 天;0=清空全部
* @return int 删除条数
*/
public static function clean(int $days): int
{
try {
if ($days <= 0) {
// 全表清空:用 TRUNCATE 更快,失败回落 delete
return Db::name('backend_log')->delete(true);
}
return Db::name('backend_log')->where('create_at', '<', time() - $days * 86400)->delete();
} catch (\Throwable $e) {
return 0;
}
}
/**
* 客户端 IP
* @return string
*/
protected static function ip(): string
{
try {
return (string) app()->request->ip();
} catch (\Throwable $e) {
return $_SERVER['REMOTE_ADDR'] ?? '';
}
}
}