FEAT: Roles and permissions modules

This commit is contained in:
Gabriel Rosa
2022-07-08 17:28:23 -03:00
parent ea04f59e04
commit 4cff593695
18 changed files with 440 additions and 106 deletions
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PermissionsController } from './permissions.controller';
describe('PermissionsController', () => {
let controller: PermissionsController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [PermissionsController],
}).compile();
controller = module.get<PermissionsController>(PermissionsController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});
@@ -0,0 +1,23 @@
import { Controller, Get, Inject } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { DadosferaLogger } from 'dadosfera-logs';
import { User } from 'src/authentication/user.decorator';
import { PermissionsClientService } from './permissions.service';
@ApiTags('Permissions')
@Controller('permissions')
export class PermissionsController {
logger: any;
constructor(
@Inject(DadosferaLogger)
dadosferaLogger: DadosferaLogger,
private permissionsService: PermissionsClientService,
) {
this.logger = dadosferaLogger.logger;
}
@Get()
getPublicPermissions(@User() user) {
this.logger.info('getPublicPermissions', { user });
}
}
@@ -5,6 +5,7 @@ import { DadosferaLogger } from 'dadosfera-logs';
import { PermissionsClientService } from './permissions.service';
import { DucClient } from '../../clients/duc/client.config';
import { PermissionsController } from './permissions.controller';
const ducClient = new DucClient();
@Module({
@@ -17,5 +18,6 @@ const ducClient = new DucClient();
]),
],
providers: [PermissionsClientService, DadosferaLogger],
controllers: [PermissionsController],
})
export class PermissionsModule {}
+58
View File
@@ -0,0 +1,58 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
export class Permission {
@ApiProperty()
id: string;
@ApiProperty()
seqid: number;
@ApiProperty()
name: string;
@ApiProperty()
claim: string;
@ApiProperty()
createdAt: string;
@ApiProperty()
updatedAt: string;
}
export class Role {
@ApiPropertyOptional({ type: [Permission] })
permissions: Permission[];
@ApiProperty()
id: string;
@ApiProperty()
name: string;
@ApiProperty()
createdAt: string;
@ApiProperty()
updatedAt: string;
}
export enum TrueOrFalseEnum {
true = 'true',
false = 'false',
}
export class GetRolesByCustomerReq {
@ApiPropertyOptional()
permission_id: string[];
@ApiPropertyOptional({ enum: TrueOrFalseEnum })
get_users: TrueOrFalseEnum;
}
export class GetRolesByCustomerRes {
@ApiProperty({ type: [Role] })
roles: Role[];
}
export class CreateRoleReq {
@ApiProperty()
name: string;
@ApiProperty()
permissionIds: string[];
@ApiProperty()
userIds: string[];
@ApiProperty()
customerId: string;
}
export class CreateRoleRes {
@ApiProperty({ type: [Role] })
roles: Role[];
}
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { RolesController } from './roles.controller';
describe('RolesController', () => {
let controller: RolesController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [RolesController],
}).compile();
controller = module.get<RolesController>(RolesController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});
+53
View File
@@ -0,0 +1,53 @@
import {
Body,
Controller,
Delete,
Get,
Inject,
Param,
Post,
Query,
} from '@nestjs/common';
import { ApiResponse, ApiTags } from '@nestjs/swagger';
import { User } from 'src/authentication/user.decorator';
import { DadosferaLogger } from 'dadosfera-logs';
import { RolesService } from './roles.service';
import {
CreateRoleReq,
CreateRoleRes,
GetRolesByCustomerReq,
GetRolesByCustomerRes,
} from './dto/entities';
@ApiTags('Roles')
@Controller('roles')
export class RolesController {
logger: any;
constructor(
@Inject(DadosferaLogger)
dadosferaLogger: DadosferaLogger,
private rolesService: RolesService,
) {
this.logger = dadosferaLogger.logger;
}
@Get()
@ApiResponse({ type: GetRolesByCustomerRes })
async getRolesByCustomer(
@User() user,
@Query() filters: GetRolesByCustomerReq,
) {
this.logger.info('getRolesByCustomer', { user });
this.rolesService.getRolesByCustomer(filters);
}
@Post()
@ApiResponse({ type: CreateRoleRes })
async createRole(@User() user, @Body() body: CreateRoleReq) {
this.logger.info('createRole', { user });
}
@Delete(':id')
async deleteRole(@User() user, @Param('id') id: string) {
this.logger.info('deleteRole', { user });
return await this.rolesService.removeRole(id);
}
}
+22
View File
@@ -0,0 +1,22 @@
import { Module } from '@nestjs/common';
import { ClientsModule } from '@nestjs/microservices';
import { DucClient } from 'src/clients/duc/client.config';
import { RolesController } from './roles.controller';
import { RolesService } from './roles.service';
import { DadosferaLogger } from 'dadosfera-logs';
const ducClient = new DucClient();
@Module({
imports: [
ClientsModule.register([
{
name: 'DUC_PACKAGE',
...ducClient.config(),
},
]),
],
controllers: [RolesController],
providers: [RolesService, DadosferaLogger],
})
export class RolesModule {}
+18
View File
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { RolesService } from './roles.service';
describe('RolesService', () => {
let service: RolesService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [RolesService],
}).compile();
service = module.get<RolesService>(RolesService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
+36
View File
@@ -0,0 +1,36 @@
import { Inject, Injectable } from '@nestjs/common';
import { ProtoServices } from 'protospack-v2/dist/lib/Duc';
import { RolesProtoService } from 'protospack-v2/dist/lib/Duc/interfaces/write-service';
import { GetRolesByCustomerReq } from './dto/entities';
import { DadosferaLogger } from 'dadosfera-logs';
import { ClientGrpc } from '@nestjs/microservices';
import { lastValueFrom } from 'rxjs';
@Injectable()
export class RolesService {
logger: any;
private rolesClientService: RolesProtoService;
constructor(
@Inject(DadosferaLogger)
dadosferaLogger: DadosferaLogger,
@Inject('DUC_PACKAGE') private readonly grpcClient: ClientGrpc,
) {
this.logger = dadosferaLogger.logger;
}
onModuleInit() {
this.rolesClientService = this.grpcClient.getService<RolesProtoService>(
ProtoServices.UsersProtoService,
);
}
async getRolesByCustomer(filters: GetRolesByCustomerReq) {
let { permission_id } = filters;
if (!Array.isArray(permission_id)) permission_id = [permission_id];
const { get_users } = filters;
const is_get_users = get_users === 'true';
}
async removeRole(id: string) {
return lastValueFrom(this.rolesClientService.RoleRemove({ id }));
}
}
+30 -61
View File
@@ -1,4 +1,5 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Role } from 'src/modules/roles/dto/entities';
export class Customer {
@ApiProperty()
id: string;
@@ -15,28 +16,26 @@ export class Customer {
@ApiProperty()
updatedAt: string;
}
export class Permission {
@ApiProperty()
id: string;
@ApiProperty()
seqid: number;
@ApiProperty()
name: string;
@ApiProperty()
claim: string;
@ApiProperty()
createdAt: string;
@ApiProperty()
updatedAt: string;
}
export class Role {
@ApiProperty({ type: [Permission] })
permissions: Permission[];
export class User {
@ApiProperty()
id: string;
@ApiProperty()
name: string;
@ApiProperty()
email: string;
@ApiProperty({ type: [Role] })
roles: Role[];
@ApiProperty()
jobTitle: string;
@ApiProperty()
department: string;
@ApiProperty()
hierarchy: number;
@ApiProperty()
customer: Customer;
@ApiProperty()
mfaStatus: string;
@ApiProperty()
createdAt: string;
@ApiProperty()
updatedAt: string;
@@ -57,61 +56,31 @@ export class UserByCustomer {
@ApiProperty()
updatedAt: string;
}
export class User {
@ApiProperty()
id: string;
@ApiProperty()
name: string;
@ApiProperty()
username: string;
@ApiProperty()
UserRole: string[];
@ApiProperty()
customer: Customer;
@ApiProperty()
mfaStatus: string;
@ApiProperty()
createdAt: string;
@ApiProperty()
updatedAt: string;
}
export class CreateUserReq {
@ApiProperty()
@ApiPropertyOptional()
name: string;
@ApiProperty()
email: string;
@ApiProperty()
@ApiPropertyOptional()
roles: string[];
@ApiProperty()
@ApiPropertyOptional()
jobTitle: string;
@ApiProperty()
@ApiPropertyOptional()
department: string;
@ApiProperty()
@ApiPropertyOptional()
hierarchy: number;
}
export class CreateUserRes {
@ApiProperty()
id: string;
@ApiProperty()
name: string;
@ApiProperty()
email: string;
@ApiProperty()
roles: string[];
@ApiProperty()
jobTitle: string;
@ApiProperty()
department: string;
@ApiProperty()
hierarchy: number;
@ApiProperty()
customer: Customer;
@ApiProperty()
mfaStatus: string;
@ApiProperty()
createdAt: string;
@ApiProperty()
updatedAt: string;
user: User;
}
export class BatchCreateUserReq {
@ApiProperty({ type: [CreateUserReq] })
users: CreateUserReq[];
}
export class BatchCreateUserRes {
@ApiProperty({ type: [User] })
users: User[];
}
export class GetAllUsersByCustomerIdReq {
+6 -1
View File
@@ -39,9 +39,14 @@ export class UsersController {
@Post()
@ApiOkResponse({ type: CreateUserRes })
async createUser(@User() user, @Body() body: CreateUserReq) {
console.log(body);
this.logger.info('createUser', { user });
}
@Post('batch')
@ApiOkResponse({ type: CreateUserRes })
async batchCreateUser(@User() user, @Body() body: CreateUserReq) {
this.logger.info('batchCreateUser', { user });
}
@Delete(':id')
@ApiOkResponse({ type: DeleteUserRes })
async deleteUser(@User() user, @Param('id') id: string) {
+1
View File
@@ -4,6 +4,7 @@ import { UsersService } from './users.service';
import { DadosferaLogger } from 'dadosfera-logs';
import { ClientsModule } from '@nestjs/microservices';
import { DucClient } from 'src/clients/duc/client.config';
const ducClient = new DucClient();
@Module({