Skip to content

Commit

Permalink
edit
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Oct 4, 2023
1 parent a0c16e3 commit 407e33b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 73 deletions.
25 changes: 14 additions & 11 deletions dsp_permissions_scripts/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

from dsp_permissions_scripts.models.groups import BuiltinGroup
from dsp_permissions_scripts.models.host import Hosts
from dsp_permissions_scripts.models.permission import Oap
from dsp_permissions_scripts.models.permission import Doap, Oap
from dsp_permissions_scripts.models.scope import PUBLIC
from dsp_permissions_scripts.utils.authentication import login
from dsp_permissions_scripts.utils.doap_get import (
get_doaps_of_project,
print_doaps_of_project,
)
from dsp_permissions_scripts.utils.doap_set import set_doaps_of_groups
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.project import get_all_resource_oaps_of_project

Expand All @@ -20,6 +20,13 @@ def modify_oaps(oaps: list[Oap]) -> list[Oap]:
return oaps


def modify_doaps(doaps: list[Doap]) -> list[Doap]:
for doap in doaps:
if doap.target.group in [BuiltinGroup.PROJECT_MEMBER.value, BuiltinGroup.PROJECT_ADMIN.value]:
doap.scope = PUBLIC
return doaps


def main() -> None:
"""
The main method assembles a sample call of all available high-level functions.
Expand All @@ -29,24 +36,20 @@ def main() -> None:
shortcode = "F18E"
token = login(host)

new_scope = PUBLIC
groups = [BuiltinGroup.PROJECT_ADMIN, BuiltinGroup.PROJECT_MEMBER]

doaps = get_doaps_of_project(
project_doaps = get_doaps_of_project(
host=host,
shortcode=shortcode,
token=token,
)
print_doaps_of_project(
doaps=doaps,
doaps=project_doaps,
host=host,
shortcode=shortcode,
)
set_doaps_of_groups(
scope=new_scope,
groups=groups,
project_doaps_updated = modify_doaps(doaps=project_doaps)
apply_updated_doaps_on_server(
doaps=project_doaps_updated,
host=host,
shortcode=shortcode,
token=token,
)
resource_oaps = get_all_resource_oaps_of_project(
Expand Down
4 changes: 2 additions & 2 deletions dsp_permissions_scripts/utils/doap_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def get_permissions_for_project(
return permissions


def get_all_doaps_of_project(
def __get_all_doaps_of_project(
project_iri: str,
host: str,
token: str,
Expand Down Expand Up @@ -102,7 +102,7 @@ def get_doaps_of_project(
shortcode=shortcode,
host=host,
)
doaps = get_all_doaps_of_project(
doaps = __get_all_doaps_of_project(
project_iri=project_iri,
host=host,
token=token,
Expand Down
68 changes: 8 additions & 60 deletions dsp_permissions_scripts/utils/doap_set.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
from typing import Sequence
from urllib.parse import quote_plus

import requests

from dsp_permissions_scripts.models.groups import BuiltinGroup
from dsp_permissions_scripts.models.permission import Doap
from dsp_permissions_scripts.models.scope import PermissionScope
from dsp_permissions_scripts.utils.authentication import get_protocol
from dsp_permissions_scripts.utils.doap_get import (
create_doap_from_admin_route_response,
get_all_doaps_of_project,
)
from dsp_permissions_scripts.utils.project import get_project_iri_by_shortcode
from dsp_permissions_scripts.utils.doap_get import create_doap_from_admin_route_response
from dsp_permissions_scripts.utils.scope_serialization import (
create_admin_route_object_from_scope,
)
Expand All @@ -37,73 +31,27 @@ def __update_doap_scope(
return new_doap


def __get_doaps_of_groups(
groups: Sequence[str | BuiltinGroup],
host: str,
shortcode: str,
token: str,
) -> list[Doap]:
"""
Retrieves the DOAPs for the given groups.
Args:
groups: the group IRIs to whose DOAP the scope should be applied
host: the DSP server where the project is located
shortcode: the shortcode of the project
token: the access token
Returns:
applicable_doaps: the applicable DOAPs
"""
project_iri = get_project_iri_by_shortcode(
shortcode=shortcode,
host=host,
)
all_doaps = get_all_doaps_of_project(
project_iri=project_iri,
host=host,
token=token,
)
groups_str = []
for g in groups:
groups_str.append(g.value if isinstance(g, BuiltinGroup) else g)
applicable_doaps = [d for d in all_doaps if d.target.group in groups_str]
assert len(applicable_doaps) == len(groups)
return applicable_doaps



def set_doaps_of_groups(
scope: PermissionScope,
groups: Sequence[str | BuiltinGroup],
def apply_updated_doaps_on_server(
doaps: list[Doap],
host: str,
shortcode: str,
token: str,
) -> None:
"""
Applies the given scope to the DOAPs of the given groups.
Updates DOAPs on the server.
Args:
scope: one of the standard scopes defined in the Scope class
groups: the group IRIs to whose DOAP the scope should be applied
doaps: the DOAPs to be sent to the server
host: the DSP server where the project is located
shortcode: the shortcode of the project
token: the access token
"""
applicable_doaps = __get_doaps_of_groups(
groups=groups,
host=host,
shortcode=shortcode,
token=token,
)
heading = f"Update {len(applicable_doaps)} DOAPs on {host}..."
heading = f"Update {len(doaps)} DOAPs on {host}..."
print(f"\n{heading}\n{'=' * len(heading)}\n")
for d in applicable_doaps:
for d in doaps:
print("Old DOAP:\n=========")
print(d.model_dump_json(indent=2))
new_doap = __update_doap_scope(
doap_iri=d.doap_iri,
scope=scope,
scope=d.scope,
host=host,
token=token,
)
Expand Down

0 comments on commit 407e33b

Please sign in to comment.