chore: project controller ok

This commit is contained in:
xiaojunnuo
2026-02-13 21:28:17 +08:00
parent 3f87752d1f
commit 4ee6e38a94
42 changed files with 399 additions and 198 deletions
@@ -65,7 +65,13 @@ export abstract class BaseController {
if (!isEnterprise()) {
return null
}
const projectIdStr = this.ctx.headers["project-id"] as string;
let projectIdStr = this.ctx.headers["project-id"] as string;
if (!projectIdStr){
projectIdStr = this.ctx.request.query["projectId"] as string;
}
if (!projectIdStr){
return null
}
if (!projectIdStr) {
throw new Error("projectId 不能为空")
}
@@ -233,13 +233,14 @@ export abstract class BaseService<T> {
throw new PermissionException('权限不足');
}
async batchDelete(ids: number[], userId: number) {
if(userId >0){
async batchDelete(ids: number[], userId: number,projectId?:number) {
if(userId!=null){
const list = await this.getRepository().find({
where: {
// @ts-ignore
id: In(ids),
userId,
projectId,
},
})
// @ts-ignore
@@ -2,17 +2,19 @@ import { IAccessService } from '@certd/pipeline';
export class AccessGetter implements IAccessService {
userId: number;
getter: <T>(id: any, userId?: number) => Promise<T>;
constructor(userId: number, getter: (id: any, userId: number) => Promise<any>) {
projectId?: number;
getter: <T>(id: any, userId?: number, projectId?: number) => Promise<T>;
constructor(userId: number, projectId: number, getter: (id: any, userId: number, projectId?: number) => Promise<any>) {
this.userId = userId;
this.projectId = projectId;
this.getter = getter;
}
async getById<T = any>(id: any) {
return await this.getter<T>(id, this.userId);
return await this.getter<T>(id, this.userId, this.projectId);
}
async getCommonById<T = any>(id: any) {
return await this.getter<T>(id, 0);
return await this.getter<T>(id, 0,null);
}
}
@@ -129,10 +129,11 @@ export class AccessService extends BaseService<AccessEntity> {
id: entity.id,
name: entity.name,
userId: entity.userId,
projectId: entity.projectId,
};
}
async getAccessById(id: any, checkUserId: boolean, userId?: number): Promise<any> {
async getAccessById(id: any, checkUserId: boolean, userId?: number, projectId?: number): Promise<any> {
const entity = await this.info(id);
if (entity == null) {
throw new Error(`该授权配置不存在,请确认是否已被删除:id=${id}`);
@@ -145,6 +146,9 @@ export class AccessService extends BaseService<AccessEntity> {
throw new PermissionException('您对该Access授权无访问权限');
}
}
if (projectId != null && projectId !== entity.projectId) {
throw new PermissionException('您对该Access授权无访问权限');
}
// const access = accessRegistry.get(entity.type);
const setting = this.decryptAccessEntity(entity);
@@ -152,12 +156,12 @@ export class AccessService extends BaseService<AccessEntity> {
id: entity.id,
...setting,
};
const accessGetter = new AccessGetter(userId, this.getById.bind(this));
const accessGetter = new AccessGetter(userId,projectId, this.getById.bind(this));
return await newAccess(entity.type, input,accessGetter);
}
async getById(id: any, userId: number): Promise<any> {
return await this.getAccessById(id, true, userId);
async getById(id: any, userId: number, projectId?: number): Promise<any> {
return await this.getAccessById(id, true, userId, projectId);
}
decryptAccessEntity(entity: AccessEntity): any {
@@ -188,7 +192,7 @@ export class AccessService extends BaseService<AccessEntity> {
}
async getSimpleByIds(ids: number[], userId: any) {
async getSimpleByIds(ids: number[], userId: any, projectId?: number) {
if (ids.length === 0) {
return [];
}
@@ -199,12 +203,14 @@ export class AccessService extends BaseService<AccessEntity> {
where: {
id: In(ids),
userId,
projectId,
},
select: {
id: true,
name: true,
type: true,
userId:true
userId:true,
projectId:true,
},
});
@@ -70,7 +70,8 @@ export class AddonService extends BaseService<AddonEntity> {
name: entity.name,
userId: entity.userId,
addonType: entity.addonType,
type: entity.type
type: entity.type,
projectId: entity.projectId
};
}
@@ -84,7 +85,7 @@ export class AddonService extends BaseService<AddonEntity> {
}
async getSimpleByIds(ids: number[], userId: any) {
async getSimpleByIds(ids: number[], userId: any,projectId?:number) {
if (ids.length === 0) {
return [];
}
@@ -94,7 +95,8 @@ export class AddonService extends BaseService<AddonEntity> {
return await this.repository.find({
where: {
id: In(ids),
userId
userId,
projectId
},
select: {
id: true,
@@ -109,11 +111,12 @@ export class AddonService extends BaseService<AddonEntity> {
}
async getDefault(userId: number, addonType: string): Promise<any> {
async getDefault(userId: number, addonType: string,projectId?:number): Promise<any> {
const res = await this.repository.findOne({
where: {
userId,
addonType
addonType,
projectId
},
order: {
isDefault: "DESC"
@@ -133,11 +136,12 @@ export class AddonService extends BaseService<AddonEntity> {
type: res.type,
name: res.name,
userId: res.userId,
setting
setting,
projectId: res.projectId
};
}
async setDefault(id: number, userId: number, addonType: string) {
async setDefault(id: number, userId: number, addonType: string,projectId?:number) {
if (!id) {
throw new ValidateException("id不能为空");
}
@@ -147,7 +151,8 @@ export class AddonService extends BaseService<AddonEntity> {
await this.repository.update(
{
userId,
addonType
addonType,
projectId
},
{
isDefault: false
@@ -157,7 +162,8 @@ export class AddonService extends BaseService<AddonEntity> {
{
id,
userId,
addonType
addonType,
projectId
},
{
isDefault: true
@@ -165,12 +171,12 @@ export class AddonService extends BaseService<AddonEntity> {
);
}
async getOrCreateDefault(opts: { addonType: string, type: string, inputs: any, userId: any }) {
const { addonType, type, inputs, userId } = opts;
async getOrCreateDefault(opts: { addonType: string, type: string, inputs: any, userId: any,projectId?:number }) {
const { addonType, type, inputs, userId,projectId } = opts;
const addonDefine = this.getDefineByType(type, addonType);
const defaultConfig = await this.getDefault(userId, addonType);
const defaultConfig = await this.getDefault(userId, addonType,projectId);
if (defaultConfig) {
return defaultConfig;
}
@@ -183,17 +189,19 @@ export class AddonService extends BaseService<AddonEntity> {
type: type,
name: addonDefine.title,
setting: JSON.stringify(setting),
isDefault: true
isDefault: true,
projectId
});
return this.buildAddonInstanceConfig(res);
}
async getOneByType(req:{addonType:string,type:string,userId:number}) {
async getOneByType(req:{addonType:string,type:string,userId:number,projectId?:number}) {
return await this.repository.findOne({
where: {
addonType: req.addonType,
type: req.type,
userId: req.userId
userId: req.userId,
projectId: req.projectId
}
});
}