|
| 1 | +import json |
| 2 | +from typing import Any, List, Optional |
| 3 | + |
| 4 | +import cwms.api as api |
| 5 | +from cwms.cwms_types import Data |
| 6 | + |
| 7 | + |
| 8 | +def _raise_user_management_error(error: api.ApiError, action: str) -> None: |
| 9 | + status_code = getattr(error.response, "status_code", None) |
| 10 | + if status_code == 403: |
| 11 | + response_hint = getattr(error.response, "reason", None) or "Forbidden" |
| 12 | + message = ( |
| 13 | + f"{action} could not be completed because the current credentials " |
| 14 | + "are not authorized for user-management access or are missing the " |
| 15 | + f"required role assignment. CDA responded with 403 {response_hint}." |
| 16 | + ) |
| 17 | + raise api.PermissionError(error.response, message) from None |
| 18 | + raise error |
| 19 | + |
| 20 | + |
| 21 | +def get_roles() -> List[str]: |
| 22 | + """Retrieve all available user-management roles.""" |
| 23 | + |
| 24 | + try: |
| 25 | + response = api.get("roles", api_version=1) |
| 26 | + except api.ApiError as error: |
| 27 | + _raise_user_management_error(error, "User role lookup") |
| 28 | + return list(response) |
| 29 | + |
| 30 | + |
| 31 | +def get_user_profile() -> dict[str, Any]: |
| 32 | + """Retrieve the profile for the currently authenticated user.""" |
| 33 | + |
| 34 | + response = api.get("user/profile", api_version=1) |
| 35 | + return dict(response) |
| 36 | + |
| 37 | + |
| 38 | +def get_users( |
| 39 | + office_id: Optional[str] = None, |
| 40 | + page: Optional[str] = None, |
| 41 | + page_size: Optional[int] = None, |
| 42 | +) -> Data: |
| 43 | + """Retrieve users with optional office and paging filters.""" |
| 44 | + |
| 45 | + params = {"office": office_id, "page": page, "page-size": page_size} |
| 46 | + try: |
| 47 | + response = api.get("users", params=params, api_version=1) |
| 48 | + except api.ApiError as error: |
| 49 | + _raise_user_management_error(error, "User list lookup") |
| 50 | + return Data(response, selector="users") |
| 51 | + |
| 52 | + |
| 53 | +def get_user(user_name: str) -> dict[str, Any]: |
| 54 | + """Retrieve a single user by user name.""" |
| 55 | + |
| 56 | + if not user_name: |
| 57 | + raise ValueError("Get user requires a user name") |
| 58 | + try: |
| 59 | + response = api.get(f"users/{user_name}", api_version=1) |
| 60 | + except api.ApiError as error: |
| 61 | + status_code = getattr(error.response, "status_code", None) |
| 62 | + if status_code == 404: |
| 63 | + raise api.NotFoundError( |
| 64 | + error.response, f"User '{user_name}' was not found." |
| 65 | + ) from None |
| 66 | + if status_code == 403: |
| 67 | + _raise_user_management_error(error, f"User '{user_name}' retrieval") |
| 68 | + raise |
| 69 | + return dict(response) |
| 70 | + |
| 71 | + |
| 72 | +def store_user(user_name: str, office_id: str, roles: List[str]) -> None: |
| 73 | + """Create a user role assignment for an office. |
| 74 | +
|
| 75 | + Notes |
| 76 | + ----- |
| 77 | + The CDA User Management API creates/manages user access through role assignment |
| 78 | + at `/user/{user-name}/roles/{office-id}`. |
| 79 | + """ |
| 80 | + |
| 81 | + if not user_name: |
| 82 | + raise ValueError("Store user requires a user name") |
| 83 | + if not office_id: |
| 84 | + raise ValueError("Store user requires an office id") |
| 85 | + if not roles: |
| 86 | + raise ValueError("Store user requires a roles list") |
| 87 | + |
| 88 | + endpoint = f"user/{user_name}/roles/{office_id}" |
| 89 | + try: |
| 90 | + api.post(endpoint, roles) |
| 91 | + except api.ApiError as error: |
| 92 | + _raise_user_management_error( |
| 93 | + error, f"User '{user_name}' role assignment update" |
| 94 | + ) |
| 95 | + |
| 96 | + |
| 97 | +def delete_user_roles(user_name: str, office_id: str, roles: List[str]) -> None: |
| 98 | + """Delete user role assignments for an office.""" |
| 99 | + |
| 100 | + if not user_name: |
| 101 | + raise ValueError("Delete user roles requires a user name") |
| 102 | + if not office_id: |
| 103 | + raise ValueError("Delete user roles requires an office id") |
| 104 | + if roles is None: |
| 105 | + raise ValueError("Delete user roles requires a roles list") |
| 106 | + |
| 107 | + endpoint = f"user/{user_name}/roles/{office_id}" |
| 108 | + headers = {"accept": "*/*", "Content-Type": api.api_version_text(api.API_VERSION)} |
| 109 | + # TODO: Delete does not currently support a body in the api module. Use SESSION directly |
| 110 | + with api.SESSION.delete( |
| 111 | + endpoint, headers=headers, data=json.dumps(roles) |
| 112 | + ) as response: |
| 113 | + if not response.ok: |
| 114 | + _raise_user_management_error( |
| 115 | + api.ApiError(response), f"User '{user_name}' role deletion" |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +def update_user(user_name: str, office_id: str, roles: List[str]) -> None: |
| 120 | + """Update a user's roles for an office by replacing the current role set.""" |
| 121 | + |
| 122 | + if not user_name: |
| 123 | + raise ValueError("Update user requires a user name") |
| 124 | + if not office_id: |
| 125 | + raise ValueError("Update user requires an office id") |
| 126 | + if not roles: |
| 127 | + raise ValueError("Update user requires a roles list") |
| 128 | + |
| 129 | + endpoint = f"user/{user_name}/roles/{office_id}" |
| 130 | + user = get_user(user_name) |
| 131 | + |
| 132 | + roles_by_office = user.get("roles") |
| 133 | + if isinstance(roles_by_office, dict): |
| 134 | + existing_roles = roles_by_office.get(office_id, []) |
| 135 | + elif isinstance(roles_by_office, list): |
| 136 | + existing_roles = roles_by_office |
| 137 | + else: |
| 138 | + existing_roles = [] |
| 139 | + |
| 140 | + if not isinstance(existing_roles, list): |
| 141 | + existing_roles = [] |
| 142 | + |
| 143 | + desired_roles = sorted(set(roles)) |
| 144 | + current_roles = sorted(set(existing_roles)) |
| 145 | + # Determine roles to add and remove |
| 146 | + roles_to_remove = [role for role in current_roles if role not in desired_roles] |
| 147 | + roles_to_add = [role for role in desired_roles if role not in current_roles] |
| 148 | + |
| 149 | + if roles_to_remove: |
| 150 | + delete_user_roles(user_name, office_id, roles_to_remove) |
| 151 | + if roles_to_add: |
| 152 | + try: |
| 153 | + api.post(endpoint, roles_to_add) |
| 154 | + except api.ApiError as error: |
| 155 | + _raise_user_management_error(error, f"User '{user_name}' role replacement") |
0 commit comments