refactor(account): 统一账号管理API、完善权限检查和操作审计
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 6m17s

- 合并 customer_account 和 shop_account 路由到统一的 account 接口
- 新增统一认证接口 (auth handler)
- 实现越权防护中间件和权限检查工具函数
- 新增操作审计日志模型和服务
- 更新数据库迁移 (版本 39: account_operation_log 表)
- 补充集成测试覆盖权限检查和审计日志场景
This commit is contained in:
2026-02-02 17:23:20 +08:00
parent 5851cc6403
commit 80f560df33
58 changed files with 10743 additions and 4915 deletions

View File

@@ -0,0 +1,7 @@
-- 删除账号操作审计日志表的索引
DROP INDEX IF EXISTS idx_account_log_operator;
DROP INDEX IF EXISTS idx_account_log_target;
DROP INDEX IF EXISTS idx_account_log_created;
-- 删除账号操作审计日志表
DROP TABLE IF EXISTS tb_account_operation_log;

View File

@@ -0,0 +1,49 @@
-- 创建账号操作审计日志表
CREATE TABLE tb_account_operation_log (
id BIGSERIAL PRIMARY KEY,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
-- 操作主体
operator_id BIGINT NOT NULL, -- 操作人 ID
operator_type INT NOT NULL, -- 操作人类型 (1=超管 2=平台 3=代理 4=企业)
operator_name VARCHAR(255) NOT NULL, -- 操作人用户名
-- 操作对象
target_account_id BIGINT, -- 目标账号 ID可选删除操作后可能查不到
target_username VARCHAR(255), -- 目标账号用户名
target_user_type INT, -- 目标账号类型
-- 操作内容
operation_type VARCHAR(50) NOT NULL, -- create/update/delete/assign_roles/remove_role
operation_desc TEXT NOT NULL, -- 操作描述(中文)
-- 变更详情JSON 格式)
before_data JSONB, -- 变更前数据update 操作)
after_data JSONB, -- 变更后数据create/update 操作)
-- 请求上下文
request_id VARCHAR(255), -- 请求 ID关联访问日志
ip_address VARCHAR(50), -- 操作 IP
user_agent TEXT -- User-Agent
);
-- 创建索引优化查询性能
CREATE INDEX idx_account_log_operator ON tb_account_operation_log(operator_id, created_at);
CREATE INDEX idx_account_log_target ON tb_account_operation_log(target_account_id, created_at);
CREATE INDEX idx_account_log_created ON tb_account_operation_log(created_at DESC);
-- 添加表注释
COMMENT ON TABLE tb_account_operation_log IS '账号操作审计日志表';
COMMENT ON COLUMN tb_account_operation_log.operator_id IS '操作人ID';
COMMENT ON COLUMN tb_account_operation_log.operator_type IS '操作人类型: 1=超级管理员 2=平台用户 3=代理账号 4=企业账号';
COMMENT ON COLUMN tb_account_operation_log.operator_name IS '操作人用户名';
COMMENT ON COLUMN tb_account_operation_log.target_account_id IS '目标账号ID';
COMMENT ON COLUMN tb_account_operation_log.target_username IS '目标账号用户名';
COMMENT ON COLUMN tb_account_operation_log.target_user_type IS '目标账号类型';
COMMENT ON COLUMN tb_account_operation_log.operation_type IS '操作类型: create/update/delete/assign_roles/remove_role';
COMMENT ON COLUMN tb_account_operation_log.operation_desc IS '操作描述(中文)';
COMMENT ON COLUMN tb_account_operation_log.before_data IS '变更前数据JSONB格式用于update操作';
COMMENT ON COLUMN tb_account_operation_log.after_data IS '变更后数据JSONB格式用于create/update操作';
COMMENT ON COLUMN tb_account_operation_log.request_id IS '请求ID可关联访问日志';
COMMENT ON COLUMN tb_account_operation_log.ip_address IS '操作来源IP地址';
COMMENT ON COLUMN tb_account_operation_log.user_agent IS '用户代理(浏览器信息)';