From 6fc72e3e7e438f4369eb2b819e4fd767e74db45d Mon Sep 17 00:00:00 2001 From: Johannes Nussbaum Date: Sat, 19 Oct 2024 12:56:46 +0200 Subject: [PATCH] fix --- dsp_permissions_scripts/models/group.py | 12 +++++++++++- dsp_permissions_scripts/models/scope.py | 13 ++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/dsp_permissions_scripts/models/group.py b/dsp_permissions_scripts/models/group.py index f35d0f99..c1999cfa 100644 --- a/dsp_permissions_scripts/models/group.py +++ b/dsp_permissions_scripts/models/group.py @@ -21,6 +21,16 @@ from dsp_permissions_scripts.utils.dsp_client import DspClient KNORA_ADMIN_ONTO_NAMESPACE = "http://www.knora.org/ontology/knora-admin#" +PREFIXED_IRI_REGEX = r"^[\w-]+:[\w-]+$" + + +def is_prefixed_iri(iri: str) -> bool: + if iri.startswith((KNORA_ADMIN_ONTO_NAMESPACE, "http://rdfh.ch/")): + return False + elif re.search(PREFIXED_IRI_REGEX, iri): + return True + else: + raise InvalidIRIError(f"{iri} is not a valid IRI") def get_prefixed_iri_from_full_iri(full_iri: str, dsp_client: DspClient) -> str: @@ -41,7 +51,7 @@ def get_prefixed_iri_from_full_iri(full_iri: str, dsp_client: DspClient) -> str: def group_builder(prefixed_iri: str) -> BuiltinGroup | CustomGroup: if prefixed_iri.startswith("knora-admin:"): return BuiltinGroup(prefixed_iri=prefixed_iri) - elif re.search(r"^[\w-]+:[\w-]+$", prefixed_iri): + elif re.search(PREFIXED_IRI_REGEX, prefixed_iri): return CustomGroup(prefixed_iri=prefixed_iri) else: raise InvalidGroupError(f"{prefixed_iri} is not a valid group IRI") diff --git a/dsp_permissions_scripts/models/scope.py b/dsp_permissions_scripts/models/scope.py index d3b69e12..2a4c46b5 100644 --- a/dsp_permissions_scripts/models/scope.py +++ b/dsp_permissions_scripts/models/scope.py @@ -14,7 +14,9 @@ from dsp_permissions_scripts.models.group import PROJECT_MEMBER from dsp_permissions_scripts.models.group import UNKNOWN_USER from dsp_permissions_scripts.models.group import GroupType +from dsp_permissions_scripts.models.group import get_prefixed_iri_from_full_iri from dsp_permissions_scripts.models.group import group_builder +from dsp_permissions_scripts.models.group import is_prefixed_iri from dsp_permissions_scripts.utils.dsp_client import DspClient @@ -49,7 +51,16 @@ def create( @staticmethod def from_dict(d: dict[str, list[str]], dsp_client: DspClient) -> PermissionScope: purged_kwargs = PermissionScope._remove_duplicates_from_kwargs(d) - return PermissionScope.model_validate({k: [group_builder(v) for v in vs] for k, vs in purged_kwargs.items()}) + purged_kwargs_final = {} + for k, vs in purged_kwargs.items(): + groups = [] + for v in vs: + if is_prefixed_iri(v): + groups.append(group_builder(v)) + else: + groups.append(group_builder(get_prefixed_iri_from_full_iri(v, dsp_client))) + purged_kwargs_final[k] = groups + return PermissionScope.model_validate(purged_kwargs_final) @staticmethod def _remove_duplicates_from_kwargs(kwargs: dict[str, list[str]]) -> dict[str, list[str]]: