Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: streamline PermissionScope serialization #35

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions dsp_permissions_scripts/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@
)
from dsp_permissions_scripts.utils.doap_set import apply_updated_doaps_on_server
from dsp_permissions_scripts.utils.oap import apply_updated_oaps_on_server
from dsp_permissions_scripts.utils.oap_serialize import (
deserialize_resource_oaps,
serialize_resource_oaps,
)
from dsp_permissions_scripts.utils.oap_serialize import serialize_resource_oaps
from dsp_permissions_scripts.utils.project import get_all_resource_oaps_of_project


def modify_oaps(oaps: list[Oap]) -> list[Oap]:
"""Adapt this sample to your needs."""
for oap in oaps:
oap.scope = oap.scope.add("CR", BuiltinGroup.SYSTEM_ADMIN)
if BuiltinGroup.SYSTEM_ADMIN.value not in oap.scope.CR:
oap.scope = oap.scope.add("CR", BuiltinGroup.SYSTEM_ADMIN)
return oaps


Expand Down Expand Up @@ -52,11 +50,7 @@ def update_oaps(
)
resource_oaps_updated = modify_oaps(oaps=resource_oaps)
serialize_resource_oaps(
resource_oaps=resource_oaps,
shortcode=shortcode,
mode="modified",
)
resource_oaps_updated = deserialize_resource_oaps(
resource_oaps=resource_oaps_updated,
shortcode=shortcode,
mode="modified",
)
Expand Down
26 changes: 11 additions & 15 deletions dsp_permissions_scripts/utils/scope_serialization.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
from typing import Any

from dsp_permissions_scripts.models.groups import BuiltinGroup
from dsp_permissions_scripts.models.scope import PermissionScope
from dsp_permissions_scripts.utils.helpers import sort_groups


def create_string_from_scope(perm_scope: PermissionScope) -> str:
"""Serializes a permission scope to a permissions string as used by /v2 routes."""
as_dict = {}
for perm_letter, groups in perm_scope.model_dump().items():
for perm_letter, groups in perm_scope.model_dump(mode="json").items():
if groups:
groups_as_str = [g.value if isinstance(g, BuiltinGroup) else g for g in groups]
as_dict[perm_letter] = sort_groups(groups_as_str)
as_dict[perm_letter] = sort_groups(groups)
strs = [f"{k} {','.join(l)}" for k, l in as_dict.items()]
return "|".join(strs)

Expand Down Expand Up @@ -43,15 +41,13 @@ 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 a shape that can be used for requests to /admin/permissions routes."""
scope_elements: list[dict[str, str | None]] = []
for perm_letter, groups in perm_scope.model_dump().items():
if groups:
groups_as_str = [g.value if isinstance(g, BuiltinGroup) else g for g in groups]
for group in groups_as_str:
scope_elements.append(
{
"additionalInformation": group,
"name": perm_letter,
"permissionCode": None,
}
)
for perm_letter, groups in perm_scope.model_dump(mode="json").items():
for group in groups:
scope_elements.append(
{
"additionalInformation": group,
"name": perm_letter,
"permissionCode": None,
}
)
return scope_elements