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

275 lines
9.3 KiB
TypeScript
Raw Normal View History

2024-10-09 02:34:28 +08:00
import { Constants, CrudController, SysSettingsService } from '@certd/lib-server';
2026-02-11 00:07:29 +08:00
import { isPlus } from '@certd/plus-core';
import { ALL, Body, Controller, Inject, Post, Provide, Query } from '@midwayjs/core';
import { SiteInfoService } from '../../../modules/monitor/index.js';
2025-01-15 01:26:39 +08:00
import { PipelineEntity } from '../../../modules/pipeline/entity/pipeline.js';
import { HistoryService } from '../../../modules/pipeline/service/history-service.js';
2026-02-11 00:07:29 +08:00
import { PipelineService } from '../../../modules/pipeline/service/pipeline-service.js';
2025-01-15 01:26:39 +08:00
import { AuthService } from '../../../modules/sys/authority/service/auth-service.js';
2023-01-29 13:44:19 +08:00
/**
*
*/
@Provide()
@Controller('/api/pi/pipeline')
export class PipelineController extends CrudController<PipelineService> {
2023-01-29 13:44:19 +08:00
@Inject()
service: PipelineService;
2023-07-03 11:45:32 +08:00
@Inject()
historyService: HistoryService;
@Inject()
authService: AuthService;
@Inject()
sysSettingsService: SysSettingsService;
2023-01-29 13:44:19 +08:00
@Inject()
siteInfoService: SiteInfoService;
2026-02-11 00:07:29 +08:00
2023-01-29 13:44:19 +08:00
getService() {
return this.service;
}
2023-06-27 09:29:43 +08:00
@Post('/page', { summary: Constants.per.authOnly })
2023-01-29 13:44:19 +08:00
async page(@Body(ALL) body) {
const isAdmin = await this.authService.isAdmin(this.ctx);
const publicSettings = await this.sysSettingsService.getPublicSettings();
2026-02-11 00:54:56 +08:00
const { projectId, userId } = await this.getProjectUserIdRead()
body.query.projectId = projectId
let onlyOther = false
if (isAdmin) {
if (publicSettings.managerOtherUserPipeline) {
//管理员管理 其他用户
if (body.query.userId === -1) {
//如果只查询其他用户
onlyOther = true;
delete body.query.userId;
}
} else {
2026-02-11 00:07:29 +08:00
body.query.userId = userId;
}
} else {
2026-02-11 00:07:29 +08:00
body.query.userId = userId;
}
const title = body.query.title;
delete body.query.title;
2023-01-29 13:44:19 +08:00
const buildQuery = qb => {
if (title) {
2024-11-20 10:46:05 +08:00
qb.andWhere('(title like :title or content like :content)', { title: `%${title}%`, content: `%${title}%` });
}
if (onlyOther) {
qb.andWhere('user_id != :userId', { userId: this.getUserId() });
}
2023-01-29 13:44:19 +08:00
};
if (!body.sort || !body.sort?.prop) {
body.sort = { prop: 'order', asc: false };
}
2024-10-14 00:19:55 +08:00
const pageRet = await this.getService().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,
});
2024-08-06 10:12:02 +08:00
return this.ok(pageRet);
2023-01-29 13:44:19 +08:00
}
2025-06-23 18:20:49 +08:00
@Post('/getSimpleByIds', { summary: Constants.per.authOnly })
async getSimpleById(@Body(ALL) body) {
2026-02-11 00:54:56 +08:00
const { projectId, userId } = await this.getProjectUserIdRead()
const ret = await this.getService().getSimplePipelines(body.ids, userId, projectId);
2025-06-23 18:20:49 +08:00
return this.ok(ret);
}
2023-06-27 09:29:43 +08:00
@Post('/add', { summary: Constants.per.authOnly })
2023-01-29 13:44:19 +08:00
async add(@Body(ALL) bean: PipelineEntity) {
2026-02-11 00:54:56 +08:00
const { projectId, userId } = await this.getProjectUserIdWrite()
2026-02-11 00:07:29 +08:00
bean.userId = userId
bean.projectId = projectId
2023-01-29 13:44:19 +08:00
return super.add(bean);
}
2023-06-27 09:29:43 +08:00
@Post('/update', { summary: Constants.per.authOnly })
2023-01-29 13:44:19 +08:00
async update(@Body(ALL) bean) {
2026-02-11 18:11:33 +08:00
await this.checkEntityOwner(this.getService(), bean.id,"write");
delete bean.userId;
2023-01-29 13:44:19 +08:00
return super.update(bean);
}
2023-06-27 09:29:43 +08:00
@Post('/save', { summary: Constants.per.authOnly })
async save(@Body(ALL) bean: { addToMonitorEnabled: boolean, addToMonitorDomains: string } & PipelineEntity) {
2026-02-11 18:11:33 +08:00
const { userId } = await this.getProjectUserIdWrite()
2023-01-29 13:44:19 +08:00
if (bean.id > 0) {
2026-02-11 18:11:33 +08:00
await this.checkEntityOwner(this.getService(), bean.id,"write");
} else {
2026-02-11 00:54:56 +08:00
bean.userId = userId;
2023-01-29 13:44:19 +08:00
}
2025-10-24 23:48:32 +08:00
if (!this.isAdmin()) {
2025-10-24 23:48:32 +08:00
// 非管理员用户 不允许设置流水线有效期
delete bean.validTime
}
const { version } = await this.service.save(bean);
//是否增加证书监控
if (bean.addToMonitorEnabled && bean.addToMonitorDomains) {
const sysPublicSettings = await this.sysSettingsService.getPublicSettings();
if (isPlus() && sysPublicSettings.certDomainAddToMonitorEnabled) {
//增加证书监控
await this.siteInfoService.doImport({
text: bean.addToMonitorDomains,
2026-02-11 00:54:56 +08:00
userId: userId,
});
}
}
return this.ok({ id: bean.id, version: version });
2023-01-29 13:44:19 +08:00
}
2023-06-27 09:29:43 +08:00
@Post('/delete', { summary: Constants.per.authOnly })
async delete(@Query('id') id: number) {
2026-02-11 18:11:33 +08:00
await this.checkEntityOwner(this.getService(), id,"write");
2023-06-28 12:46:29 +08:00
await this.service.delete(id);
return this.ok({});
2023-01-29 13:44:19 +08:00
}
@Post('/disabled', { summary: Constants.per.authOnly })
async disabled(@Body(ALL) bean) {
2026-02-11 18:11:33 +08:00
await this.checkEntityOwner(this.getService(), bean.id,"write");
delete bean.userId;
2026-01-07 10:58:17 +08:00
await this.service.disabled(bean.id, bean.disabled);
return this.ok({});
}
2023-06-27 09:29:43 +08:00
@Post('/detail', { summary: Constants.per.authOnly })
async detail(@Query('id') id: number) {
2026-02-11 18:11:33 +08:00
await this.checkEntityOwner(this.getService(), id,"read");
2023-01-29 13:44:19 +08:00
const detail = await this.service.detail(id);
return this.ok(detail);
}
2023-06-27 09:29:43 +08:00
@Post('/trigger', { summary: Constants.per.authOnly })
async trigger(@Query('id') id: number, @Query('stepId') stepId?: string) {
2026-02-11 18:11:33 +08:00
await this.checkEntityOwner(this.getService(), id,"write");
await this.service.trigger(id, stepId, true);
2023-01-29 13:44:19 +08:00
return this.ok({});
}
2023-07-03 11:45:32 +08:00
@Post('/cancel', { summary: Constants.per.authOnly })
async cancel(@Query('historyId') historyId: number) {
2026-02-11 18:11:33 +08:00
await this.checkEntityOwner(this.historyService, historyId,"write");
2023-07-03 11:45:32 +08:00
await this.service.cancel(historyId);
return this.ok({});
}
2024-10-31 22:35:05 +08:00
@Post('/count', { summary: Constants.per.authOnly })
async count() {
2026-02-11 00:54:56 +08:00
const { userId } = await this.getProjectUserIdRead()
const count = await this.service.count({ userId: userId });
2024-10-31 22:35:05 +08:00
return this.ok({ count });
}
2026-02-11 00:54:56 +08:00
private async checkPermissionCall(callback:any){
let { projectId ,userId} = await this.getProjectUserIdWrite()
if(projectId){
return await callback({userId,projectId});
}
const isAdmin = await this.authService.isAdmin(this.ctx);
userId = isAdmin ? undefined : userId;
return await callback({userId});
}
@Post('/batchDelete', { summary: Constants.per.authOnly })
async batchDelete(@Body('ids') ids: number[]) {
2026-02-11 00:54:56 +08:00
// let { projectId ,userId} = await this.getProjectUserIdWrite()
// if(projectId){
// await this.service.batchDelete(ids, null,projectId);
// return this.ok({});
// }
// const isAdmin = await this.authService.isAdmin(this.ctx);
// userId = isAdmin ? undefined : userId;
// await this.service.batchDelete(ids, userId);
// return this.ok({});
await this.checkPermissionCall(async ({userId,projectId})=>{
await this.service.batchDelete(ids, userId,projectId);
})
return this.ok({})
}
2025-12-16 22:31:06 +08:00
@Post('/batchUpdateGroup', { summary: Constants.per.authOnly })
async batchUpdateGroup(@Body('ids') ids: number[], @Body('groupId') groupId: number) {
2026-02-11 00:54:56 +08:00
// let { projectId ,userId} = await this.getProjectUserIdWrite()
// if(projectId){
// await this.service.batchUpdateGroup(ids, groupId, null,projectId);
// return this.ok({});
// }
// const isAdmin = await this.authService.isAdmin(this.ctx);
// userId = isAdmin ? undefined : this.getUserId();
// await this.service.batchUpdateGroup(ids, groupId, userId);
await this.checkPermissionCall(async ({userId,projectId})=>{
await this.service.batchUpdateGroup(ids, groupId, userId,projectId);
})
return this.ok({});
}
2025-05-27 11:08:08 +08:00
@Post('/batchUpdateTrigger', { summary: Constants.per.authOnly })
async batchUpdateTrigger(@Body('ids') ids: number[], @Body('trigger') trigger: any) {
2026-02-11 00:54:56 +08:00
// let { projectId ,userId} = await this.getProjectUserIdWrite()
// if(projectId){
// await this.service.batchUpdateTrigger(ids, trigger, null,projectId);
// return this.ok({});
// }
// const isAdmin = await this.authService.isAdmin(this.ctx);
// userId = isAdmin ? undefined : this.getUserId();
// await this.service.batchUpdateTrigger(ids, trigger, userId);
await this.checkPermissionCall(async ({userId,projectId})=>{
await this.service.batchUpdateTrigger(ids, trigger, userId,projectId);
})
return this.ok({});
}
@Post('/batchUpdateNotification', { summary: Constants.per.authOnly })
async batchUpdateNotification(@Body('ids') ids: number[], @Body('notification') notification: any) {
2026-02-11 00:54:56 +08:00
// const isAdmin = await this.authService.isAdmin(this.ctx);
// const userId = isAdmin ? undefined : this.getUserId();
// await this.service.batchUpdateNotifications(ids, notification, userId);
await this.checkPermissionCall(async ({userId,projectId})=>{
await this.service.batchUpdateNotifications(ids, notification, userId,projectId);
})
return this.ok({});
}
2025-05-27 11:08:08 +08:00
@Post('/batchRerun', { summary: Constants.per.authOnly })
async batchRerun(@Body('ids') ids: number[], @Body('force') force: boolean) {
2026-02-11 00:54:56 +08:00
await this.checkPermissionCall(async ({userId,projectId})=>{
await this.service.batchRerun(ids, force,userId,projectId);
})
2025-05-27 11:08:08 +08:00
return this.ok({});
}
@Post('/refreshWebhookKey', { summary: Constants.per.authOnly })
async refreshWebhookKey(@Body('id') id: number) {
2026-02-11 00:54:56 +08:00
const { projectId } = await this.getProjectUserIdWrite()
if (projectId) {
await this.authService.checkEntityProjectId(this.getService(), projectId, id);
} else {
await this.authService.checkEntityUserId(this.ctx, this.getService(), id);
}
const res = await this.service.refreshWebhookKey(id);
return this.ok({
webhookKey: res,
});
}
2023-01-29 13:44:19 +08:00
}