|
31 | 31 |
|
32 | 32 | def create_realm(body: dict) -> tuple[dict, int]: |
33 | 33 | """Create a spoke realm from the spiffworkflow template. Returns (response_dict, status_code).""" |
| 34 | + |
| 35 | + user = getattr(g, 'user', None) |
| 36 | + if not user: |
| 37 | + raise ApiError(error_code="not_authenticated", message="User not authenticated", status_code=401) |
| 38 | + |
| 39 | + is_authorized = AuthorizationService.user_has_permission(user, "create", request.path) |
| 40 | + |
| 41 | + if not is_authorized: |
| 42 | + logger.warning( |
| 43 | + "User %s (groups: %s) attempted to create a tenant/realm without required permissions", |
| 44 | + user.username, |
| 45 | + [getattr(g, 'identifier', g.name) for g in getattr(user, 'groups', [])], |
| 46 | + ) |
| 47 | + raise ApiError(error_code="forbidden", message="Not authorized to create a tenant.", status_code=403) |
| 48 | + |
| 49 | + |
34 | 50 | realm_id = body.get("realm_id") |
35 | 51 | if not realm_id or not str(realm_id).strip(): |
36 | 52 | return {"detail": "realm_id is required"}, 400 |
@@ -134,15 +150,23 @@ def delete_tenant_realm(realm_id: str) -> tuple[dict, int]: |
134 | 150 | with ON DELETE RESTRICT. If any rows still reference this tenant, the delete returns |
135 | 151 | 409 and the caller must remove or reassign those references first (or use soft delete). |
136 | 152 | """ |
137 | | - auth_header = request.headers.get("Authorization") |
138 | | - if not auth_header or not auth_header.startswith("Bearer "): |
139 | | - return {"detail": "Authorization header with Bearer token is required"}, 401 |
140 | | - |
141 | | - admin_token = auth_header.split(" ")[1] |
142 | | - if not verify_admin_token(admin_token): |
143 | | - return {"detail": "Invalid or unauthorized admin token"}, 401 |
| 153 | + user = getattr(g, 'user', None) |
| 154 | + if not user: |
| 155 | + raise ApiError(error_code="not_authenticated", message="User not authenticated", status_code=401) |
| 156 | + |
| 157 | + is_authorized = AuthorizationService.user_has_permission(user, "delete", request.path) |
| 158 | + |
| 159 | + if not is_authorized: |
| 160 | + logger.warning( |
| 161 | + "User %s (groups: %s) attempted to delete tenant %s without required permissions", |
| 162 | + user.username, |
| 163 | + [getattr(g, 'identifier', g.name) for g in getattr(user, 'groups', [])], |
| 164 | + realm_id |
| 165 | + ) |
| 166 | + raise ApiError(error_code="forbidden", message="Not authorized to delete a tenant.", status_code=403) |
144 | 167 |
|
145 | 168 | try: |
| 169 | + admin_token = get_master_admin_token() |
146 | 170 | # Delete from Keycloak first. If this raises, we do not touch Postgres. |
147 | 171 | delete_realm(realm_id, admin_token=admin_token) |
148 | 172 |
|
|
0 commit comments