perf: [comm] 支持插件管理

This commit is contained in:
xiaojunnuo
2024-10-14 00:19:55 +08:00
parent 417971d15d
commit e8b617b80c
24 changed files with 270 additions and 179 deletions
@@ -66,7 +66,7 @@ const development = {
type: 'better-sqlite3',
database: './data/db.sqlite',
synchronize: false, // 如果第一次使用,不存在表,有同步的需求可以写 true
logging: false,
logging: true,
// 配置实体模型 或者 entities: '/entity',
entities: ['**/modules/**/entity/*.js', ...libServerEntities, ...commercialEntities, PipelineEntity, FlywayHistory, UserEntity],
@@ -28,7 +28,12 @@ export class CnameRecordController extends CrudController<CnameRecordService> {
}
};
const pageRet = await this.getService().page(body?.query, body?.page, body?.sort, bq);
const pageRet = await this.getService().page({
query: body.query,
page: body.page,
order: body.order,
buildQuery: bq,
});
return this.ok(pageRet);
}
@@ -12,10 +12,6 @@ export class AccessController extends CrudController<AccessService> {
@Inject()
service: AccessService;
userId() {
return this.getUserId();
}
getService(): AccessService {
return this.service;
}
@@ -23,36 +19,45 @@ export class AccessController extends CrudController<AccessService> {
@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);
delete body.query.userId;
const buildQuery = qb => {
qb.where('user_id = :userId', { userId: this.getUserId() });
};
const res = await this.service.page({
query: body.query,
page: body.page,
order: body.order,
buildQuery,
});
return this.ok(res);
}
@Post('/list', { summary: Constants.per.authOnly })
async list(@Body(ALL) body) {
body.userId = this.userId();
body.userId = this.getUserId();
return super.list(body);
}
@Post('/add', { summary: Constants.per.authOnly })
async add(@Body(ALL) bean) {
bean.userId = this.userId();
bean.userId = this.getUserId();
return super.add(bean);
}
@Post('/update', { summary: Constants.per.authOnly })
async update(@Body(ALL) bean) {
await this.service.checkUserId(bean.id, this.userId());
await this.service.checkUserId(bean.id, this.getUserId());
return super.update(bean);
}
@Post('/info', { summary: Constants.per.authOnly })
async info(@Query('id') id: number) {
await this.service.checkUserId(id, this.userId());
await this.service.checkUserId(id, this.getUserId());
return super.info(id);
}
@Post('/delete', { summary: Constants.per.authOnly })
async delete(@Query('id') id: number) {
await this.service.checkUserId(id, this.userId());
await this.service.checkUserId(id, this.getUserId());
return super.delete(id);
}
@@ -48,8 +48,11 @@ export class HistoryController extends CrudController<HistoryService> {
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}%` });
const pipelines = await this.pipelineService.list({
query: pipelineQuery,
buildQuery: qb => {
qb.where('title like :title', { title: `%${pipelineTitle}%` });
},
});
pipelineIds = pipelines.map(p => p.id);
}
@@ -62,7 +65,12 @@ export class HistoryController extends CrudController<HistoryService> {
}
};
const res = await this.service.page(body?.query, body?.page, body?.sort, buildQuery);
const res = await this.service.page({
query: body.query,
page: body.page,
order: body.order,
buildQuery,
});
return this.ok(res);
}
@@ -78,7 +86,11 @@ export class HistoryController extends CrudController<HistoryService> {
const buildQuery = qb => {
qb.limit(10);
};
const listRet = await this.getService().list(body, { prop: 'id', asc: false }, buildQuery);
const listRet = await this.getService().list({
query: body,
order: { prop: 'id', asc: false },
buildQuery,
});
return this.ok(listRet);
}
@@ -44,7 +44,12 @@ export class PipelineController extends CrudController<PipelineService> {
body.sort = { prop: 'order', asc: false };
}
const pageRet = await this.getService().page(body?.query, body?.page, body?.sort, buildQuery);
const pageRet = await this.getService().page({
query: body.query,
page: body.page,
order: body.order,
buildQuery,
});
return this.ok(pageRet);
}
@@ -1,7 +1,6 @@
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';
import { BaseController, Constants } from '@certd/lib-server';
import { PluginService } from '../../modules/plugin/service/plugin-service.js';
/**
* 插件
@@ -10,19 +9,19 @@ import { Constants } from '@certd/lib-server';
@Controller('/api/pi/plugin')
export class PluginController extends BaseController {
@Inject()
service: BuiltInPluginService;
service: PluginService;
@Post('/list', { summary: Constants.per.authOnly })
async list(@Query(ALL) query: any) {
query.userId = this.getUserId();
const list = this.service.getList();
const list = await this.service.getEnabledBuiltInList();
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();
const group = await this.service.getEnabledBuildInGroup();
return this.ok(group);
}
}
@@ -16,14 +16,14 @@ export class SysAccessController extends AccessController {
return this.service2;
}
userId() {
getUserId() {
checkComm();
return 0;
}
@Post('/page', { summary: 'sys:settings:view' })
async page(@Body(ALL) body: any) {
return await await super.page(body);
return await super.page(body);
}
@Post('/list', { summary: 'sys:settings:view' })
@@ -61,8 +61,8 @@ export class PluginController extends CrudController<PluginService> {
}
@Post('/setDisabled', { summary: 'sys:settings:edit' })
async setDisabled(@Body('id') id: number, @Body('disabled') disabled: boolean) {
await this.service.setDisabled(id, disabled);
async setDisabled(@Body(ALL) body: { id: number; name: string; type: string; disabled: boolean }) {
await this.service.setDisabled(body);
return this.ok();
}
}
@@ -1,7 +1,7 @@
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { BaseService, PermissionException, ValidateException } from '@certd/lib-server';
import { BaseService, PageReq, PermissionException, ValidateException } from '@certd/lib-server';
import { AccessEntity } from '../entity/access.js';
import { AccessDefine, accessRegistry, newAccess } from '@certd/pipeline';
import { EncryptService } from './encrypt-service.js';
@@ -23,8 +23,8 @@ export class AccessService extends BaseService<AccessEntity> {
return this.repository;
}
async page(query, page = { offset: 0, limit: 20 }, order, buildQuery) {
const res = await super.page(query, page, order, buildQuery);
async page(pageReq: PageReq<AccessEntity>) {
const res = await super.page(pageReq);
res.records = res.records.map(item => {
delete item.encryptSetting;
return item;
@@ -107,7 +107,7 @@ export class AccessService extends BaseService<AccessEntity> {
if (entity == null) {
throw new Error(`该授权配置不存在,请确认是否已被删除:id=${id}`);
}
if (userId !== entity.userId) {
if (userId !== entity.userId && entity.userId !== 0) {
throw new PermissionException('您对该Access授权无访问权限');
}
// const access = accessRegistry.get(entity.type);
@@ -1,7 +1,7 @@
import { Config, Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { In, Repository } from 'typeorm';
import { BaseService } from '@certd/lib-server';
import { BaseService, PageReq } from '@certd/lib-server';
import { HistoryEntity } from '../entity/history.js';
import { PipelineEntity } from '../entity/pipeline.js';
import { HistoryDetail } from '../entity/vo/history-detail.js';
@@ -32,8 +32,8 @@ export class HistoryService extends BaseService<HistoryEntity> {
return this.repository;
}
async page(query, page, sort, buildQuery) {
const res = await super.page(query, page, sort, buildQuery);
async page(pageReq: PageReq<HistoryEntity>) {
const res = await super.page(pageReq);
for (const item of res.records) {
item.fillPipelineTitle();
}
@@ -1,7 +1,7 @@
import { Config, Inject, Provide, Scope, ScopeEnum, sleep } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { In, Repository } from 'typeorm';
import { BaseService } from '@certd/lib-server';
import { BaseService, PageReq } from '@certd/lib-server';
import { PipelineEntity } from '../entity/pipeline.js';
import { PipelineDetail } from '../entity/vo/pipeline-detail.js';
import { Executor, isPlus, Pipeline, ResultType, RunHistory, UserInfo } from '@certd/pipeline';
@@ -68,8 +68,8 @@ export class PipelineService extends BaseService<PipelineEntity> {
return bean;
}
async page(query: any, page: { offset: number; limit: number }, order: any, buildQuery: any) {
const result = await super.page(query, page, order, buildQuery);
async page(pageReq: PageReq<PipelineEntity>) {
const result = await super.page(pageReq);
const pipelineIds: number[] = [];
const recordMap = {};
for (const record of result.records) {
@@ -1,10 +1,11 @@
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { BaseService } from '@certd/lib-server';
import { BaseService, PageReq } from '@certd/lib-server';
import { PluginEntity } from '../entity/plugin.js';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { checkComm } from '@certd/pipeline';
import { BuiltInPluginService } from '../../pipeline/service/plugin-service.js';
import { checkComm, isComm } from '@certd/pipeline';
import { BuiltInPluginService } from '../../pipeline/service/builtin-plugin-service.js';
import { merge } from 'lodash-es';
@Provide()
@Scope(ScopeEnum.Singleton)
@@ -21,7 +22,103 @@ export class PluginService extends BaseService<PluginEntity> {
return this.repository;
}
async setDisabled(id: number, disabled: boolean) {
await this.repository.update({ id }, { disabled });
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async page(pageReq: PageReq<PluginEntity>) {
const builtInList = await this.getBuiltInEntityList();
return {
records: builtInList,
total: builtInList.length,
offset: 0,
limit: 99999,
};
}
async getEnabledBuildInGroup() {
const groups = this.builtInPluginService.getGroups();
if (!isComm()) {
return groups;
}
const list = await this.list({
query: {
type: 'builtIn',
disabled: true,
},
});
const disabledNames = list.map(it => it.name);
for (const key in groups) {
const group = groups[key];
if (!group.plugins) {
continue;
}
group.plugins = group.plugins.filter(it => !disabledNames.includes(it.name));
}
return groups;
}
async getEnabledBuiltInList(): Promise<any> {
const builtInList = this.builtInPluginService.getList();
if (!isComm()) {
return builtInList;
}
const list = await this.list({
query: {
type: 'builtIn',
disabled: true,
},
});
const disabledNames = list.map(it => it.name);
return builtInList.filter(it => !disabledNames.includes(it.name));
}
async getBuiltInEntityList() {
const builtInList = this.builtInPluginService.getList();
const list = await this.list({
query: {
type: 'builtIn',
},
});
const records: PluginEntity[] = [];
for (const item of builtInList) {
let record = list.find(it => it.name === item.name);
if (!record) {
record = new PluginEntity();
record.disabled = false;
}
merge(record, {
name: item.name,
title: item.title,
type: 'builtIn',
icon: item.icon,
desc: item.desc,
group: item.group,
});
records.push(record);
}
return records;
}
async setDisabled(opts: { id: number; name: string; type: string; disabled: boolean }) {
const { id, name, type, disabled } = opts;
if (!type) {
throw new Error('参数错误: type 不能为空');
}
if (id > 0) {
//update
await this.repository.update({ id }, { disabled });
return;
}
if (name && type === 'builtIn') {
const pluginEntity = new PluginEntity();
pluginEntity.name = name;
pluginEntity.type = type;
pluginEntity.disabled = disabled;
await this.repository.save(pluginEntity);
return;
}
throw new Error('参数错误: id 和 name 必须有一个');
}
}
@@ -10,6 +10,7 @@ import querystring from 'querystring';
desc: 'dynadot dns provider',
// 这里是对应的 cloudflare的access类型名称
accessType: 'dynadot',
deprecated: '暂不支持',
})
export class DynadotDnsProvider extends AbstractDnsProvider {
// 通过Autowire传递context
@@ -26,6 +26,7 @@ function promisfy(func: any) {
desc: '京东云 dns provider',
// 这里是对应的 cloudflare的access类型名称
accessType: 'jdcloud',
deprecated: '暂不支持',
})
export class JDCloudDnsProvider extends AbstractDnsProvider {
// 通过Autowire传递context