"""Interface to the ows-permissions microservice.""" from owsrequest import request from collaborator.constants import error, header, service_name from collaborator.models.ows import ows_users from collaborator.utils import owsrequest from collaborator.utils.error import OwsError def get_permissions(profile_type, profile_id, resource_type) -> dict: """Get the resources that a profile has access to. profile_type (str): the user's profile type e.g. CollaboratorsProfile profile_id (str): the user's profile_id resource_type (str): the type of resource to fetch Returns: oto.response.Response """ permissions_url = ( f"/admin/profile-type/{profile_type}/" f"profile/{profile_id}/resource/{resource_type}" ) res = request.get(service_name.OWS_PERMISSIONS, permissions_url) return owsrequest.unwrap_data_or_error(res, error.ERROR_CODE_OWS_PERMISSIONS) def get_admin_access_by_resource_type(identity_id: str, resource_type: str) -> dict: """Get the resources that a profile has admin access to. identity_id (str): the user's identity ID Returns: oto.response.Response """ settings_profile = ows_users.get_profile_for_identity( header.SETTINGS_PROFILE, identity_id ) if not settings_profile: raise OwsError.not_found(message=error.ERROR_MESSAGE_PROFILE_NOT_FOUND) res = request.get( service_name.OWS_PERMISSIONS, f"/identity/{identity_id}/direct-access/resources/{resource_type}", params={"active": True}, headers={ "Orchard-Identity-Id": identity_id, "Orchard-Profile-Id": str(settings_profile["profile_id"]), "Orchard-Profile-Type": settings_profile["profile_type"], }, ) return owsrequest.unwrap_data_or_error(res, error.ERROR_CODE_OWS_PERMISSIONS)