Files
certd/packages/ui/certd-server/src/controller/user/pipeline/access-controller.ts
T

144 lines
5.0 KiB
TypeScript
Raw Normal View History

2026-05-31 01:41:33 +08:00
import { ALL, Body, Controller, Inject, Post, Provide, Query } from "@midwayjs/core";
import { Constants, CrudController } from "@certd/lib-server";
import { AccessService } from "@certd/lib-server";
import { AuthService } from "../../../modules/sys/authority/service/auth-service.js";
import { AccessDefine } from "@certd/pipeline";
import { ApiTags } from "@midwayjs/swagger";
2026-07-12 02:29:54 +08:00
import { AuditType } from "../../../modules/sys/enterprise/service/audit-constants.js";
2023-01-29 13:44:19 +08:00
/**
* 授权
*/
@Provide()
2026-05-31 01:41:33 +08:00
@ApiTags(["pipeline-access"])
@Controller("/api/pi/access")
2023-05-23 18:01:20 +08:00
export class AccessController extends CrudController<AccessService> {
2023-01-29 13:44:19 +08:00
@Inject()
service: AccessService;
2024-11-20 18:12:10 +08:00
@Inject()
authService: AuthService;
2024-10-03 22:03:49 +08:00
getService(): AccessService {
2023-01-29 13:44:19 +08:00
return this.service;
}
2026-07-12 02:29:54 +08:00
getAuditType(): string {
2026-07-13 00:53:19 +08:00
return AuditType.access.value;
2026-07-12 02:29:54 +08:00
}
2026-05-31 01:41:33 +08:00
@Post("/page", { description: Constants.per.authOnly, summary: "查询授权配置分页列表" })
2023-01-29 13:44:19 +08:00
async page(@Body(ALL) body) {
2026-05-31 01:41:33 +08:00
const { projectId, userId } = await this.getProjectUserIdRead();
2023-01-29 13:44:19 +08:00
body.query = body.query ?? {};
2024-10-14 00:19:55 +08:00
delete body.query.userId;
2026-02-13 21:28:17 +08:00
body.query.userId = userId;
body.query.projectId = projectId;
2026-05-31 01:41:33 +08:00
const name = body.query?.name;
2025-07-28 23:36:10 +08:00
delete body.query.name;
2024-10-14 00:19:55 +08:00
const buildQuery = qb => {
2025-07-28 23:36:10 +08:00
if (name) {
2026-05-31 01:41:33 +08:00
qb.andWhere("name like :name", { name: `%${name.trim()}%` });
2025-07-28 23:36:10 +08:00
}
2024-10-14 00:19:55 +08:00
};
const res = await this.service.page({
query: body.query,
page: body.page,
2024-10-14 14:00:24 +08:00
sort: body.sort,
2024-10-14 00:19:55 +08:00
buildQuery,
});
return this.ok(res);
2023-01-29 13:44:19 +08:00
}
2026-05-31 01:41:33 +08:00
@Post("/list", { description: Constants.per.authOnly, summary: "查询授权配置列表" })
2023-01-29 13:44:19 +08:00
async list(@Body(ALL) body) {
2026-05-31 01:41:33 +08:00
const { projectId, userId } = await this.getProjectUserIdRead();
body.query = body.query ?? {};
2026-02-13 21:28:17 +08:00
body.query.userId = userId;
body.query.projectId = projectId;
2023-01-29 13:44:19 +08:00
return super.list(body);
}
2026-05-31 01:41:33 +08:00
@Post("/add", { description: Constants.per.authOnly, summary: "添加授权配置" })
2023-01-29 13:44:19 +08:00
async add(@Body(ALL) bean) {
2026-05-31 01:41:33 +08:00
const { projectId, userId } = await this.getProjectUserIdWrite();
2026-02-13 21:28:17 +08:00
bean.userId = userId;
bean.projectId = projectId;
2026-07-10 19:24:42 +08:00
const res = await super.add(bean);
this.auditLog({
content: `新增了授权「${bean.name}」(ID:${res.data}, 类型:${bean.type})`,
});
return res;
2023-01-29 13:44:19 +08:00
}
2026-05-31 01:41:33 +08:00
@Post("/update", { description: Constants.per.authOnly, summary: "更新授权配置" })
2023-01-29 13:44:19 +08:00
async update(@Body(ALL) bean) {
2026-02-13 21:28:17 +08:00
await this.checkOwner(this.getService(), bean.id, "write");
delete bean.userId;
2026-02-13 21:28:17 +08:00
delete bean.projectId;
2026-07-10 19:24:42 +08:00
const res = await super.update(bean);
this.auditLog({
content: `修改了授权「${bean.name}」(ID:${bean.id})`,
});
return res;
2023-01-29 13:44:19 +08:00
}
2026-05-31 01:41:33 +08:00
@Post("/info", { description: Constants.per.authOnly, summary: "查询授权配置详情" })
async info(@Query("id") id: number) {
2026-02-13 21:28:17 +08:00
await this.checkOwner(this.getService(), id, "read");
2023-01-29 13:44:19 +08:00
return super.info(id);
}
2026-05-31 01:41:33 +08:00
@Post("/delete", { description: Constants.per.authOnly, summary: "删除授权配置" })
async delete(@Query("id") id: number) {
2026-02-13 21:28:17 +08:00
await this.checkOwner(this.getService(), id, "write");
2026-07-10 19:24:42 +08:00
const res = await super.delete(id);
this.auditLog({
content: `删除了授权(ID:${id})`,
});
return res;
2023-01-29 13:44:19 +08:00
}
2026-05-31 01:41:33 +08:00
@Post("/define", { description: Constants.per.authOnly, summary: "查询授权插件定义" })
async define(@Query("type") type: string) {
2024-03-22 00:50:02 +08:00
const access = this.service.getDefineByType(type);
return this.ok(access);
2023-01-29 13:44:19 +08:00
}
2026-05-31 01:41:33 +08:00
@Post("/getSecretPlain", { description: Constants.per.authOnly, summary: "获取授权配置明文密钥" })
2024-10-28 18:20:10 +08:00
async getSecretPlain(@Body(ALL) body: { id: number; key: string }) {
2026-05-31 01:41:33 +08:00
const { userId, projectId } = await this.checkOwner(this.getService(), body.id, "read");
const value = await this.service.getById(body.id, userId, projectId);
2024-10-28 18:20:10 +08:00
return this.ok(value[body.key]);
}
2026-05-31 01:41:33 +08:00
@Post("/accessTypeDict", { description: Constants.per.authOnly, summary: "查询授权类型字典" })
2023-01-29 13:44:19 +08:00
async getAccessTypeDict() {
2025-04-28 23:34:08 +08:00
let list: AccessDefine[] = this.service.getDefineList();
2026-05-31 01:41:33 +08:00
list = list.sort((a, b) => {
return (a.order ?? 10) - (b.order ?? 10);
2025-04-28 23:34:08 +08:00
});
2023-01-29 13:44:19 +08:00
const dict = [];
for (const item of list) {
dict.push({
value: item.name,
label: item.title,
2024-11-30 01:57:09 +08:00
icon: item.icon,
2023-01-29 13:44:19 +08:00
});
}
return this.ok(dict);
}
2024-11-20 18:12:10 +08:00
2026-05-31 01:41:33 +08:00
@Post("/simpleInfo", { description: Constants.per.authOnly, summary: "查询授权配置简单信息" })
async simpleInfo(@Query("id") id: number) {
2026-02-13 21:28:17 +08:00
// await this.authService.checkUserIdButAllowAdmin(this.ctx, this.service, id);
2026-03-03 23:31:42 +08:00
// await this.checkOwner(this.getService(), id, "read",true);
2024-11-20 18:12:10 +08:00
const res = await this.service.getSimpleInfo(id);
return this.ok(res);
}
2025-07-12 23:00:04 +08:00
2026-05-31 01:41:33 +08:00
@Post("/getDictByIds", { description: Constants.per.authOnly, summary: "根据ID列表获取授权配置字典" })
async getDictByIds(@Body("ids") ids: number[]) {
const { userId, projectId } = await this.getProjectUserIdRead();
2026-02-13 21:28:17 +08:00
const res = await this.service.getSimpleByIds(ids, userId, projectId);
2025-07-12 23:00:04 +08:00
return this.ok(res);
}
2023-01-29 13:44:19 +08:00
}