2
2
from fastapi import Depends
3
3
from fastapi import HTTPException
4
4
from fastapi import Response
5
+ from fastapi_users import exceptions
5
6
6
7
from ee .onyx .auth .users import current_cloud_superuser
7
8
from ee .onyx .server .tenants .models import ImpersonateRequest
@@ -24,14 +25,24 @@ async def impersonate_user(
24
25
_ : User = Depends (current_cloud_superuser ),
25
26
) -> Response :
26
27
"""Allows a cloud superuser to impersonate another user by generating an impersonation JWT token"""
27
- tenant_id = get_tenant_id_for_email (impersonate_request .email )
28
+ try :
29
+ tenant_id = get_tenant_id_for_email (impersonate_request .email )
30
+ except exceptions .UserNotExists :
31
+ detail = f"User has no tenant mapping: { impersonate_request .email = } "
32
+ logger .warning (detail )
33
+ raise HTTPException (status_code = 422 , detail = detail )
28
34
29
35
with get_session_with_tenant (tenant_id = tenant_id ) as tenant_session :
30
36
user_to_impersonate = get_user_by_email (
31
37
impersonate_request .email , tenant_session
32
38
)
33
39
if user_to_impersonate is None :
34
- raise HTTPException (status_code = 404 , detail = "User not found" )
40
+ detail = (
41
+ f"User not found in tenant: { impersonate_request .email = } { tenant_id = } "
42
+ )
43
+ logger .warning (detail )
44
+ raise HTTPException (status_code = 422 , detail = detail )
45
+
35
46
token = await get_redis_strategy ().write_token (user_to_impersonate )
36
47
37
48
response = await auth_backend .transport .get_login_response (token )
0 commit comments