forked from Shared/esg
Implement normalized error handling with Persian translations across all exception types, replace legacy NestJS exceptions with AppException, and add unit, integration, and smoke tests. - Add error catalog with gateway-owned codes and Persian messages - Introduce AppException wrapping normalized error envelopes - Add translateError helper for automatic messageFa population - Remove claims module and update provider error normalization - Add unit tests for error contracts and helper functions - Add integration tests for admin and inquiry endpoints - Add smoke tests for real provider connectivity - Add test support utilities (auth mocks, assertions, app factory)
48 lines
1.6 KiB
TypeScript
48 lines
1.6 KiB
TypeScript
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
|
|
import { Reflector } from '@nestjs/core';
|
|
import { Request } from 'express';
|
|
import { ADMIN_ROLES } from '../../common/enums/role.enum';
|
|
import { InquiryType } from '../../common/enums/inquiry-type.enum';
|
|
import { INQUIRY_ACCESS_KEY } from '../constants/auth.constants';
|
|
import { AuthenticatedUser } from '../interfaces/authenticated-user.interface';
|
|
import { AppException } from '../../common/exceptions/app-exception';
|
|
|
|
/**
|
|
* Validates that the user may call a specific inquiry endpoint.
|
|
* ADMIN / SUPER_ADMIN bypass — external USER clients need allowedInquiries.
|
|
*/
|
|
@Injectable()
|
|
export class InquiryAccessGuard implements CanActivate {
|
|
constructor(private readonly reflector: Reflector) {}
|
|
|
|
canActivate(context: ExecutionContext): boolean {
|
|
const requiredInquiry = this.reflector.getAllAndOverride<InquiryType>(INQUIRY_ACCESS_KEY, [
|
|
context.getHandler(),
|
|
context.getClass(),
|
|
]);
|
|
|
|
if (!requiredInquiry) {
|
|
return true;
|
|
}
|
|
|
|
const request = context.switchToHttp().getRequest<Request & { user: AuthenticatedUser }>();
|
|
const user = request.user;
|
|
|
|
if (!user) {
|
|
throw new AppException('AUTHENTICATION_REQUIRED');
|
|
}
|
|
|
|
if (ADMIN_ROLES.includes(user.role)) {
|
|
return true;
|
|
}
|
|
|
|
// Temporarily disabled: allow every authenticated user/client to call every inquiry.
|
|
// const inquiryKey = requiredInquiry as string;
|
|
// if (!user.allowedInquiries.includes(inquiryKey)) {
|
|
// throw new ForbiddenException(`Access denied for inquiry: ${inquiryKey}`);
|
|
// }
|
|
|
|
return true;
|
|
}
|
|
}
|