mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 18:56:22 +00:00
Update role guards to differentiate between admin and super admin roles
This commit is contained in:
@ -16,7 +16,7 @@ import { ResponseMessage } from '../../../libs/common/src/response/response.deco
|
|||||||
import { UserLoginDto } from '../dtos/user-login.dto';
|
import { UserLoginDto } from '../dtos/user-login.dto';
|
||||||
import { ForgetPasswordDto, UserOtpDto, VerifyOtpDto } from '../dtos';
|
import { ForgetPasswordDto, UserOtpDto, VerifyOtpDto } from '../dtos';
|
||||||
import { RefreshTokenGuard } from '@app/common/guards/jwt-refresh.auth.guard';
|
import { RefreshTokenGuard } from '@app/common/guards/jwt-refresh.auth.guard';
|
||||||
import { AdminRoleGuard } from 'src/guards/admin.role.guard';
|
import { SuperAdminRoleGuard } from 'src/guards/super.admin.role.guard';
|
||||||
|
|
||||||
@Controller({
|
@Controller({
|
||||||
version: '1',
|
version: '1',
|
||||||
@ -52,7 +52,7 @@ export class UserAuthController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(AdminRoleGuard)
|
@UseGuards(SuperAdminRoleGuard)
|
||||||
@Delete('user/delete/:id')
|
@Delete('user/delete/:id')
|
||||||
async userDelete(@Param('id') id: string) {
|
async userDelete(@Param('id') id: string) {
|
||||||
await this.userAuthService.deleteUser(id);
|
await this.userAuthService.deleteUser(id);
|
||||||
@ -98,7 +98,7 @@ export class UserAuthController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(AdminRoleGuard)
|
@UseGuards(SuperAdminRoleGuard)
|
||||||
@Get('user/list')
|
@Get('user/list')
|
||||||
async userList() {
|
async userList() {
|
||||||
const userList = await this.userAuthService.userList();
|
const userList = await this.userAuthService.userList();
|
||||||
|
@ -4,13 +4,13 @@ import { AuthGuard } from '@nestjs/passport';
|
|||||||
|
|
||||||
export class AdminRoleGuard extends AuthGuard('jwt') {
|
export class AdminRoleGuard extends AuthGuard('jwt') {
|
||||||
handleRequest(err, user) {
|
handleRequest(err, user) {
|
||||||
const isAdmin = user.roles.some(
|
|
||||||
(role) =>
|
|
||||||
role.type === RoleType.SUPER_ADMIN || role.type === RoleType.ADMIN,
|
|
||||||
);
|
|
||||||
if (err || !user) {
|
if (err || !user) {
|
||||||
throw err || new UnauthorizedException();
|
throw err || new UnauthorizedException();
|
||||||
} else {
|
} else {
|
||||||
|
const isAdmin = user.roles.some(
|
||||||
|
(role) =>
|
||||||
|
role.type === RoleType.SUPER_ADMIN || role.type === RoleType.ADMIN,
|
||||||
|
);
|
||||||
if (!isAdmin) {
|
if (!isAdmin) {
|
||||||
throw new BadRequestException('Only admin role can access this route');
|
throw new BadRequestException('Only admin role can access this route');
|
||||||
}
|
}
|
||||||
|
21
src/guards/super.admin.role.guard.ts
Normal file
21
src/guards/super.admin.role.guard.ts
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||||
|
import { BadRequestException, UnauthorizedException } from '@nestjs/common';
|
||||||
|
import { AuthGuard } from '@nestjs/passport';
|
||||||
|
|
||||||
|
export class SuperAdminRoleGuard extends AuthGuard('jwt') {
|
||||||
|
handleRequest(err, user) {
|
||||||
|
if (err || !user) {
|
||||||
|
throw err || new UnauthorizedException();
|
||||||
|
} else {
|
||||||
|
const isSuperAdmin = user.roles.some(
|
||||||
|
(role) => role.type === RoleType.SUPER_ADMIN,
|
||||||
|
);
|
||||||
|
if (!isSuperAdmin) {
|
||||||
|
throw new BadRequestException(
|
||||||
|
'Only super admin role can access this route',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
}
|
@ -4,15 +4,15 @@ import { AuthGuard } from '@nestjs/passport';
|
|||||||
|
|
||||||
export class UserRoleGuard extends AuthGuard('jwt') {
|
export class UserRoleGuard extends AuthGuard('jwt') {
|
||||||
handleRequest(err, user) {
|
handleRequest(err, user) {
|
||||||
const isUserOrAdmin = user.roles.some(
|
|
||||||
(role) =>
|
|
||||||
role.type === RoleType.SUPER_ADMIN ||
|
|
||||||
role.type === RoleType.ADMIN ||
|
|
||||||
role.type === RoleType.USER,
|
|
||||||
);
|
|
||||||
if (err || !user) {
|
if (err || !user) {
|
||||||
throw err || new UnauthorizedException();
|
throw err || new UnauthorizedException();
|
||||||
} else {
|
} else {
|
||||||
|
const isUserOrAdmin = user.roles.some(
|
||||||
|
(role) =>
|
||||||
|
role.type === RoleType.SUPER_ADMIN ||
|
||||||
|
role.type === RoleType.ADMIN ||
|
||||||
|
role.type === RoleType.USER,
|
||||||
|
);
|
||||||
if (!isUserOrAdmin) {
|
if (!isUserOrAdmin) {
|
||||||
throw new BadRequestException(
|
throw new BadRequestException(
|
||||||
'Only admin or user role can access this route',
|
'Only admin or user role can access this route',
|
||||||
|
@ -11,7 +11,7 @@ import {
|
|||||||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
||||||
import { RoleService } from '../services/role.service';
|
import { RoleService } from '../services/role.service';
|
||||||
import { UserRoleEditDto } from '../dtos';
|
import { UserRoleEditDto } from '../dtos';
|
||||||
import { AdminRoleGuard } from 'src/guards/admin.role.guard';
|
import { SuperAdminRoleGuard } from 'src/guards/super.admin.role.guard';
|
||||||
|
|
||||||
@ApiTags('Role Module')
|
@ApiTags('Role Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -21,7 +21,7 @@ import { AdminRoleGuard } from 'src/guards/admin.role.guard';
|
|||||||
export class RoleController {
|
export class RoleController {
|
||||||
constructor(private readonly roleService: RoleService) {}
|
constructor(private readonly roleService: RoleService) {}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(AdminRoleGuard)
|
@UseGuards(SuperAdminRoleGuard)
|
||||||
@Get('types')
|
@Get('types')
|
||||||
async fetchRoleTypes() {
|
async fetchRoleTypes() {
|
||||||
try {
|
try {
|
||||||
@ -36,7 +36,7 @@ export class RoleController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(AdminRoleGuard)
|
@UseGuards(SuperAdminRoleGuard)
|
||||||
@Put('edit/user/:userUuid')
|
@Put('edit/user/:userUuid')
|
||||||
async editUserRoleType(
|
async editUserRoleType(
|
||||||
@Param('userUuid') userUuid: string,
|
@Param('userUuid') userUuid: string,
|
||||||
|
@ -1,13 +1,16 @@
|
|||||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { IsEnum } from 'class-validator';
|
import { IsEnum, IsIn } from 'class-validator';
|
||||||
|
|
||||||
export class UserRoleEditDto {
|
export class UserRoleEditDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'role type',
|
description: 'Role type (USER or ADMIN)',
|
||||||
enum: RoleType,
|
enum: [RoleType.USER, RoleType.ADMIN],
|
||||||
required: true,
|
required: true,
|
||||||
})
|
})
|
||||||
@IsEnum(RoleType)
|
@IsEnum(RoleType)
|
||||||
|
@IsIn([RoleType.USER, RoleType.ADMIN], {
|
||||||
|
message: 'roleType must be one of the following values: USER, ADMIN',
|
||||||
|
})
|
||||||
roleType: RoleType;
|
roleType: RoleType;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user