chore: 目录调整,controller转移到外部单独的目录

This commit is contained in:
xiaojunnuo
2024-10-13 21:59:29 +08:00
parent ccfe72a0d9
commit 417971d15d
37 changed files with 79 additions and 81 deletions
@@ -0,0 +1,80 @@
import { ALL, Body, Controller, Inject, Post, Provide, Query } from '@midwayjs/core';
import { CrudController } from '@certd/lib-server';
import { AccessService } from '../../modules/pipeline/service/access-service.js';
import { Constants } from '@certd/lib-server';
/**
* 授权
*/
@Provide()
@Controller('/api/pi/access')
export class AccessController extends CrudController<AccessService> {
@Inject()
service: AccessService;
userId() {
return this.getUserId();
}
getService(): AccessService {
return this.service;
}
@Post('/page', { summary: Constants.per.authOnly })
async page(@Body(ALL) body) {
body.query = body.query ?? {};
body.query.userId = this.userId();
return await super.page(body);
}
@Post('/list', { summary: Constants.per.authOnly })
async list(@Body(ALL) body) {
body.userId = this.userId();
return super.list(body);
}
@Post('/add', { summary: Constants.per.authOnly })
async add(@Body(ALL) bean) {
bean.userId = this.userId();
return super.add(bean);
}
@Post('/update', { summary: Constants.per.authOnly })
async update(@Body(ALL) bean) {
await this.service.checkUserId(bean.id, this.userId());
return super.update(bean);
}
@Post('/info', { summary: Constants.per.authOnly })
async info(@Query('id') id: number) {
await this.service.checkUserId(id, this.userId());
return super.info(id);
}
@Post('/delete', { summary: Constants.per.authOnly })
async delete(@Query('id') id: number) {
await this.service.checkUserId(id, this.userId());
return super.delete(id);
}
@Post('/define', { summary: Constants.per.authOnly })
async define(@Query('type') type: string) {
const access = this.service.getDefineByType(type);
return this.ok(access);
}
@Post('/accessTypeDict', { summary: Constants.per.authOnly })
async getAccessTypeDict() {
const list = this.service.getDefineList();
const dict = [];
for (const item of list) {
if (item?.deprecated) {
continue;
}
dict.push({
value: item.name,
label: item.title,
});
}
return this.ok(dict);
}
}
@@ -0,0 +1,34 @@
import { ALL, Controller, Inject, Post, Provide, Query } from '@midwayjs/core';
import { DnsProviderService } from '../../modules/pipeline/service/dns-provider-service.js';
import { BaseController } from '@certd/lib-server';
import { Constants } from '@certd/lib-server';
/**
* 插件
*/
@Provide()
@Controller('/api/pi/dnsProvider')
export class DnsProviderController extends BaseController {
@Inject()
service: DnsProviderService;
@Post('/list', { summary: Constants.per.authOnly })
async list(@Query(ALL) query: any) {
query.userId = this.getUserId();
const list = this.service.getList();
return this.ok(list);
}
@Post('/dnsProviderTypeDict', { summary: Constants.per.authOnly })
async getDnsProviderTypeDict() {
const list = this.service.getList();
const dict = [];
for (const item of list) {
dict.push({
value: item.name,
label: item.title,
});
}
return this.ok(dict);
}
}
@@ -0,0 +1,98 @@
import { ALL, Body, Controller, Inject, Post, Provide } from '@midwayjs/core';
import { Constants } from '@certd/lib-server';
import {
AccessRequestHandleReq,
http,
ITaskPlugin,
logger,
mergeUtils,
newAccess,
pluginRegistry,
PluginRequestHandleReq,
TaskInstanceContext,
utils,
} from '@certd/pipeline';
import { BaseController } from '@certd/lib-server';
import { AccessService } from '../../modules/pipeline/service/access-service.js';
import { EmailService } from '../../modules/basic/service/email-service.js';
import { AccessGetter } from '../../modules/pipeline/service/access-getter.js';
@Provide()
@Controller('/api/pi/handle')
export class HandleController extends BaseController {
@Inject()
accessService: AccessService;
@Inject()
emailService: EmailService;
@Post('/access', { summary: Constants.per.authOnly })
async accessRequest(@Body(ALL) body: AccessRequestHandleReq) {
let inputAccess = body.input.access;
if (body.input.id > 0) {
const oldEntity = await this.accessService.info(body.input.id);
if (oldEntity) {
const param: any = {
type: body.typeName,
setting: JSON.stringify(body.input.access),
};
this.accessService.encryptSetting(param, oldEntity);
inputAccess = this.accessService.decryptAccessEntity(param);
}
}
const access = newAccess(body.typeName, inputAccess);
const res = await access.onRequest(body);
return this.ok(res);
}
@Post('/plugin', { summary: Constants.per.authOnly })
async pluginRequest(@Body(ALL) body: PluginRequestHandleReq) {
const userId = this.getUserId();
const pluginDefine = pluginRegistry.get(body.typeName);
const pluginCls = pluginDefine.target;
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;
const accessGetter = new AccessGetter(userId, this.accessService.getById.bind(this.accessService));
//@ts-ignore
const taskCtx: TaskInstanceContext = {
pipeline: undefined,
step: undefined,
lastStatus: undefined,
http,
logger: logger,
inputChanged: true,
accessService: accessGetter,
emailService: this.emailService,
pipelineContext: undefined,
userContext: undefined,
fileStore: undefined,
signal: undefined,
// 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,
};
instance.setCtx(taskCtx);
mergeUtils.merge(plugin, body.input);
await instance.onInstance();
const res = await plugin.onRequest(body);
return this.ok(res);
}
}
@@ -0,0 +1,191 @@
import { ALL, Body, Controller, Get, Inject, Post, Provide, Query } from '@midwayjs/core';
import { CommonException, Constants, CrudController, PermissionException } from '@certd/lib-server';
import { PipelineEntity } from '../../modules/pipeline/entity/pipeline.js';
import { HistoryService } from '../../modules/pipeline/service/history-service.js';
import { HistoryLogService } from '../../modules/pipeline/service/history-log-service.js';
import { HistoryEntity } from '../../modules/pipeline/entity/history.js';
import { HistoryLogEntity } from '../../modules/pipeline/entity/history-log.js';
import { PipelineService } from '../../modules/pipeline/service/pipeline-service.js';
import * as fs from 'fs';
import { logger } from '@certd/pipeline';
import { AuthService } from '../../modules/sys/authority/service/auth-service.js';
import { SysSettingsService } from '@certd/lib-server';
import { In } from 'typeorm';
/**
* 证书
*/
@Provide()
@Controller('/api/pi/history')
export class HistoryController extends CrudController<HistoryService> {
@Inject()
service: HistoryService;
@Inject()
pipelineService: PipelineService;
@Inject()
logService: HistoryLogService;
@Inject()
authService: AuthService;
@Inject()
sysSettingsService: SysSettingsService;
getService(): HistoryService {
return this.service;
}
@Post('/page', { summary: Constants.per.authOnly })
async page(@Body(ALL) body: any) {
const isAdmin = await this.authService.isAdmin(this.ctx);
const publicSettings = await this.sysSettingsService.getPublicSettings();
const pipelineQuery: any = {};
if (!(publicSettings.managerOtherUserPipeline && isAdmin)) {
body.query.userId = this.getUserId();
pipelineQuery.userId = this.getUserId();
}
let pipelineIds: any = null;
const pipelineTitle = body.query?.pipelineTitle;
if (pipelineTitle) {
const pipelines = await this.pipelineService.list(pipelineQuery, null, qb => {
qb.where('title like :title', { title: `%${pipelineTitle}%` });
});
pipelineIds = pipelines.map(p => p.id);
}
const buildQuery = qb => {
if (pipelineIds) {
qb.where({
pipelineId: In(pipelineIds),
});
}
};
const res = await this.service.page(body?.query, body?.page, body?.sort, buildQuery);
return this.ok(res);
}
@Post('/list', { summary: Constants.per.authOnly })
async list(@Body(ALL) body) {
const isAdmin = await this.authService.isAdmin(this.ctx);
if (!isAdmin) {
body.userId = this.getUserId();
}
if (body.pipelineId == null) {
return this.ok([]);
}
const buildQuery = qb => {
qb.limit(10);
};
const listRet = await this.getService().list(body, { prop: 'id', asc: false }, buildQuery);
return this.ok(listRet);
}
@Post('/add', { summary: Constants.per.authOnly })
async add(@Body(ALL) bean: PipelineEntity) {
bean.userId = this.getUserId();
return super.add(bean);
}
@Post('/update', { summary: Constants.per.authOnly })
async update(@Body(ALL) bean) {
await this.authService.checkEntityUserId(this.ctx, this.getService(), bean.id);
return super.update(bean);
}
@Post('/save', { summary: Constants.per.authOnly })
async save(@Body(ALL) bean: HistoryEntity) {
bean.userId = this.getUserId();
if (bean.id > 0) {
await this.authService.checkEntityUserId(this.ctx, this.getService(), bean.id);
}
await this.service.save(bean);
return this.ok(bean.id);
}
@Post('/saveLog', { summary: Constants.per.authOnly })
async saveLog(@Body(ALL) bean: HistoryLogEntity) {
bean.userId = this.getUserId();
if (bean.id > 0) {
await this.authService.checkEntityUserId(this.ctx, this.getService(), bean.id);
}
await this.logService.save(bean);
return this.ok(bean.id);
}
@Post('/delete', { summary: Constants.per.authOnly })
async delete(@Query('id') id: number) {
await this.authService.checkEntityUserId(this.ctx, this.getService(), id);
await super.delete(id);
return this.ok();
}
@Post('/deleteByIds', { summary: Constants.per.authOnly })
async deleteByIds(@Body(ALL) body: any) {
await this.authService.checkEntityUserId(this.ctx, this.getService(), body.ids);
const isAdmin = await this.authService.isAdmin(this.ctx);
const userId = isAdmin ? null : this.getUserId();
await this.getService().deleteByIds(body.ids, userId);
return this.ok();
}
@Post('/detail', { summary: Constants.per.authOnly })
async detail(@Query('id') id: number) {
await this.authService.checkEntityUserId(this.ctx, this.getService(), id);
const detail = await this.service.detail(id);
return this.ok(detail);
}
@Post('/logs', { summary: Constants.per.authOnly })
async logs(@Query('id') id: number) {
await this.authService.checkEntityUserId(this.ctx, this.logService, id);
const logInfo = await this.logService.info(id);
return this.ok(logInfo);
}
@Post('/files', { summary: Constants.per.authOnly })
async files(@Query('pipelineId') pipelineId: number, @Query('historyId') historyId: number) {
await this.authService.checkEntityUserId(this.ctx, this.service, historyId);
const files = await this.getFiles(historyId, pipelineId);
return this.ok(files);
}
private async getFiles(historyId, pipelineId) {
let history = null;
if (historyId != null) {
// nothing
history = await this.service.info(historyId);
} else if (pipelineId != null) {
history = await this.service.getLastHistory(pipelineId);
}
if (history == null) {
throw new CommonException('historyId is null');
}
if (history.userId !== this.getUserId()) {
throw new PermissionException();
}
return await this.service.getFiles(history);
}
@Get('/download', { summary: Constants.per.authOnly })
async download(@Query('pipelineId') pipelineId: number, @Query('historyId') historyId: number, @Query('fileId') fileId: string) {
await this.authService.checkEntityUserId(this.ctx, this.service, historyId);
const files = await this.getFiles(historyId, pipelineId);
const file = files.find(f => f.id === fileId);
if (file == null) {
throw new CommonException('file not found');
}
// koa send file
// 下载文件的名称
// const filename = file.filename;
// 要下载的文件的完整路径
const path = file.path;
logger.info(`download:${path}`);
// 以流的形式下载文件
this.ctx.attachment(path);
this.ctx.set('Content-Type', 'application/octet-stream');
return fs.createReadStream(path);
}
}
@@ -0,0 +1,101 @@
import { ALL, Body, Controller, Inject, Post, Provide, Query } from '@midwayjs/core';
import { Constants, CrudController, SysSettingsService } from '@certd/lib-server';
import { PipelineService } from '../../modules/pipeline/service/pipeline-service.js';
import { PipelineEntity } from '../../modules/pipeline/entity/pipeline.js';
import { HistoryService } from '../../modules/pipeline/service/history-service.js';
import { AuthService } from '../../modules/sys/authority/service/auth-service.js';
/**
* 证书
*/
@Provide()
@Controller('/api/pi/pipeline')
export class PipelineController extends CrudController<PipelineService> {
@Inject()
service: PipelineService;
@Inject()
historyService: HistoryService;
@Inject()
authService: AuthService;
@Inject()
sysSettingsService: SysSettingsService;
getService() {
return this.service;
}
@Post('/page', { summary: Constants.per.authOnly })
async page(@Body(ALL) body) {
const isAdmin = await this.authService.isAdmin(this.ctx);
const publicSettings = await this.sysSettingsService.getPublicSettings();
if (!(publicSettings.managerOtherUserPipeline && isAdmin)) {
body.query.userId = this.getUserId();
}
const title = body.query.title;
delete body.query.title;
const buildQuery = qb => {
if (title) {
qb.where('title like :title', { title: `%${title}%` });
}
};
if (!body.sort || !body.sort?.prop) {
body.sort = { prop: 'order', asc: false };
}
const pageRet = await this.getService().page(body?.query, body?.page, body?.sort, buildQuery);
return this.ok(pageRet);
}
@Post('/add', { summary: Constants.per.authOnly })
async add(@Body(ALL) bean: PipelineEntity) {
bean.userId = this.getUserId();
return super.add(bean);
}
@Post('/update', { summary: Constants.per.authOnly })
async update(@Body(ALL) bean) {
await this.authService.checkEntityUserId(this.ctx, this.getService(), bean.id);
return super.update(bean);
}
@Post('/save', { summary: Constants.per.authOnly })
async save(@Body(ALL) bean: PipelineEntity) {
if (bean.id > 0) {
await this.authService.checkEntityUserId(this.ctx, this.getService(), bean.id);
} else {
bean.userId = this.getUserId();
}
await this.service.save(bean);
return this.ok(bean.id);
}
@Post('/delete', { summary: Constants.per.authOnly })
async delete(@Query('id') id: number) {
await this.authService.checkEntityUserId(this.ctx, this.getService(), id);
await this.service.delete(id);
return this.ok({});
}
@Post('/detail', { summary: Constants.per.authOnly })
async detail(@Query('id') id: number) {
await this.authService.checkEntityUserId(this.ctx, this.getService(), id);
const detail = await this.service.detail(id);
return this.ok(detail);
}
@Post('/trigger', { summary: Constants.per.authOnly })
async trigger(@Query('id') id: number, @Query('stepId') stepId?: string) {
await this.authService.checkEntityUserId(this.ctx, this.getService(), id);
await this.service.trigger(id, stepId);
return this.ok({});
}
@Post('/cancel', { summary: Constants.per.authOnly })
async cancel(@Query('historyId') historyId: number) {
await this.authService.checkEntityUserId(this.ctx, this.historyService, historyId);
await this.service.cancel(historyId);
return this.ok({});
}
}
@@ -0,0 +1,28 @@
import { ALL, Controller, Inject, Post, Provide, Query } from '@midwayjs/core';
import { BaseController } from '@certd/lib-server';
import { BuiltInPluginService } from '../../modules/pipeline/service/plugin-service.js';
import { Constants } from '@certd/lib-server';
/**
* 插件
*/
@Provide()
@Controller('/api/pi/plugin')
export class PluginController extends BaseController {
@Inject()
service: BuiltInPluginService;
@Post('/list', { summary: Constants.per.authOnly })
async list(@Query(ALL) query: any) {
query.userId = this.getUserId();
const list = this.service.getList();
return this.ok(list);
}
@Post('/groups', { summary: Constants.per.authOnly })
async groups(@Query(ALL) query: any) {
query.userId = this.getUserId();
const group = this.service.getGroups();
return this.ok(group);
}
}