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

163 lines
5.7 KiB
TypeScript
Raw Normal View History

2026-05-31 01:41:33 +08:00
import { ALL, Body, Controller, Inject, Post, Provide } from "@midwayjs/core";
import { AccessGetter, AccessService, BaseController, Constants } from "@certd/lib-server";
import { AccessRequestHandleReq, IAccessService, ITaskPlugin, newAccess, newNotification, NotificationRequestHandleReq, pluginRegistry, PluginRequestHandleReq, TaskInstanceContext } from "@certd/pipeline";
import { EmailService } from "../../../modules/basic/service/email-service.js";
import { http, HttpRequestConfig, logger, mergeUtils, utils } from "@certd/basic";
import { NotificationService } from "../../../modules/pipeline/service/notification-service.js";
import { TaskServiceBuilder } from "../../../modules/pipeline/service/getter/task-service-getter.js";
import { cloneDeep } from "lodash-es";
import { ApiTags } from "@midwayjs/swagger";
import { AuthService } from "../../../modules/sys/authority/service/auth-service.js";
2024-09-27 02:15:41 +08:00
2024-09-20 19:29:16 +08:00
@Provide()
2026-05-31 01:41:33 +08:00
@Controller("/api/pi/handle")
@ApiTags(["pipeline-handle"])
2024-09-20 19:29:16 +08:00
export class HandleController extends BaseController {
2024-09-27 02:15:41 +08:00
@Inject()
accessService: AccessService;
@Inject()
emailService: EmailService;
2026-03-16 23:27:24 +08:00
@Inject()
authService: AuthService;
@Inject()
taskServiceBuilder: TaskServiceBuilder;
2025-03-21 12:23:59 +08:00
2024-11-25 11:35:16 +08:00
@Inject()
notificationService: NotificationService;
2026-05-31 01:41:33 +08:00
@Post("/access", { description: Constants.per.authOnly, summary: "处理授权请求" })
2024-09-27 02:15:41 +08:00
async accessRequest(@Body(ALL) body: AccessRequestHandleReq) {
2026-05-31 01:41:33 +08:00
let { projectId, userId } = await this.getProjectUserIdRead();
if (body.fromType === "sys") {
2026-03-16 23:27:24 +08:00
//系统级别的请求
const pass = await this.authService.checkPermission(this.ctx, "sys:settings:view");
if (!pass) {
2026-05-31 01:41:33 +08:00
throw new Error("权限不足");
2026-03-16 23:27:24 +08:00
}
2026-05-31 01:41:33 +08:00
projectId = null;
userId = 0;
2026-03-16 23:27:24 +08:00
}
2026-05-31 01:41:33 +08:00
2026-02-15 12:59:08 +08:00
let inputAccess = body.input;
if (body.record.id > 0) {
const oldEntity = await this.accessService.info(body.record.id);
2024-09-30 18:00:51 +08:00
if (oldEntity) {
2026-03-16 23:27:24 +08:00
if (oldEntity.userId !== userId && oldEntity.userId !== this.getUserId()) {
2026-05-31 01:41:33 +08:00
throw new Error("您没有权限使用该授权");
2024-11-25 11:35:16 +08:00
}
2026-02-13 21:28:17 +08:00
if (oldEntity.projectId && oldEntity.projectId !== projectId) {
2026-05-31 01:41:33 +08:00
throw new Error("您没有权限使用该授权(projectId不匹配)");
2026-02-13 21:28:17 +08:00
}
2024-09-30 18:00:51 +08:00
const param: any = {
2024-09-27 02:15:41 +08:00
type: body.typeName,
2026-02-15 12:59:08 +08:00
setting: JSON.stringify(body.input),
2024-09-27 02:15:41 +08:00
};
this.accessService.encryptSetting(param, oldEntity);
2024-09-30 18:00:51 +08:00
inputAccess = this.accessService.decryptAccessEntity(param);
2024-09-20 19:29:16 +08:00
}
}
const getAccessById = this.accessService.getById.bind(this.accessService);
const accessGetter = new AccessGetter(userId, projectId, getAccessById);
const serviceGetter = this.taskServiceBuilder.create({ userId, projectId });
const accessContext = {
http,
logger,
utils,
accessService: accessGetter,
serviceGetter,
define: undefined,
} as any;
const access = await newAccess(body.typeName, inputAccess, accessGetter, accessContext);
2024-10-01 23:52:44 +08:00
2026-02-15 12:59:08 +08:00
// mergeUtils.merge(access, body.input);
2024-10-01 23:52:44 +08:00
const res = await access.onRequest(body);
2024-09-27 02:15:41 +08:00
return this.ok(res);
}
2026-05-31 01:41:33 +08:00
@Post("/notification", { description: Constants.per.authOnly, summary: "处理通知请求" })
2024-11-25 11:35:16 +08:00
async notificationRequest(@Body(ALL) body: NotificationRequestHandleReq) {
const { projectId, userId } = await this.getProjectUserIdRead();
2026-02-15 12:59:08 +08:00
const input = body.input;
const serviceGetter = this.taskServiceBuilder.create({ userId, projectId });
2024-11-25 11:35:16 +08:00
const notification = await newNotification(body.typeName, input, {
2024-11-25 11:35:16 +08:00
http,
logger,
utils,
emailService: this.emailService,
serviceGetter,
} as any);
2024-11-25 11:35:16 +08:00
const res = await notification.onRequest(body);
return this.ok(res);
}
2026-05-31 01:41:33 +08:00
@Post("/plugin", { description: Constants.per.authOnly, summary: "处理插件请求" })
2024-09-27 02:15:41 +08:00
async pluginRequest(@Body(ALL) body: PluginRequestHandleReq) {
2026-05-31 01:41:33 +08:00
const { projectId, userId } = await this.getProjectUserIdRead();
2024-09-27 02:15:41 +08:00
const pluginDefine = pluginRegistry.get(body.typeName);
const pluginCls = await pluginDefine.target();
2024-09-27 02:15:41 +08:00
if (pluginCls == null) {
throw new Error(`plugin ${body.typeName} not found`);
}
//实例化access
//@ts-ignore
const plugin: PluginRequestHandler = new pluginCls();
//@ts-ignore
const instance = plugin as ITaskPlugin;
2024-10-09 02:34:28 +08:00
2024-11-04 16:39:02 +08:00
const download = async (config: HttpRequestConfig, savePath: string) => {
await utils.download({
http,
logger,
config,
savePath,
});
};
2025-03-21 12:23:59 +08:00
2026-05-31 01:41:33 +08:00
const taskServiceGetter = this.taskServiceBuilder.create({ userId, projectId });
2026-05-31 01:41:33 +08:00
const accessGetter = await taskServiceGetter.get<IAccessService>("accessService");
2024-09-27 02:15:41 +08:00
//@ts-ignore
const taskCtx: TaskInstanceContext = {
pipeline: undefined,
step: undefined,
2026-05-31 01:41:33 +08:00
define: cloneDeep(pluginDefine.define),
2024-09-27 02:15:41 +08:00
lastStatus: undefined,
http,
2024-11-04 16:39:02 +08:00
download,
2024-09-27 02:15:41 +08:00
logger: logger,
inputChanged: true,
2024-10-09 02:34:28 +08:00
accessService: accessGetter,
2024-09-27 02:15:41 +08:00
emailService: this.emailService,
pipelineContext: undefined,
userContext: undefined,
fileStore: undefined,
signal: undefined,
2026-05-31 01:41:33 +08:00
user: { id: userId, role: "user" },
2026-02-13 21:28:17 +08:00
projectId,
2024-09-27 02:15:41 +08:00
// pipelineContext: this.pipelineContext,
// userContext: this.contextFactory.getContext('user', this.options.userId),
// fileStore: new FileStore({
// scope: this.pipeline.id,
// parent: this.runtime.id,
// rootDir: this.options.fileRootDir,
// }),
// signal: this.abort.signal,
utils,
2026-05-31 01:41:33 +08:00
serviceGetter: taskServiceGetter,
} as any;
await instance.setCtx(taskCtx);
2024-09-27 02:15:41 +08:00
mergeUtils.merge(plugin, body.input);
await instance.onInstance();
const res = await plugin.onRequest(body);
return this.ok(res);
2024-09-20 19:29:16 +08:00
}
}