From 8015d59dbd41201c2a914f42080a06b3c8d1529a Mon Sep 17 00:00:00 2001 From: Johannes Nussbaum <39048939+jnussbaum@users.noreply.github.com> Date: Fri, 26 Jul 2024 15:31:40 +0200 Subject: [PATCH] fix: updating DOAPs with relative IRIs doesn't work (DEV-3921) (#118) --- dsp_permissions_scripts/doap/doap_set.py | 6 +--- dsp_permissions_scripts/oap/oap_set.py | 2 +- .../utils/scope_serialization.py | 7 ++-- tests/test_scope_serialization.py | 33 ++++++++++++------- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/dsp_permissions_scripts/doap/doap_set.py b/dsp_permissions_scripts/doap/doap_set.py index dcd74aab..f6047e78 100644 --- a/dsp_permissions_scripts/doap/doap_set.py +++ b/dsp_permissions_scripts/doap/doap_set.py @@ -6,7 +6,6 @@ from dsp_permissions_scripts.models.scope import PermissionScope from dsp_permissions_scripts.utils.dsp_client import DspClient from dsp_permissions_scripts.utils.get_logger import get_logger -from dsp_permissions_scripts.utils.helpers import KNORA_ADMIN_ONTO_NAMESPACE from dsp_permissions_scripts.utils.scope_serialization import create_admin_route_object_from_scope logger = get_logger(__name__) @@ -14,10 +13,7 @@ def _update_doap_scope_on_server(doap_iri: str, scope: PermissionScope, dsp_client: DspClient) -> Doap: iri = quote_plus(doap_iri, safe="") - payload = { - "hasPermissions": create_admin_route_object_from_scope(scope), - "context": {"knora-admin": KNORA_ADMIN_ONTO_NAMESPACE}, - } + payload = {"hasPermissions": create_admin_route_object_from_scope(scope)} try: response = dsp_client.put(f"/admin/permissions/{iri}/hasPermissions", data=payload) except ApiError as err: diff --git a/dsp_permissions_scripts/oap/oap_set.py b/dsp_permissions_scripts/oap/oap_set.py index 9b2c97a0..8ff3b14f 100644 --- a/dsp_permissions_scripts/oap/oap_set.py +++ b/dsp_permissions_scripts/oap/oap_set.py @@ -165,4 +165,4 @@ def apply_updated_oaps_on_server( ) msg = f"ERROR: {len(failed_iris)} resources or values could not be updated. They were written to {filename}." logger.error(msg) - logger.info(f"Updated OAPs of {len(oaps)} resources on {host}") + logger.info(f"Updated {res_oap_count} resource OAPs and {value_oap_count} value OAPs on {host}... *******") diff --git a/dsp_permissions_scripts/utils/scope_serialization.py b/dsp_permissions_scripts/utils/scope_serialization.py index 52a295fd..1930cfa4 100644 --- a/dsp_permissions_scripts/utils/scope_serialization.py +++ b/dsp_permissions_scripts/utils/scope_serialization.py @@ -40,14 +40,17 @@ def create_scope_from_admin_route_object(admin_route_object: list[dict[str, Any] def create_admin_route_object_from_scope(perm_scope: PermissionScope) -> list[dict[str, str | None]]: - """Serializes a permission scope to an object that can be used for requests to /admin/permissions routes.""" + """ + Serializes a permission scope to an object that can be used for requests to /admin/permissions routes. + Note: This route doesn't accept relative IRIs. + """ scope_elements: list[dict[str, str | None]] = [] for perm_letter in perm_scope.model_fields: groups = perm_scope.get(perm_letter) for group in groups: scope_elements.append( { - "additionalInformation": group.val, + "additionalInformation": group.val.replace("knora-admin:", KNORA_ADMIN_ONTO_NAMESPACE), "name": perm_letter, "permissionCode": None, } diff --git a/tests/test_scope_serialization.py b/tests/test_scope_serialization.py index c0e95911..da13cbb3 100644 --- a/tests/test_scope_serialization.py +++ b/tests/test_scope_serialization.py @@ -3,6 +3,7 @@ from dsp_permissions_scripts.models import group from dsp_permissions_scripts.models.scope import PermissionScope +from dsp_permissions_scripts.utils.helpers import KNORA_ADMIN_ONTO_NAMESPACE from dsp_permissions_scripts.utils.scope_serialization import create_admin_route_object_from_scope from dsp_permissions_scripts.utils.scope_serialization import create_scope_from_admin_route_object from dsp_permissions_scripts.utils.scope_serialization import create_scope_from_string @@ -18,24 +19,32 @@ class TestScopeSerialization: ) admin_route_objects = ( [ - {"name": "CR", "additionalInformation": "knora-admin:SystemAdmin", "permissionCode": None}, - {"name": "V", "additionalInformation": "knora-admin:CustomGroup", "permissionCode": None}, + {"name": "CR", "additionalInformation": f"{KNORA_ADMIN_ONTO_NAMESPACE}SystemAdmin", "permissionCode": None}, + {"name": "V", "additionalInformation": f"{KNORA_ADMIN_ONTO_NAMESPACE}CustomGroup", "permissionCode": None}, ], [ - {"name": "D", "additionalInformation": "knora-admin:ProjectAdmin", "permissionCode": None}, - {"name": "RV", "additionalInformation": "knora-admin:ProjectMember", "permissionCode": None}, + {"name": "D", "additionalInformation": f"{KNORA_ADMIN_ONTO_NAMESPACE}ProjectAdmin", "permissionCode": None}, + { + "name": "RV", + "additionalInformation": f"{KNORA_ADMIN_ONTO_NAMESPACE}ProjectMember", + "permissionCode": None, + }, ], [ - {"name": "M", "additionalInformation": "knora-admin:ProjectAdmin", "permissionCode": None}, - {"name": "V", "additionalInformation": "knora-admin:Creator", "permissionCode": None}, - {"name": "V", "additionalInformation": "knora-admin:KnownUser", "permissionCode": None}, - {"name": "RV", "additionalInformation": "knora-admin:UnknownUser", "permissionCode": None}, + {"name": "M", "additionalInformation": f"{KNORA_ADMIN_ONTO_NAMESPACE}ProjectAdmin", "permissionCode": None}, + {"name": "V", "additionalInformation": f"{KNORA_ADMIN_ONTO_NAMESPACE}Creator", "permissionCode": None}, + {"name": "V", "additionalInformation": f"{KNORA_ADMIN_ONTO_NAMESPACE}KnownUser", "permissionCode": None}, + {"name": "RV", "additionalInformation": f"{KNORA_ADMIN_ONTO_NAMESPACE}UnknownUser", "permissionCode": None}, ], [ - {"name": "CR", "additionalInformation": "knora-admin:SystemAdmin", "permissionCode": None}, - {"name": "CR", "additionalInformation": "knora-admin:ProjectAdmin", "permissionCode": None}, - {"name": "D", "additionalInformation": "knora-admin:Creator", "permissionCode": None}, - {"name": "RV", "additionalInformation": "knora-admin:UnknownUser", "permissionCode": None}, + {"name": "CR", "additionalInformation": f"{KNORA_ADMIN_ONTO_NAMESPACE}SystemAdmin", "permissionCode": None}, + { + "name": "CR", + "additionalInformation": f"{KNORA_ADMIN_ONTO_NAMESPACE}ProjectAdmin", + "permissionCode": None, + }, + {"name": "D", "additionalInformation": f"{KNORA_ADMIN_ONTO_NAMESPACE}Creator", "permissionCode": None}, + {"name": "RV", "additionalInformation": f"{KNORA_ADMIN_ONTO_NAMESPACE}UnknownUser", "permissionCode": None}, ], ) scopes = (