Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Oct 19, 2024
1 parent 03dc863 commit 6fc72e3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
12 changes: 11 additions & 1 deletion dsp_permissions_scripts/models/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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")
Expand Down
13 changes: 12 additions & 1 deletion dsp_permissions_scripts/models/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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]]:
Expand Down

0 comments on commit 6fc72e3

Please sign in to comment.