mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 20:04:54 +00:00
Refactor user role handling to use single role object
This commit is contained in:
@ -7,11 +7,12 @@ export class AdminRoleGuard extends AuthGuard('jwt') {
|
||||
if (err || !user) {
|
||||
throw err || new UnauthorizedException();
|
||||
} else {
|
||||
const isAdmin = user.roles.some(
|
||||
(role) =>
|
||||
role.type === RoleType.SUPER_ADMIN || role.type === RoleType.ADMIN,
|
||||
);
|
||||
if (!isAdmin) {
|
||||
if (
|
||||
!(
|
||||
user.role.type === RoleType.ADMIN ||
|
||||
user.role.type === RoleType.SUPER_ADMIN
|
||||
)
|
||||
) {
|
||||
throw new BadRequestException('Only admin role can access this route');
|
||||
}
|
||||
}
|
||||
|
||||
@ -20,10 +20,10 @@ export class CommunityPermissionGuard implements CanActivate {
|
||||
|
||||
if (
|
||||
user &&
|
||||
user.roles &&
|
||||
user.roles.some(
|
||||
(role) =>
|
||||
role.type === RoleType.ADMIN || role.type === RoleType.SUPER_ADMIN,
|
||||
user.role &&
|
||||
!(
|
||||
user.role.type === RoleType.ADMIN ||
|
||||
user.role.type === RoleType.SUPER_ADMIN
|
||||
)
|
||||
) {
|
||||
return true;
|
||||
|
||||
@ -7,10 +7,7 @@ export class SuperAdminRoleGuard extends AuthGuard('jwt') {
|
||||
if (err || !user) {
|
||||
throw err || new UnauthorizedException();
|
||||
} else {
|
||||
const isSuperAdmin = user.roles.some(
|
||||
(role) => role.type === RoleType.SUPER_ADMIN,
|
||||
);
|
||||
if (!isSuperAdmin) {
|
||||
if (!(user.role.type === RoleType.SUPER_ADMIN)) {
|
||||
throw new BadRequestException(
|
||||
'Only super admin role can access this route',
|
||||
);
|
||||
|
||||
@ -36,19 +36,4 @@ export class RoleController {
|
||||
data: roleTypes,
|
||||
};
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(SuperAdminRoleGuard)
|
||||
@Post()
|
||||
@ApiOperation({
|
||||
summary: ControllerRoute.ROLE.ACTIONS.ADD_USER_ROLE_SUMMARY,
|
||||
description: ControllerRoute.ROLE.ACTIONS.ADD_USER_ROLE_DESCRIPTION,
|
||||
})
|
||||
async addUserRoleType(@Body() addUserRoleDto: AddUserRoleDto) {
|
||||
await this.roleService.addUserRoleType(addUserRoleDto);
|
||||
return {
|
||||
statusCode: HttpStatus.OK,
|
||||
message: 'User Role Added Successfully',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,7 +7,6 @@ import { RoleController } from './controllers/role.controller';
|
||||
import { DeviceUserPermissionRepository } from '@app/common/modules/device/repositories';
|
||||
import { PermissionTypeRepository } from '@app/common/modules/permission/repositories';
|
||||
import { RoleTypeRepository } from '@app/common/modules/role-type/repositories';
|
||||
import { UserRoleRepository } from '@app/common/modules/user/repositories';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule, DeviceRepositoryModule],
|
||||
@ -18,7 +17,6 @@ import { UserRoleRepository } from '@app/common/modules/user/repositories';
|
||||
DeviceRepository,
|
||||
RoleService,
|
||||
RoleTypeRepository,
|
||||
UserRoleRepository,
|
||||
],
|
||||
exports: [RoleService],
|
||||
})
|
||||
|
||||
@ -1,55 +1,16 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { RoleTypeRepository } from './../../../libs/common/src/modules/role-type/repositories/role.type.repository';
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { AddUserRoleDto } from '../dtos/role.add.dto';
|
||||
import { UserRoleRepository } from '@app/common/modules/user/repositories';
|
||||
import { QueryFailedError } from 'typeorm';
|
||||
import { CommonErrorCodes } from '@app/common/constants/error-codes.enum';
|
||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||
|
||||
@Injectable()
|
||||
export class RoleService {
|
||||
constructor(
|
||||
private readonly roleTypeRepository: RoleTypeRepository,
|
||||
private readonly userRoleRepository: UserRoleRepository,
|
||||
) {}
|
||||
|
||||
async addUserRoleType(addUserRoleDto: AddUserRoleDto) {
|
||||
try {
|
||||
const roleType = await this.fetchRoleByType(addUserRoleDto.roleType);
|
||||
|
||||
if (roleType.uuid) {
|
||||
return await this.userRoleRepository.save({
|
||||
user: { uuid: addUserRoleDto.userUuid },
|
||||
roleType: { uuid: roleType.uuid },
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
if (
|
||||
error instanceof QueryFailedError &&
|
||||
error.driverError.code === CommonErrorCodes.DUPLICATE_ENTITY
|
||||
) {
|
||||
// Postgres unique constraint violation error code
|
||||
throw new HttpException(
|
||||
'This role already exists for this user',
|
||||
HttpStatus.CONFLICT,
|
||||
);
|
||||
}
|
||||
throw new HttpException(
|
||||
error.message || 'Internal Server Error',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
constructor(private readonly roleTypeRepository: RoleTypeRepository) {}
|
||||
|
||||
async fetchRoleTypes() {
|
||||
const roleTypes = await this.roleTypeRepository.find();
|
||||
|
||||
return roleTypes;
|
||||
}
|
||||
private async fetchRoleByType(roleType: string) {
|
||||
return await this.roleTypeRepository.findOne({
|
||||
where: {
|
||||
type: roleType,
|
||||
},
|
||||
});
|
||||
const roles = roleTypes.filter(
|
||||
(roleType) => roleType.type !== RoleType.SUPER_ADMIN,
|
||||
);
|
||||
return roles;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user