Skip to content

Commit

Permalink
Added read only capabilities to case management
Browse files Browse the repository at this point in the history
Users with the readonly role will not be able to consume non-GET requests
  • Loading branch information
barrfalk committed Sep 19, 2024
1 parent 2d2c8c5 commit afd4424
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
24 changes: 20 additions & 4 deletions backend/src/auth/jwtrole.guard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { ExecutionContext, Injectable, CanActivate, UnauthorizedException, Logger } from "@nestjs/common";
import {
ExecutionContext,
Injectable,
CanActivate,
UnauthorizedException,
Logger,
ForbiddenException,
} from "@nestjs/common";
import { Reflector } from "@nestjs/core";
import { AuthGuard } from "@nestjs/passport";
import { Role } from "src/enum/role.enum";
Expand Down Expand Up @@ -38,6 +45,18 @@ export class JwtRoleGuard extends AuthGuard("jwt") implements CanActivate {
throw new UnauthorizedException("Cannot verify user authorization");
}

const userRoles: string[] = user.client_roles;
// Check if the user has the readonly role
const hasReadOnlyRole = userRoles.includes(Role.READ_ONLY);

// If the user has readonly role, allow only GET requests
if (hasReadOnlyRole) {
if (request.method !== "GET") {
this.logger.debug(`User with readonly role attempted ${request.method} method`);
throw new ForbiddenException("Access denied: Read-only users cannot perform this action");
}
}

// if there aren't any required roles, don't allow the user to access any api. Unless the API is marked as public, at least one role is required.
if (!requiredRoles) {
this.logger.error(
Expand All @@ -46,9 +65,6 @@ export class JwtRoleGuard extends AuthGuard("jwt") implements CanActivate {
return false;
}

// roles that the user has
const userRoles: string[] = user.client_roles;

this.logger.debug(`User Roles: ${userRoles}`);

// does the user have a required role?
Expand Down
1 change: 1 addition & 0 deletions backend/src/enum/role.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export enum Role {
COS_OFFICER = "COS Officer",
COS_ADMIN = "COS Admin",
CEEB = "CEEB",
READ_ONLY = "READ ONLY",
}

0 comments on commit afd4424

Please sign in to comment.