Skip to content

Commit

Permalink
fix: improve code + add requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Emnaghz committed Sep 21, 2024
1 parent 42cfc1f commit 16f10c5
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions api/src/user/controllers/role.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import {
Patch,
Query,
UseInterceptors,
Session,
ForbiddenException,
Session,
} from '@nestjs/common';
import { CsrfCheck } from '@tekuconcept/nestjs-csrf';
import { Session as ExpressSession } from 'express-session';
Expand Down Expand Up @@ -148,11 +148,16 @@ export class RoleController extends BaseController<Role, RoleStub> {
@Delete(':id')
@HttpCode(204)
async deleteOne(@Param('id') id: string, @Session() session: ExpressSession) {
const roles = (
await this.userService.findOneAndPopulate(session.passport?.user?.id, [
'roles',
])
).roles.map((role) => role.id);
const currentUser = await this.userService.findOneAndPopulate(
session.passport.user.id,
['roles'],
);
if (!currentUser) {
throw new NotFoundException('User not found');
}

const roles = currentUser.roles.map((role) => role.id);

if (roles.includes(id)) {
throw new ForbiddenException("Your account's role can't be deleted");
} else {
Expand Down

0 comments on commit 16f10c5

Please sign in to comment.