feat: remove emit cmd

This commit is contained in:
mozzie 2023-10-10 16:48:34 +08:00
parent 90b9a17fd0
commit f942bd1355
19 changed files with 88 additions and 95 deletions

3
.vscode/tasks.json vendored
View File

@ -16,7 +16,8 @@
"dmp-gateway",
"aorta-gateway",
"cert-gateway"
]
],
"problemMatcher": []
},
{
"label": "build:pkg",

View File

@ -23,27 +23,24 @@ export class AuthController {
const { username, password, phoneNumber } = userLoginDto;
// TODO: isEnabled、phoneNumber用于后续验证
const { isLegal, data, msg } = await firstValueFrom(
this.client.send({ cmd: 'cert.user.account' }, { username, password }),
this.client.send('cert.user.account', { username, password }),
);
await firstValueFrom(
this.client.send(
{ cmd: 'logger.user.signIn' },
{
platform: 'dmp',
username,
finger: request.headers['x-finger'] as string,
finger2: request.headers['x-finger2'] as string,
isLegal,
},
),
this.client.send('logger.user.signIn', {
platform: 'dmp',
username,
finger: request.headers['x-finger'] as string,
finger2: request.headers['x-finger2'] as string,
isLegal,
}),
);
if (isLegal) {
// 签发token
const { token } = await firstValueFrom(
this.client.send({ cmd: 'cert.token.create' }, { username }),
this.client.send('cert.token.create', { username }),
);
const { tokenKeyInCookie, expires } = await firstValueFrom(
this.client.send({ cmd: 'cert.token.config' }, []),
this.client.send('cert.token.config', []),
);
res.cookie(tokenKeyInCookie, token, {
maxAge: expires * 1000,

View File

@ -29,14 +29,14 @@ export class AuthGuard implements CanActivate {
// 获取token的配置
const { tokenKeyInCookie, tokenResignIn, expires } = await firstValueFrom(
this.client.send({ cmd: 'cert.token.config' }, []),
this.client.send('cert.token.config', []),
);
const tokenCipher = request.cookies[tokenKeyInCookie];
if (!tokenCipher) throw new ForbiddenException('您没有执行此操作的权限');
// 1.验证token是否合法
const { tokenValid, payload } = await firstValueFrom(
this.client.send({ cmd: 'cert.token.decode' }, tokenCipher),
this.client.send('cert.token.decode', tokenCipher),
);
if (!tokenValid) throw new ForbiddenException('您没有执行此操作的权限');
@ -46,7 +46,7 @@ export class AuthGuard implements CanActivate {
if (tokenExpiredSoon) {
const { username } = payload;
const { token: newToken } = await firstValueFrom(
this.client.send({ cmd: 'cert.token.create' }, { username }),
this.client.send('cert.token.create', { username }),
);
request.res.cookie(tokenKeyInCookie, newToken, {
maxAge: expires * 1000,
@ -65,10 +65,10 @@ export class AuthGuard implements CanActivate {
*/
async rolePermissionAllow(payload: { username: string }, requestUrl: string) {
const { allow } = await firstValueFrom(
this.client.send(
{ cmd: 'cert.role.authorize' },
{ user: payload, url: requestUrl },
),
this.client.send('cert.role.authorize', {
user: payload,
url: requestUrl,
}),
);
return { allow };
}

View File

@ -6,7 +6,7 @@ import { MinioOSS } from '@tavi/oss';
@Controller()
export class AppController {
@EventPattern({ cmd: 'report-upload-image' })
@EventPattern('report-upload-image')
async uploadImageHandler({ buffer, originalName }) {
const objectName = Date.now() + '.' + getFileExt(originalName);
const { bucketName, ...config } = clientConfig;

View File

@ -16,7 +16,7 @@ export class AppController {
* token签发,
* @description redis的EXcookie的maxAge(ms)jwt的expiresIn
*/
@EventPattern({ cmd: 'cert.token.create' })
@EventPattern('cert.token.create')
async createToken(payload) {
const { username } = payload;
const token = await this.jwtService.sign(payload);
@ -28,7 +28,7 @@ export class AppController {
* token有效性 && token
* @description guard对请求token进行拦截token是否合法&redis中
*/
@EventPattern({ cmd: 'cert.token.decode' })
@EventPattern('cert.token.decode')
async decodeToken(
token: string,
): Promise<{ tokenValid: boolean; payload: unknown; error?: unknown }> {
@ -47,7 +47,7 @@ export class AppController {
* token在cookie中存储的key
* @description cookie的maxAge单位是毫秒cookie使用expires需要 x1000
*/
@EventPattern({ cmd: 'cert.token.config' })
@EventPattern('cert.token.config')
async tokenConfig() {
return {
tokenResignIn: this.configService.get('TOKEN_RESIGN_IN'),
@ -56,7 +56,7 @@ export class AppController {
};
}
@EventPattern({ cmd: 'cert.token.deprecated' })
@EventPattern('cert.token.deprecated')
async removeToken(payload) {
const { username } = payload;
console.log('username', username);

View File

@ -1,6 +1,6 @@
import { Controller } from '@nestjs/common';
import { AppService } from './app.service';
import { EventPattern, MessagePattern } from '@nestjs/microservices';
import { EventPattern } from '@nestjs/microservices';
import { RbacService } from './rbac/rbac.service';
@Controller()
@ -10,59 +10,59 @@ export class AppController {
private readonly rbacService: RbacService,
) {}
@EventPattern({ cmd: 'cert.authorize' })
@EventPattern('cert.authorize')
async authorize(payload) {
console.log('获取user的token进行payload权限的验证', payload);
return false;
}
@EventPattern({ cmd: 'cert.init.role.admin' })
@EventPattern('cert.init.role.admin')
async initRole() {
await this.rbacService.initSuperAdminRole();
await this.rbacService.initSuperAdminAccount();
}
@EventPattern({ cmd: 'cert.create.role' })
@EventPattern('cert.create.role')
async createRole(payload) {
return await this.rbacService.createRole(payload);
}
@EventPattern({ cmd: 'cert.remove.role' })
@EventPattern('cert.remove.role')
async removeRole(payload) {
return await this.rbacService.removeRole(payload);
}
@EventPattern({ cmd: 'cert.find.role.all' })
@EventPattern('cert.find.role.all')
async findAllRole() {
return await this.rbacService.findAllRole();
}
@EventPattern({ cmd: 'cert.init.permission.resource' })
@EventPattern('cert.init.permission.resource')
async initPermission(payload) {
await this.rbacService.initPermission(payload);
}
@EventPattern({ cmd: 'cert.find.role.permission' })
@EventPattern('cert.find.role.permission')
async findRolePermission(payload) {
return await this.rbacService.findRolePermission(payload);
}
@EventPattern({ cmd: 'cert.update.role.permissions' })
@EventPattern('cert.update.role.permissions')
async updateRolePermissions(payload) {
return await this.rbacService.updateRolePermissions(payload);
}
@EventPattern({ cmd: 'cert.update.role' })
@EventPattern('cert.update.role')
async updateRole(payload) {
return await this.rbacService.updateRole(payload);
}
@EventPattern({ cmd: 'cert.find.all.user' })
@EventPattern('cert.find.all.user')
async findAllUser() {
return await this.rbacService.findAllUser();
}
@EventPattern({ cmd: 'cert.role.authorize' })
@EventPattern('cert.role.authorize')
async roleAuthorize({ user, url }) {
const allow = await this.rbacService.roleAuthorize(user, url);
return { allow };

View File

@ -12,7 +12,7 @@ export class UserController {
/**
*
*/
@EventPattern({ cmd: 'cert.user.account' })
@EventPattern('cert.user.account')
async findUser(
payload,
): Promise<{ isLegal: boolean; msg?: string; data?: any }> {
@ -28,22 +28,22 @@ export class UserController {
return { isLegal: true, data: user };
}
@EventPattern({ cmd: 'cert.user.encrypt' })
@EventPattern('cert.user.encrypt')
async encrypt(plainText: string) {
return await this.bcryptService.hashPassword(plainText);
}
@EventPattern({ cmd: 'cert.encrypt.compare' })
@EventPattern('cert.encrypt.compare')
async compare(plainText: string, hash: string) {
return await this.bcryptService.comparePassword(plainText, hash);
}
@EventPattern({ cmd: 'cert.user.create' })
@EventPattern('cert.user.create')
async createUser(payload) {
return await this.userService.create(payload);
}
@EventPattern({ cmd: 'cert.user.update' })
@EventPattern('cert.user.update')
async updateUser(payload) {
const { password, ...rest } = payload;
const updateFields = password
@ -52,12 +52,12 @@ export class UserController {
return await this.userService.update(updateFields);
}
@EventPattern({ cmd: 'cert.user.delete' })
@EventPattern('cert.user.delete')
async deleteUser(userIds: number[]) {
return await this.userService.delete(userIds);
}
@EventPattern({ cmd: 'cert.user.find.annotator' })
@EventPattern('cert.user.find.annotator')
async findAnnotators() {
const annotators = await this.userService.findAnnotators();
return { data: annotators };

View File

@ -25,7 +25,7 @@ export class AdminController {
@Post('role/create')
async createRole(@Body() createRoleDto: CreateRoleDto) {
const { success, result, message } = await firstValueFrom(
this.client.send({ cmd: 'cert.create.role' }, createRoleDto),
this.client.send('cert.create.role', createRoleDto),
);
return success
? { code: 0, data: result, msg: message }
@ -34,14 +34,14 @@ export class AdminController {
@Post('role/remove')
async removeRole(@Body() role: RoleDto) {
const pattern = { cmd: 'cert.remove.role' };
const pattern = 'cert.remove.role';
const result = await firstValueFrom(this.client.send(pattern, role));
return { code: 'ok', data: result };
}
@Post('role/update')
async updateRole(@Body() forbidRoleDto: ForbidRoleDto) {
const pattern = { cmd: 'cert.update.role' };
const pattern = 'cert.update.role';
const payload = forbidRoleDto;
const result = await firstValueFrom(this.client.send(pattern, payload));
return { code: 'ok', data: result };
@ -49,14 +49,14 @@ export class AdminController {
@Get('find/role/all')
async findAllRolePermissions() {
const pattern = { cmd: 'cert.find.role.all' };
const pattern = 'cert.find.role.all';
const result = await firstValueFrom(this.client.send(pattern, []));
return { code: 'ok', data: result };
}
@Post('role/permissions/find')
async findRolePermission(@Body() findRoleDto: FindRoleDto): Promise<any> {
const pattern = { cmd: 'cert.find.role.permission' };
const pattern = 'cert.find.role.permission';
const payload = findRoleDto.id;
// 角色在role-permission关联表中已存在的权限
const { permissions } = await firstValueFrom(
@ -79,7 +79,7 @@ export class AdminController {
async updateRolePermissions(
@Body() updateRolePermissionsDto: UpdateRolePermissionsDto,
) {
const pattern = { cmd: 'cert.update.role.permissions' };
const pattern = 'cert.update.role.permissions';
const { roleId, permissionIds } = updateRolePermissionsDto;
const payload = { roleId, permissionIds };
const result = await firstValueFrom(this.client.send(pattern, payload));
@ -89,7 +89,7 @@ export class AdminController {
@Post('user/create')
async createUser(@Body() createUserDto: CreateUserDto) {
const { success, msg, data } = await firstValueFrom(
this.client.send({ cmd: 'cert.user.create' }, createUserDto),
this.client.send('cert.user.create', createUserDto),
);
return success ? { code: 0, data, msg } : { code: 1, msg };
}
@ -97,26 +97,24 @@ export class AdminController {
@Get('user/find/all')
async userSelect() {
const result = await firstValueFrom(
this.client.send({ cmd: 'cert.find.all.user' }, []),
this.client.send('cert.find.all.user', []),
);
return { code: 'ok', data: result };
}
@Post('user/update')
async userUpdate(@Body() user: UpdateUserDto) {
const pattern = { cmd: 'cert.user.update' };
const pattern = 'cert.user.update';
const payload = user;
const result = await firstValueFrom(this.client.send(pattern, payload));
// 删除token
await firstValueFrom(
this.client.send({ cmd: 'cert.token.deprecated' }, payload),
);
await firstValueFrom(this.client.send('cert.token.deprecated', payload));
return { code: 'ok', data: result };
}
@Post('user/delete')
async userDelete(@Body() userIds: number[]) {
const pattern = { cmd: 'cert.user.delete' };
const pattern = 'cert.user.delete';
const payload = userIds;
const result = await firstValueFrom(this.client.send(pattern, payload));
return { code: 'ok', data: result };

View File

@ -17,12 +17,12 @@ export class AppController {
@Get('/auth/user')
async auth(@Req() request: Request) {
const { tokenKeyInCookie } = await firstValueFrom(
this.client.send({ cmd: 'cert.token.config' }, []),
this.client.send('cert.token.config', []),
);
const tokenCipher = request.cookies[tokenKeyInCookie];
if (!tokenCipher) return { code: 1, msg: '登录状态失效,请重新登录' };
const result = await firstValueFrom(
this.client.send({ cmd: 'cert.token.decode' }, tokenCipher),
this.client.send('cert.token.decode', tokenCipher),
);
return { code: 0, data: result };
}

View File

@ -12,7 +12,7 @@ export class AppService {
*/
async permissionResourceInitialization() {
console.log('初始化资源权限');
const pattern = { cmd: 'cert.init.permission.resource' };
const pattern = 'cert.init.permission.resource';
const payload = rbacConfiguration.map((i) => i.permissions).flat();
const result = await firstValueFrom(this.client.emit(pattern, payload));
return { code: 'ok', data: result };
@ -22,7 +22,7 @@ export class AppService {
* &
*/
async roleInitialization() {
const pattern = { cmd: 'cert.init.role.admin' };
const pattern = 'cert.init.role.admin';
const payload = [];
const result = await firstValueFrom(this.client.emit(pattern, payload));
return { code: 'ok', data: result };

View File

@ -118,7 +118,7 @@ export class AppController {
}
}
@EventPattern({ cmd: 'dicom.archive.url' })
@EventPattern('dicom.archive.url')
async archiveUrl({ ID, Type }: { ID: string; Type: string }) {
const pacsUrl = this.configService.get('PACS_URL');
const mapping = {

View File

@ -6,12 +6,12 @@ import { EventPattern } from '@nestjs/microservices';
export class AppController {
constructor(private readonly appService: AppService) {}
@EventPattern({ cmd: 'archive.task.create' })
@EventPattern('archive.task.create')
async createArchiveTask(payload) {
return await this.appService.createArchiveTask(payload);
}
@EventPattern({ cmd: 'archive.task.find' })
@EventPattern('archive.task.find')
async findArchiveTask(payload) {
return await this.appService.findArchiveTask(payload);
}

View File

@ -9,7 +9,7 @@ export class AdminController {
@Get('find/annotator')
async findAnnotator() {
const { data } = await firstValueFrom(
this.client.send({ cmd: 'cert.user.find.annotator' }, {}),
this.client.send('cert.user.find.annotator', {}),
);
return { data, code: 0 };
}
@ -19,7 +19,7 @@ export class AdminController {
const { user, study } = body;
const { username } = user;
const { success, data, error } = await firstValueFrom(
this.client.send({ cmd: 'archive.task.create' }, { username, study }),
this.client.send('archive.task.create', { username, study }),
);
return success ? { code: 0, data } : { code: 1, msg: error.code };
}

View File

@ -10,15 +10,15 @@ export class AnnotatorController {
@Get('find/archiveTask')
async findArchiveTask(@Req() request: Request) {
const { tokenKeyInCookie } = await firstValueFrom(
this.client.send({ cmd: 'cert.token.config' }, []),
this.client.send('cert.token.config', []),
);
const tokenCipher = request.cookies[tokenKeyInCookie];
const { payload } = await firstValueFrom(
this.client.send({ cmd: 'cert.token.decode' }, tokenCipher),
this.client.send('cert.token.decode', tokenCipher),
);
const { username } = payload;
const data = await firstValueFrom(
this.client.send({ cmd: 'archive.task.find' }, { username }),
this.client.send('archive.task.find', { username }),
);
return { code: 0, data };
}

View File

@ -16,29 +16,26 @@ export class AuthController {
) {
const { username, password } = userLoginDto;
const { isLegal, data, msg } = await firstValueFrom(
this.client.send({ cmd: 'cert.user.account' }, { username, password }),
this.client.send('cert.user.account', { username, password }),
);
// 日志
await firstValueFrom(
this.client.send(
{ cmd: 'logger.user.signIn' },
{
platform: 'dmp',
username,
finger: request.headers['x-finger'] as string,
finger2: request.headers['x-finger2'] as string,
isLegal,
},
),
this.client.send('logger.user.signIn', {
platform: 'dmp',
username,
finger: request.headers['x-finger'] as string,
finger2: request.headers['x-finger2'] as string,
isLegal,
}),
);
if (isLegal) {
// 签发token,签用户的角色,前端UI路由鉴权
const { roles } = data;
const { token } = await firstValueFrom(
this.client.send({ cmd: 'cert.token.create' }, { username, roles }),
this.client.send('cert.token.create', { username, roles }),
);
const { tokenKeyInCookie, expires } = await firstValueFrom(
this.client.send({ cmd: 'cert.token.config' }, []),
this.client.send('cert.token.config', []),
);
res.cookie(tokenKeyInCookie, token, {
maxAge: expires * 1000,

View File

@ -12,7 +12,7 @@ export class DicomController {
async downloadDicom(@Body() body, @Res() res: Response) {
const { ID, Type } = body;
const url = await firstValueFrom(
this.client.send({ cmd: 'dicom.archive.url' }, { ID, Type }),
this.client.send('dicom.archive.url', { ID, Type }),
);
try {
const { data: dataStream } = await axios.get(url, {

View File

@ -29,14 +29,14 @@ export class AuthGuard implements CanActivate {
// 获取token的配置
const { tokenKeyInCookie, tokenResignIn, expires } = await firstValueFrom(
this.client.send({ cmd: 'cert.token.config' }, []),
this.client.send('cert.token.config', []),
);
const tokenCipher = request.cookies[tokenKeyInCookie];
if (!tokenCipher) throw new ForbiddenException('您没有执行此操作的权限');
// 1.验证token是否合法
const { tokenValid, payload } = await firstValueFrom(
this.client.send({ cmd: 'cert.token.decode' }, tokenCipher),
this.client.send('cert.token.decode', tokenCipher),
);
if (!tokenValid) throw new ForbiddenException('您没有执行此操作的权限');
@ -46,7 +46,7 @@ export class AuthGuard implements CanActivate {
if (tokenExpiredSoon) {
const { username } = payload;
const { token: newToken } = await firstValueFrom(
this.client.send({ cmd: 'cert.token.create' }, { username }),
this.client.send('cert.token.create', { username }),
);
request.res.cookie(tokenKeyInCookie, newToken, {
maxAge: expires * 1000,
@ -65,10 +65,10 @@ export class AuthGuard implements CanActivate {
*/
async rolePermissionAllow(payload: { username: string }, requestUrl: string) {
const { allow } = await firstValueFrom(
this.client.send(
{ cmd: 'cert.role.authorize' },
{ user: payload, url: requestUrl },
),
this.client.send('cert.role.authorize', {
user: payload,
url: requestUrl,
}),
);
return { allow };
}

View File

@ -16,7 +16,7 @@ interface UserSignLoggerDto {
export class AppController {
constructor(private readonly appService: AppService) {}
@EventPattern({ cmd: 'logger.user.signIn' })
@EventPattern('logger.user.signIn')
async userSignIn(payload: UserSignLoggerDto) {
const dateTime = dayjs().format('YYYY-MM-DD HH:mm:ss');
const { finger2, ...rest } = payload;
@ -25,7 +25,7 @@ export class AppController {
return 1;
}
@EventPattern({ cmd: 'logger.save' })
@EventPattern('logger.save')
async saveLogger(payload: UserSignLoggerDto) {
return 'ok';
}

View File

@ -6,7 +6,7 @@
// 网关
@Get('/hello')
async getHello(): Promise<any> {
const pattern = { cmd: 'sum' };
const pattern = 'sum';
const payload = [1, 2];
// this.client.emit(pattern,payload)没有返回值
const it = await this.client.send<number[]>(pattern, payload);
@ -14,7 +14,7 @@ async getHello(): Promise<any> {
}
// 服务
@EventPattern({ cmd: 'sum' })
@EventPattern('sum')
sum(data: number[]): number {
return (data || []).reduce((a, b) => a + b);
}