@@ -41,20 +41,17 @@ export const createImpersonationRequestService = async (
41
41
body : CreateImpersonationRequestServiceBody
42
42
) : Promise < ImpersonationRequest > => {
43
43
try {
44
- const { userExists, user : impersonatedUser } = await fetchUser ( { userId : body . impersonatedUserId } ) ;
44
+ const { userExists } = await fetchUser ( { userId : body . createdFor } ) ;
45
45
if ( ! userExists ) {
46
46
throw new NotFound ( TASK_REQUEST_MESSAGES . USER_NOT_FOUND ) ;
47
47
}
48
48
49
- const { username : createdFor } = impersonatedUser as User ;
50
49
51
50
const impersonationRequest = await createImpersonationRequest ( {
52
51
status : REQUEST_STATE . PENDING ,
53
- userId : body . userId ,
54
- impersonatedUserId : body . impersonatedUserId ,
55
- isImpersonationFinished : false ,
56
52
createdBy : body . createdBy ,
57
- createdFor : createdFor ,
53
+ createdFor : body . createdFor ,
54
+ isImpersonationFinished : false ,
58
55
reason : body . reason ,
59
56
} ) ;
60
57
@@ -63,7 +60,7 @@ export const createImpersonationRequestService = async (
63
60
meta : {
64
61
requestId : impersonationRequest . id ,
65
62
action : LOG_ACTION . CREATE ,
66
- userId : body . userId ,
63
+ userId : body . createdBy ,
67
64
createdAt : Date . now ( ) ,
68
65
} ,
69
66
body : {
@@ -103,7 +100,7 @@ export const updateImpersonationRequestService = async (
103
100
throw new NotFound ( REQUEST_DOES_NOT_EXIST ) ;
104
101
}
105
102
106
- if ( request . impersonatedUserId !== body . lastModifiedBy || request . status !== REQUEST_STATE . PENDING ) {
103
+ if ( request . createdFor !== body . lastModifiedBy || request . status !== REQUEST_STATE . PENDING ) {
107
104
throw new Forbidden ( OPERATION_NOT_ALLOWED ) ;
108
105
}
109
106
@@ -156,7 +153,7 @@ export const startImpersonationService = async (
156
153
}
157
154
158
155
if (
159
- body . userId !== impersonationRequest . userId ||
156
+ body . userId !== impersonationRequest . createdBy ||
160
157
impersonationRequest . status !== REQUEST_STATE . APPROVED ||
161
158
impersonationRequest . isImpersonationFinished === true
162
159
) {
@@ -218,7 +215,7 @@ export const stopImpersonationService = async (
218
215
if ( ! impersonationRequest ) {
219
216
throw new NotFound ( REQUEST_DOES_NOT_EXIST ) ;
220
217
}
221
- if ( body . userId !== impersonationRequest . impersonatedUserId ) {
218
+ if ( body . userId !== impersonationRequest . createdFor ) {
222
219
throw new Forbidden ( OPERATION_NOT_ALLOWED ) ;
223
220
}
224
221
@@ -267,31 +264,28 @@ export const stopImpersonationService = async (
267
264
export const generateImpersonationTokenService = async (
268
265
requestId : string ,
269
266
action : string
270
- ) : Promise < { name : string , value : string , options : object } > => {
271
- try {
267
+ ) : Promise < { name : string ; value : string ; options : object } > => {
268
+ const cookieName = config . get < string > ( "userToken.cookieName" ) ;
269
+ const rdsUiUrl = new URL ( config . get < string > ( "services.rdsUi.baseUrl" ) ) ;
270
+ const ttlInSeconds = Number ( config . get ( "userToken.ttl" ) ) ;
271
+ try {
272
272
const request = await getImpersonationRequestById ( requestId ) ;
273
273
if ( ! request ) {
274
274
throw new NotFound ( REQUEST_DOES_NOT_EXIST ) ;
275
275
}
276
276
277
- const { userId, impersonatedUserId } = request ;
278
- const cookieName = config . get < string > ( "userToken.cookieName" ) ;
279
- const rdsUiUrl = new URL ( config . get < string > ( "services.rdsUi.baseUrl" ) ) ;
280
- const ttlInSeconds = Number ( config . get ( "userToken.ttl" ) ) ;
277
+ const { createdBy : userId , createdFor : impersonatedUserId } = request ;
281
278
282
- let token : string ;
283
-
284
- switch ( action ) {
285
- case "START" :
286
- token = await authService . generateImpersonationAuthToken ( { userId, impersonatedUserId } ) ;
287
- break ;
288
279
289
- case "STOP" :
290
- token = await authService . generateAuthToken ( { userId } ) ;
291
- break ;
280
+ let token : string ;
292
281
293
- default :
294
- throw new BadRequest ( INVALID_ACTION_PARAM ) ;
282
+ if ( action === "START" ) {
283
+ token = await authService . generateImpersonationAuthToken ( {
284
+ userId,
285
+ impersonatedUserId,
286
+ } ) ;
287
+ } else if ( action === "STOP" ) {
288
+ token = await authService . generateAuthToken ( { userId } ) ;
295
289
}
296
290
297
291
return {
0 commit comments