1- import { NextFunction , Response } from 'express' ;
1+ import { NextFunction , Request , Response } from 'express' ;
22import { z } from 'zod' ;
33
44import { User , UserJobFunction } from '../entities/User' ;
55import { getLoggers , getTraceId , TraceId } from '../helpers/loggingHelpers' ;
6- import { UserParticipantRequest } from '../services/participantsService' ;
6+ import { getAllParticipants , UserParticipantRequest } from '../services/participantsService' ;
77import { findUserByEmail , UserRequest } from '../services/usersService' ;
8- import { isSuperUser , isUid2Support } from './userRoleMiddleware' ;
8+ import { isSuperUser , isUid2InternalEmail , isUid2Support } from './userRoleMiddleware' ;
99
10- // Helper to check if email is a UID2 internal email
11- const isUid2InternalEmail = ( email : string ) => email . toLowerCase ( ) . endsWith ( '@unifiedid.com' ) ;
10+ // Extended user type with support role flags
11+ type UserWithSupportRoles = User & { isUid2Support : boolean ; isSuperUser : boolean } ;
1212
1313// Create a new @unifiedid .com user in the portal database from Keycloak token data
1414const createUid2InternalUser = async (
@@ -49,14 +49,16 @@ export const isUserBelongsToParticipant = async (
4949} ;
5050
5151export const canUserAccessParticipant = async (
52- requestingUserEmail : string ,
52+ req : Request ,
5353 participantId : number ,
5454 traceId : TraceId
5555) => {
56- return (
57- ( await isUid2Support ( requestingUserEmail ) ) ||
58- ( await isUserBelongsToParticipant ( requestingUserEmail , participantId , traceId ) )
59- ) ;
56+ const requestingUserEmail = req . auth ?. payload ?. email as string ;
57+ // SuperUsers and UID2Support have access to all participants
58+ if ( isSuperUser ( req ) || ( await isUid2Support ( requestingUserEmail ) ) ) {
59+ return true ;
60+ }
61+ return isUserBelongsToParticipant ( requestingUserEmail , participantId , traceId ) ;
6062} ;
6163
6264export const enrichCurrentUser = async ( req : UserRequest , res : Response , next : NextFunction ) => {
@@ -77,18 +79,19 @@ export const enrichCurrentUser = async (req: UserRequest, res: Response, next: N
7779 if ( user . locked ) {
7880 return res . status ( 403 ) . send ( [ { message : 'Unauthorized.' } ] ) ;
7981 }
80- req . user = user ;
81- return next ( ) ;
82- } ;
8382
84- export const enrichUserWithSupportRoles = async ( user : User ) => {
85- const userIsUid2Support = await isUid2Support ( user . email ) ;
86- const userIsSuperUser = await isSuperUser ( user . email ) ;
87- return {
88- ...user ,
89- isUid2Support : userIsUid2Support ,
90- isSuperUser : userIsSuperUser ,
91- } ;
83+ // Enrich user with support roles and participants
84+ const enrichedUser = user as UserWithSupportRoles ;
85+ enrichedUser . isUid2Support = await isUid2Support ( userEmail ) ;
86+ enrichedUser . isSuperUser = isSuperUser ( req ) ;
87+
88+ // SuperUsers and UID2Support get all participants
89+ if ( enrichedUser . isSuperUser || enrichedUser . isUid2Support ) {
90+ enrichedUser . participants = await getAllParticipants ( ) ;
91+ }
92+
93+ req . user = enrichedUser ;
94+ return next ( ) ;
9295} ;
9396
9497const userIdSchema = z . object ( {
@@ -122,9 +125,8 @@ export const verifyAndEnrichUser = async (
122125 return res . status ( 404 ) . send ( [ { message : 'The user cannot be found.' } ] ) ;
123126 }
124127
125- const requestingUserEmail = req . auth ?. payload ?. email as string ;
126128 const canRequestingUserAccessParticipant = await canUserAccessParticipant (
127- requestingUserEmail ,
129+ req ,
128130 participant ! . id ,
129131 traceId
130132 ) ;
0 commit comments