@@ -55,14 +55,14 @@ def _validate_permissions_and_get_ownership_info(
55
55
owner_id = current_user .id
56
56
57
57
# Fail fast without having to do DB lookups
58
- personal_users : list [UserRole ] = ["key_creator" , "user" ] # 'user' is the non-admin system user
59
- if user_role in personal_users :
58
+ team_users : list [UserRole ] = [UserRole . TEAM_ADMIN , UserRole . KEY_CREATOR ]
59
+ if user_role == UserRole . DEFAULT :
60
60
if owner_id != current_user .id or team_id is not None :
61
61
raise HTTPException (
62
62
status_code = status .HTTP_403_FORBIDDEN ,
63
63
detail = "Not authorized to perform this action"
64
64
)
65
- elif user_role == "admin" : # roles always refer to the team role, so admin is a team admin
65
+ elif user_role in team_users :
66
66
if team_id is not None and team_id != current_user .team_id :
67
67
raise HTTPException (
68
68
status_code = status .HTTP_403_FORBIDDEN ,
@@ -455,7 +455,7 @@ async def list_private_ai_keys(
455
455
else :
456
456
# Check if user is a team admin
457
457
if current_user .team_id is not None :
458
- if current_user .role == "admin" :
458
+ if current_user .role == UserRole . TEAM_ADMIN :
459
459
# Get all users in the team
460
460
team_users = db .query (DBUser ).filter (DBUser .team_id == current_user .team_id ).all ()
461
461
team_user_ids = [user .id for user in team_users ]
@@ -551,22 +551,25 @@ def _get_key_if_allowed(key_id: int, current_user: DBUser, user_role: UserRole,
551
551
)
552
552
553
553
# Check if user has permission to view the key
554
+ team_users : list [UserRole ] = [UserRole .TEAM_ADMIN , UserRole .KEY_CREATOR ]
554
555
if current_user .is_admin :
555
556
# System admin can view any key
556
557
pass
557
- elif user_role == "admin" :
558
- # Team admin can only view keys from their team
558
+ elif user_role in team_users :
559
+ # No cross team access
559
560
if private_ai_key .team_id is not None :
560
561
# For team-owned keys, check if it belongs to the admin's team
561
562
if private_ai_key .team_id != current_user .team_id :
563
+ logger .warning (f"User { current_user .id } trying to delete across teams." )
562
564
raise HTTPException (
563
565
status_code = status .HTTP_404_NOT_FOUND ,
564
566
detail = "Private AI Key not found"
565
567
)
566
568
else :
567
- # For user-owned keys, check if the owner is in the admin 's team
569
+ # For user-owned keys, check if the owner is in the viewer 's team
568
570
owner = db .query (DBUser ).filter (DBUser .id == private_ai_key .owner_id ).first ()
569
571
if not owner or owner .team_id != current_user .team_id :
572
+ logger .warning (f"Keys owned by different teams" )
570
573
raise HTTPException (
571
574
status_code = status .HTTP_404_NOT_FOUND ,
572
575
detail = "Private AI Key not found"
0 commit comments