Skip to content

Commit

Permalink
Merge branch 'main' into wip/080E-limc
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum authored Aug 12, 2024
2 parents b9c0881 + 1e46718 commit 6b0215e
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 21 deletions.
3 changes: 1 addition & 2 deletions dsp_permissions_scripts/ap/ap_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from dsp_permissions_scripts.models.group import Group
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.project import get_project_iri_and_onto_iris_by_shortcode

logger = get_logger(__name__)
Expand All @@ -16,7 +15,7 @@
def create_ap_from_admin_route_object(permission: dict[str, Any]) -> Ap:
"""Deserializes a AP from JSON as returned by /admin/permissions/ap/{project_iri}"""
ap = Ap(
forGroup=Group(val=permission["forGroup"].replace(KNORA_ADMIN_ONTO_NAMESPACE, "knora-admin:")),
forGroup=Group(val=permission["forGroup"]),
forProject=permission["forProject"],
hasPermissions=frozenset(ApValue(p["name"]) for p in permission["hasPermissions"]),
iri=permission["iri"],
Expand Down
3 changes: 1 addition & 2 deletions dsp_permissions_scripts/ap/ap_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from dsp_permissions_scripts.models.group import Group
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.project import get_project_iri_and_onto_iris_by_shortcode

logger = get_logger(__name__)
Expand Down Expand Up @@ -50,7 +49,7 @@ def create_new_ap_on_server(
) -> Ap | None:
proj_iri, _ = get_project_iri_and_onto_iris_by_shortcode(shortcode, dsp_client)
payload = {
"forGroup": forGroup.val.replace("knora-admin:", KNORA_ADMIN_ONTO_NAMESPACE),
"forGroup": forGroup.full_iri(),
"forProject": proj_iri,
"hasPermissions": [
{"additionalInformation": None, "name": ap_val.value, "permissionCode": None} for ap_val in hasPermissions
Expand Down
7 changes: 1 addition & 6 deletions dsp_permissions_scripts/doap/doap_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from dsp_permissions_scripts.models.group import Group
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.project import get_project_iri_and_onto_iris_by_shortcode
from dsp_permissions_scripts.utils.scope_serialization import create_scope_from_admin_route_object

Expand Down Expand Up @@ -50,14 +49,10 @@ def _get_all_doaps_of_project(project_iri: str, dsp_client: DspClient) -> list[D
def create_doap_from_admin_route_response(permission: dict[str, Any]) -> Doap:
"""Deserializes a DOAP from JSON as returned by /admin/permissions/doap/{project_iri}"""
scope = create_scope_from_admin_route_object(permission["hasPermissions"])
if permission.get("forGroup"):
relative_group_iri = permission["forGroup"].replace(KNORA_ADMIN_ONTO_NAMESPACE, "knora-admin:")
else:
relative_group_iri = None
doap = Doap(
target=DoapTarget(
project_iri=permission["forProject"],
group=Group(val=relative_group_iri) if relative_group_iri else None,
group=Group(val=permission["forGroup"]) if permission.get("forGroup") else None,
resource_class=permission.get("forResourceClass"),
property=permission.get("forProperty"),
),
Expand Down
3 changes: 1 addition & 2 deletions dsp_permissions_scripts/doap/doap_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,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.project import get_project_iri_and_onto_iris_by_shortcode
from dsp_permissions_scripts.utils.scope_serialization import create_admin_route_object_from_scope

Expand Down Expand Up @@ -48,7 +47,7 @@ def create_new_doap_on_server(
) -> Doap | None:
proj_iri, _ = get_project_iri_and_onto_iris_by_shortcode(shortcode, dsp_client)
payload = {
"forGroup": target.group.val.replace("knora-admin:", KNORA_ADMIN_ONTO_NAMESPACE) if target.group else None,
"forGroup": target.group.full_iri() if target.group else None,
"forProject": proj_iri,
"forProperty": target.property,
"forResourceClass": target.resource_class,
Expand Down
15 changes: 15 additions & 0 deletions dsp_permissions_scripts/models/group.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
from __future__ import annotations

from typing import Any

from pydantic import BaseModel
from pydantic import ConfigDict
from pydantic import model_validator

from dsp_permissions_scripts.models.errors import InvalidGroupError

KNORA_ADMIN_ONTO_NAMESPACE = "http://www.knora.org/ontology/knora-admin#"


class Group(BaseModel):
model_config = ConfigDict(frozen=True)

val: str

@model_validator(mode="before")
@classmethod
def _shorten_iri(cls, data: Any) -> Any:
if not isinstance(data, dict):
return data
data["val"] = data["val"].replace(KNORA_ADMIN_ONTO_NAMESPACE, "knora-admin:")
return data

@model_validator(mode="after")
def _check_regex(self) -> Group:
if not self.val.startswith("knora-admin:"):
raise InvalidGroupError(f"{self.val} is not a valid group IRI")
return self

def full_iri(self) -> str:
return self.val.replace("knora-admin:", KNORA_ADMIN_ONTO_NAMESPACE)


UNKNOWN_USER = Group(val="knora-admin:UnknownUser")
KNOWN_USER = Group(val="knora-admin:KnownUser")
Expand Down
2 changes: 1 addition & 1 deletion dsp_permissions_scripts/oap/oap_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

from dsp_permissions_scripts.models.errors import ApiError
from dsp_permissions_scripts.models.errors import PermissionsAlreadyUpToDate
from dsp_permissions_scripts.models.group import KNORA_ADMIN_ONTO_NAMESPACE
from dsp_permissions_scripts.models.scope import PermissionScope
from dsp_permissions_scripts.oap.oap_model import ResourceOap
from dsp_permissions_scripts.oap.oap_model import ValueOap
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_string_from_scope

logger = get_logger(__name__)
Expand Down
1 change: 0 additions & 1 deletion dsp_permissions_scripts/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from dsp_permissions_scripts.models import group

PACKAGE_NAME = "dsp-permissions-scripts"
KNORA_ADMIN_ONTO_NAMESPACE = "http://www.knora.org/ontology/knora-admin#"


def dereference_prefix(
Expand Down
10 changes: 4 additions & 6 deletions dsp_permissions_scripts/utils/scope_serialization.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Any

from dsp_permissions_scripts.models.scope import PermissionScope
from dsp_permissions_scripts.utils.helpers import KNORA_ADMIN_ONTO_NAMESPACE
from dsp_permissions_scripts.utils.helpers import sort_groups


Expand All @@ -22,7 +21,7 @@ def create_scope_from_string(permission_string: str) -> PermissionScope:
for scope in scopes:
perm_letter, groups_as_str = scope.split(" ")
groups = groups_as_str.split(",")
kwargs[perm_letter] = [g.replace(KNORA_ADMIN_ONTO_NAMESPACE, "knora-admin:") for g in groups]
kwargs[perm_letter] = groups
return PermissionScope.from_dict(kwargs)


Expand All @@ -31,11 +30,10 @@ def create_scope_from_admin_route_object(admin_route_object: list[dict[str, Any]
kwargs: dict[str, list[str]] = {}
for obj in admin_route_object:
attr_name: str = obj["name"]
group: str = obj["additionalInformation"].replace(KNORA_ADMIN_ONTO_NAMESPACE, "knora-admin:")
if attr_name in kwargs:
kwargs[attr_name].append(group)
kwargs[attr_name].append(obj["additionalInformation"])
else:
kwargs[attr_name] = [group]
kwargs[attr_name] = [obj["additionalInformation"]]
return PermissionScope.from_dict(kwargs)


Expand All @@ -50,7 +48,7 @@ def create_admin_route_object_from_scope(perm_scope: PermissionScope) -> list[di
for group in groups:
scope_elements.append(
{
"additionalInformation": group.val.replace("knora-admin:", KNORA_ADMIN_ONTO_NAMESPACE),
"additionalInformation": group.full_iri(),
"name": perm_letter,
"permissionCode": None,
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_scope_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from pytest_unordered import unordered

from dsp_permissions_scripts.models import group
from dsp_permissions_scripts.models.group import KNORA_ADMIN_ONTO_NAMESPACE
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
Expand Down

0 comments on commit 6b0215e

Please sign in to comment.