feat: 支持审计日志,操作日志

This commit is contained in:
xiaojunnuo
2026-07-12 02:29:54 +08:00
parent 4250d0e266
commit f2855d6dac
83 changed files with 1252 additions and 1341 deletions
@@ -1,7 +1,7 @@
import { ALL, Body, Controller, Inject, Post, Provide, Query } from "@midwayjs/core";
import { CrudController } from "@certd/lib-server";
import { PermissionService } from "../../../modules/sys/authority/service/permission-service.js";
import { AuditType, AuditAction } from "../../modules/sys/enterprise/service/audit-constants.js";
import { AuditType } from "../../../modules/sys/enterprise/service/audit-constants.js";
/**
* 权限资源
@@ -15,8 +15,11 @@ export class PermissionController extends CrudController<PermissionService> {
getService() {
return this.service;
}
getAuditType(): string {
return AuditType.permission;
}
@Post("/page", { description: "sys:auth:per:view" })
@Post("/page", { description: "sys:auth:per:view", summary: "查询权限分页列表" })
async page(
@Body(ALL)
body
@@ -24,48 +27,42 @@ export class PermissionController extends CrudController<PermissionService> {
return await super.page(body);
}
@Post("/add", { description: "sys:auth:per:add" })
@Post("/add", { description: "sys:auth:per:add", summary: "添加权限" })
async add(
@Body(ALL)
bean
) {
const res = await super.add(bean);
await this.auditLog({
type: AuditType.permission,
action: AuditAction.add,
content: `新增了权限「${bean.name}」(ID:${res.data})`,
});
return res;
}
@Post("/update", { description: "sys:auth:per:edit" })
@Post("/update", { description: "sys:auth:per:edit", summary: "更新权限" })
async update(
@Body(ALL)
bean
) {
const res = await super.update(bean);
await this.auditLog({
type: AuditType.permission,
action: AuditAction.update,
content: `修改了权限「${bean.name}」(ID:${bean.id})`,
});
return res;
}
@Post("/delete", { description: "sys:auth:per:remove" })
@Post("/delete", { description: "sys:auth:per:remove", summary: "删除权限" })
async delete(
@Query("id")
id: number
) {
const res = await super.delete(id);
await this.auditLog({
type: AuditType.permission,
action: AuditAction.delete,
content: `删除了权限(ID:${id})`,
});
return res;
}
@Post("/tree", { description: "sys:auth:per:view" })
@Post("/tree", { description: "sys:auth:per:view", summary: "查询权限树" })
async tree() {
const tree = await this.service.tree({});
return this.ok(tree);
@@ -1,7 +1,7 @@
import { ALL, Body, Controller, Inject, Post, Provide, Query } from "@midwayjs/core";
import { CrudController } from "@certd/lib-server";
import { RoleService } from "../../../modules/sys/authority/service/role-service.js";
import { AuditType, AuditAction } from "../../modules/sys/enterprise/service/audit-constants.js";
import { AuditType } from "../../../modules/sys/enterprise/service/audit-constants.js";
/**
* 系统用户
@@ -16,6 +16,10 @@ export class RoleController extends CrudController<RoleService> {
return this.service;
}
getAuditType(): string {
return AuditType.role;
}
@Post("/page", { description: "sys:auth:role:view" })
async page(
@Body(ALL)
@@ -30,34 +34,30 @@ export class RoleController extends CrudController<RoleService> {
return this.ok(ret);
}
@Post("/add", { description: "sys:auth:role:add" })
@Post("/add", { description: "sys:auth:role:add", summary: "新增角色" })
async add(
@Body(ALL)
bean
) {
const res = await super.add(bean);
await this.auditLog({
type: AuditType.role,
action: AuditAction.add,
content: `新增了角色「${bean.name}」(ID:${res.data})`,
});
return res;
}
@Post("/update", { description: "sys:auth:role:edit" })
@Post("/update", { description: "sys:auth:role:edit", summary: "修改角色" })
async update(
@Body(ALL)
bean
) {
const res = await super.update(bean);
await this.auditLog({
type: AuditType.role,
action: AuditAction.update,
content: `修改了角色「${bean.name}」(ID:${bean.id})`,
});
return res;
}
@Post("/delete", { description: "sys:auth:role:remove" })
@Post("/delete", { description: "sys:auth:role:remove", summary: "删除角色" })
async delete(
@Query("id")
id: number
@@ -67,8 +67,6 @@ export class RoleController extends CrudController<RoleService> {
}
const res = await super.delete(id);
await this.auditLog({
type: AuditType.role,
action: AuditAction.delete,
content: `删除了角色(ID:${id})`,
});
return res;
@@ -97,9 +95,10 @@ export class RoleController extends CrudController<RoleService> {
* @param roleId
* @param permissionIds
*/
@Post("/authz", { description: "sys:auth:role:edit" })
@Post("/authz", { description: "sys:auth:role:edit", summary: "角色授权" })
async authz(@Body("roleId") roleId, @Body("permissionIds") permissionIds) {
await this.service.authz(roleId, permissionIds);
await this.auditLog({ content: `为角色(ID:${roleId})进行了授权` });
return this.ok(null);
}
}
@@ -6,7 +6,7 @@ import { PermissionService } from "../../../modules/sys/authority/service/permis
import { Constants } from "@certd/lib-server";
import { In } from "typeorm";
import { LoginService } from "../../../modules/login/service/login-service.js";
import { AuditType, AuditAction } from "../../modules/sys/enterprise/service/audit-constants.js";
import { AuditType } from "../../../modules/sys/enterprise/service/audit-constants.js";
/**
* 系统用户
@@ -25,6 +25,10 @@ export class UserController extends CrudController<UserService> {
@Inject()
loginService: LoginService;
getAuditType(): string {
return AuditType.user;
}
getService() {
return this.service;
}
@@ -93,35 +97,31 @@ export class UserController extends CrudController<UserService> {
return ret;
}
@Post("/add", { description: "sys:auth:user:add" })
@Post("/add", { description: "sys:auth:user:add", summary: "新增用户" })
async add(
@Body(ALL)
bean
) {
const res = await super.add(bean);
await this.auditLog({
type: AuditType.user,
action: AuditAction.add,
content: `新增了用户「${bean.username}」(ID:${res.data})`,
});
return res;
}
@Post("/update", { description: "sys:auth:user:edit" })
@Post("/update", { description: "sys:auth:user:edit", summary: "修改用户" })
async update(
@Body(ALL)
bean
) {
const res = await super.update(bean);
await this.auditLog({
type: AuditType.user,
action: AuditAction.update,
content: `修改了用户「${bean.username}」(ID:${bean.id})`,
});
return res;
}
@Post("/delete", { description: "sys:auth:user:remove" })
@Post("/delete", { description: "sys:auth:user:remove", summary: "删除用户" })
async delete(
@Query("id")
id: number
@@ -134,8 +134,6 @@ export class UserController extends CrudController<UserService> {
}
const res = await super.delete(id);
await this.auditLog({
type: AuditType.user,
action: AuditAction.delete,
content: `删除了用户(ID:${id})`,
});
return res;
@@ -144,13 +142,14 @@ export class UserController extends CrudController<UserService> {
/**
* 解除登录锁定
*/
@Post("/unlockBlock", { description: "sys:auth:user:edit" })
@Post("/unlockBlock", { description: "sys:auth:user:edit", summary: "解锁登录锁定" })
public async unlockBlock(@Body("id") id: number) {
const info = await this.service.info(id, ["password"]);
this.loginService.clearCacheOnSuccess(info.username);
if (info.mobile) {
this.loginService.clearCacheOnSuccess(info.mobile);
}
await this.auditLog({ content: `解锁了用户登录锁定(ID:${id})` });
return this.ok(info);
}