Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: CE-1046 - Added read only capabilities to case management #86

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions backend/src/auth/jwtrole.guard.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
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";
import { Role } from "../enum/role.enum";
import { ROLES_KEY } from "./decorators/roles.decorator";
import { GqlExecutionContext } from "@nestjs/graphql";

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",
}
Loading