2024-08-27 13:46:19 +08:00
|
|
|
import { ALL, Body, Controller, Inject, Post, Provide, Query } from '@midwayjs/core';
|
2024-11-01 00:59:09 +08:00
|
|
|
import { Constants, CrudController } from '@certd/lib-server';
|
2024-12-22 14:00:46 +08:00
|
|
|
import { AccessService } from '@certd/lib-server';
|
2025-01-15 01:05:34 +08:00
|
|
|
import { AuthService } from '../../../modules/sys/authority/service/auth-service.js';
|
2024-11-30 01:57:09 +08:00
|
|
|
import { AccessDefine } from '@certd/pipeline';
|
2023-01-29 13:44:19 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 授权
|
|
|
|
|
*/
|
|
|
|
|
@Provide()
|
|
|
|
|
@Controller('/api/pi/access')
|
2023-05-23 18:01:20 +08:00
|
|
|
export class AccessController extends CrudController<AccessService> {
|
2023-01-29 13:44:19 +08:00
|
|
|
@Inject()
|
|
|
|
|
service: AccessService;
|
2024-11-20 18:12:10 +08:00
|
|
|
@Inject()
|
|
|
|
|
authService: AuthService;
|
2023-01-29 13:44:19 +08:00
|
|
|
|
2024-10-03 22:03:49 +08:00
|
|
|
getService(): AccessService {
|
2023-01-29 13:44:19 +08:00
|
|
|
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) {
|
2026-02-13 21:28:17 +08:00
|
|
|
const { projectId, userId } = await this.getProjectUserIdRead()
|
2023-01-29 13:44:19 +08:00
|
|
|
body.query = body.query ?? {};
|
2024-10-14 00:19:55 +08:00
|
|
|
delete body.query.userId;
|
2026-02-13 21:28:17 +08:00
|
|
|
body.query.userId = userId;
|
|
|
|
|
body.query.projectId = projectId;
|
2025-07-28 23:36:10 +08:00
|
|
|
let name = body.query?.name;
|
|
|
|
|
delete body.query.name;
|
2024-10-14 00:19:55 +08:00
|
|
|
const buildQuery = qb => {
|
2025-07-28 23:36:10 +08:00
|
|
|
if (name) {
|
|
|
|
|
qb.andWhere('name like :name', { name: `%${name.trim()}%` });
|
|
|
|
|
}
|
2024-10-14 00:19:55 +08:00
|
|
|
};
|
|
|
|
|
const res = await this.service.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,
|
|
|
|
|
});
|
|
|
|
|
return this.ok(res);
|
2023-01-29 13:44:19 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-27 09:29:43 +08:00
|
|
|
@Post('/list', { summary: Constants.per.authOnly })
|
2023-01-29 13:44:19 +08:00
|
|
|
async list(@Body(ALL) body) {
|
2026-02-13 21:28:17 +08:00
|
|
|
const { projectId, userId } = await this.getProjectUserIdRead()
|
2024-12-09 02:24:30 +08:00
|
|
|
body.query = body.query ?? {};
|
2026-02-13 21:28:17 +08:00
|
|
|
body.query.userId = userId;
|
|
|
|
|
body.query.projectId = projectId;
|
2023-01-29 13:44:19 +08:00
|
|
|
return super.list(body);
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
2026-02-13 21:28:17 +08:00
|
|
|
const { projectId, userId } = await this.getProjectUserIdWrite()
|
|
|
|
|
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-13 21:28:17 +08:00
|
|
|
await this.checkOwner(this.getService(), bean.id, "write");
|
2024-12-01 02:10:40 +08:00
|
|
|
delete bean.userId;
|
2026-02-13 21:28:17 +08:00
|
|
|
delete bean.projectId;
|
2023-01-29 13:44:19 +08:00
|
|
|
return super.update(bean);
|
|
|
|
|
}
|
2023-06-27 09:29:43 +08:00
|
|
|
@Post('/info', { summary: Constants.per.authOnly })
|
2024-08-30 18:50:53 +08:00
|
|
|
async info(@Query('id') id: number) {
|
2026-02-13 21:28:17 +08:00
|
|
|
await this.checkOwner(this.getService(), id, "read");
|
2023-01-29 13:44:19 +08:00
|
|
|
return super.info(id);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 09:29:43 +08:00
|
|
|
@Post('/delete', { summary: Constants.per.authOnly })
|
2024-08-30 18:50:53 +08:00
|
|
|
async delete(@Query('id') id: number) {
|
2026-02-13 21:28:17 +08:00
|
|
|
await this.checkOwner(this.getService(), id, "write");
|
2023-01-29 13:44:19 +08:00
|
|
|
return super.delete(id);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 09:29:43 +08:00
|
|
|
@Post('/define', { summary: Constants.per.authOnly })
|
2024-09-29 14:57:20 +08:00
|
|
|
async define(@Query('type') type: string) {
|
2024-03-22 00:50:02 +08:00
|
|
|
const access = this.service.getDefineByType(type);
|
|
|
|
|
return this.ok(access);
|
2023-01-29 13:44:19 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-28 18:20:10 +08:00
|
|
|
@Post('/getSecretPlain', { summary: Constants.per.authOnly })
|
|
|
|
|
async getSecretPlain(@Body(ALL) body: { id: number; key: string }) {
|
2026-02-13 21:28:17 +08:00
|
|
|
const {userId, projectId} = await this.checkOwner(this.getService(), body.id, "read");
|
|
|
|
|
const value = await this.service.getById(body.id, userId, projectId);
|
2024-10-28 18:20:10 +08:00
|
|
|
return this.ok(value[body.key]);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 09:29:43 +08:00
|
|
|
@Post('/accessTypeDict', { summary: Constants.per.authOnly })
|
2023-01-29 13:44:19 +08:00
|
|
|
async getAccessTypeDict() {
|
2025-04-28 23:34:08 +08:00
|
|
|
let list: AccessDefine[] = this.service.getDefineList();
|
|
|
|
|
list = list.sort((a,b) => {
|
|
|
|
|
return (a.order??10) - (b.order??10);
|
|
|
|
|
});
|
2023-01-29 13:44:19 +08:00
|
|
|
const dict = [];
|
|
|
|
|
for (const item of list) {
|
|
|
|
|
dict.push({
|
|
|
|
|
value: item.name,
|
|
|
|
|
label: item.title,
|
2024-11-30 01:57:09 +08:00
|
|
|
icon: item.icon,
|
2023-01-29 13:44:19 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return this.ok(dict);
|
|
|
|
|
}
|
2024-11-20 18:12:10 +08:00
|
|
|
|
|
|
|
|
@Post('/simpleInfo', { summary: Constants.per.authOnly })
|
|
|
|
|
async simpleInfo(@Query('id') id: number) {
|
2026-02-13 21:28:17 +08:00
|
|
|
// await this.authService.checkUserIdButAllowAdmin(this.ctx, this.service, id);
|
2026-03-03 23:31:42 +08:00
|
|
|
// await this.checkOwner(this.getService(), id, "read",true);
|
2024-11-20 18:12:10 +08:00
|
|
|
const res = await this.service.getSimpleInfo(id);
|
|
|
|
|
return this.ok(res);
|
|
|
|
|
}
|
2025-07-12 23:00:04 +08:00
|
|
|
|
|
|
|
|
@Post('/getDictByIds', { summary: Constants.per.authOnly })
|
|
|
|
|
async getDictByIds(@Body('ids') ids: number[]) {
|
2026-02-13 21:28:17 +08:00
|
|
|
const { userId, projectId } = await this.getProjectUserIdRead()
|
|
|
|
|
const res = await this.service.getSimpleByIds(ids, userId, projectId);
|
2025-07-12 23:00:04 +08:00
|
|
|
return this.ok(res);
|
|
|
|
|
}
|
2023-01-29 13:44:19 +08:00
|
|
|
}
|