prevent admin role from managing owner role (backend)

This commit is contained in:
Philipinho
2024-07-22 16:16:33 +01:00
parent 109dbdbe02
commit b4bc184cb3

View File

@ -1,5 +1,6 @@
import { import {
BadRequestException, BadRequestException,
ForbiddenException,
Injectable, Injectable,
NotFoundException, NotFoundException,
} from '@nestjs/common'; } from '@nestjs/common';
@ -217,11 +218,21 @@ export class WorkspaceService {
) { ) {
const user = await this.userRepo.findById(userRoleDto.userId, workspaceId); const user = await this.userRepo.findById(userRoleDto.userId, workspaceId);
const newRole = userRoleDto.role.toLowerCase();
if (!user) { if (!user) {
throw new BadRequestException('Workspace member not found'); throw new BadRequestException('Workspace member not found');
} }
if (user.role === userRoleDto.role) { // prevent ADMIN from managing OWNER role
if (
(authUser.role === UserRole.ADMIN && newRole === UserRole.OWNER) ||
(authUser.role === UserRole.ADMIN && user.role === UserRole.OWNER)
) {
throw new ForbiddenException();
}
if (user.role === newRole) {
return user; return user;
} }
@ -238,7 +249,7 @@ export class WorkspaceService {
await this.userRepo.updateUser( await this.userRepo.updateUser(
{ {
role: userRoleDto.role, role: newRole,
}, },
user.id, user.id,
workspaceId, workspaceId,