|
| 1 | +import { |
| 2 | + Injectable, |
| 3 | + NestInterceptor, |
| 4 | + ExecutionContext, |
| 5 | + CallHandler, |
| 6 | + ForbiddenException, |
| 7 | +} from '@nestjs/common'; |
| 8 | +import { GqlExecutionContext, TypeMetadataStorage } from '@nestjs/graphql'; |
| 9 | +import { Observable, map } from 'rxjs'; |
| 10 | + |
| 11 | +import { Role, User } from '@prisma/client'; |
| 12 | +import { |
| 13 | + FieldNode, |
| 14 | + getNamedType, |
| 15 | + GraphQLResolveInfo, |
| 16 | + GraphQLType, |
| 17 | + Kind, |
| 18 | +} from 'graphql'; |
| 19 | +import { GraphQLContext } from '../config/graphql.context'; |
| 20 | +import { FIELD_ROLE } from './field-access.decorator'; |
| 21 | + |
| 22 | +@Injectable() |
| 23 | +export class FieldAccessInterceptor<T extends Record<string, unknown>> |
| 24 | + implements NestInterceptor |
| 25 | +{ |
| 26 | + intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> { |
| 27 | + const gqlContext = GqlExecutionContext.create(context); |
| 28 | + |
| 29 | + const { returnType, fieldNodes } = gqlContext.getInfo<GraphQLResolveInfo>(); |
| 30 | + |
| 31 | + const targetClass = this.extractTargetClass(returnType); |
| 32 | + |
| 33 | + if (!targetClass) return next.handle(); |
| 34 | + |
| 35 | + const { user } = gqlContext.getContext<GraphQLContext>(); |
| 36 | + |
| 37 | + const selectField = this.extractSelectFields<T>([...fieldNodes]); |
| 38 | + |
| 39 | + return next.handle().pipe( |
| 40 | + map((data: T | T[]) => { |
| 41 | + if (Array.isArray(data)) { |
| 42 | + return data.map((v) => |
| 43 | + this.filterField(v, targetClass, selectField, user?.role), |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + return this.filterField(data, targetClass, selectField, user?.role); |
| 48 | + }), |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + private filterField<T extends Record<string, unknown>>( |
| 53 | + obj: T, |
| 54 | + targetClass: { prototype: object }, |
| 55 | + selectField: (keyof T)[], |
| 56 | + userRole?: User['role'], |
| 57 | + ) { |
| 58 | + if (!obj || typeof obj !== 'object') return obj; |
| 59 | + |
| 60 | + return Object.keys(obj) |
| 61 | + .filter((k) => selectField.includes(k)) |
| 62 | + .reduce((acc, cur: keyof T) => { |
| 63 | + const requiredRoles = Reflect.getMetadata( |
| 64 | + FIELD_ROLE, |
| 65 | + targetClass.prototype, |
| 66 | + cur as string, |
| 67 | + ) as User['role'][] | undefined; |
| 68 | + |
| 69 | + const isPermitted = this.hasAccess(requiredRoles, userRole); |
| 70 | + |
| 71 | + if (!isPermitted) throw new ForbiddenException(); |
| 72 | + |
| 73 | + acc[cur] = obj[cur]; |
| 74 | + |
| 75 | + return acc; |
| 76 | + }, {} as T); |
| 77 | + } |
| 78 | + |
| 79 | + private hasAccess<T extends User['role']>( |
| 80 | + requiredRole: T[] | undefined, |
| 81 | + userRole?: T, |
| 82 | + ) { |
| 83 | + if (userRole === Role.ADMIN) return true; |
| 84 | + |
| 85 | + return requiredRole?.some((role) => role === userRole) ?? true; |
| 86 | + } |
| 87 | + |
| 88 | + private extractSelectFields<T>(fieldNodes: FieldNode[]): (keyof T)[] { |
| 89 | + return fieldNodes.reduce( |
| 90 | + (acc, { selectionSet }) => { |
| 91 | + if (!selectionSet) return acc; |
| 92 | + |
| 93 | + selectionSet.selections.forEach((selection) => { |
| 94 | + if (selection.kind !== Kind.FIELD) return; |
| 95 | + |
| 96 | + acc.push(selection.name.value as keyof T); |
| 97 | + }); |
| 98 | + |
| 99 | + return acc; |
| 100 | + }, |
| 101 | + [] as (keyof T)[], |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + private extractTargetClass(returnType: GraphQLType) { |
| 106 | + const gqlType = getNamedType(returnType); |
| 107 | + |
| 108 | + const typeName = gqlType.name; |
| 109 | + |
| 110 | + const typeMetadata = TypeMetadataStorage.getObjectTypesMetadata().find( |
| 111 | + (metadata) => metadata.name === typeName, |
| 112 | + ); |
| 113 | + |
| 114 | + return typeMetadata?.target as { prototype: object }; |
| 115 | + } |
| 116 | +} |
0 commit comments