|
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 |
@@ -147,15 +163,23 @@ def delete_tenant_realm(realm_id: str) -> tuple[dict, int]: |
147 | 163 | with ON DELETE RESTRICT. If any rows still reference this tenant, the delete returns |
148 | 164 | 409 and the caller must remove or reassign those references first (or use soft delete). |
149 | 165 | """ |
150 | | - auth_header = request.headers.get("Authorization") |
151 | | - if not auth_header or not auth_header.startswith("Bearer "): |
152 | | - return {"detail": "Authorization header with Bearer token is required"}, 401 |
153 | | - |
154 | | - admin_token = auth_header.split(" ")[1] |
155 | | - if not verify_admin_token(admin_token): |
156 | | - return {"detail": "Invalid or unauthorized admin token"}, 401 |
| 166 | + user = getattr(g, 'user', None) |
| 167 | + if not user: |
| 168 | + raise ApiError(error_code="not_authenticated", message="User not authenticated", status_code=401) |
| 169 | + |
| 170 | + is_authorized = AuthorizationService.user_has_permission(user, "delete", request.path) |
| 171 | + |
| 172 | + if not is_authorized: |
| 173 | + logger.warning( |
| 174 | + "User %s (groups: %s) attempted to delete tenant %s without required permissions", |
| 175 | + user.username, |
| 176 | + [getattr(g, 'identifier', g.name) for g in getattr(user, 'groups', [])], |
| 177 | + realm_id |
| 178 | + ) |
| 179 | + raise ApiError(error_code="forbidden", message="Not authorized to delete a tenant.", status_code=403) |
157 | 180 |
|
158 | 181 | try: |
| 182 | + admin_token = get_master_admin_token() |
159 | 183 | # Delete from Keycloak first. If this raises, we do not touch Postgres. |
160 | 184 | delete_realm(realm_id, admin_token=admin_token) |
161 | 185 |
|
|
0 commit comments