From 407e33bbfc679e52a7896ef27bc7325c7ce49a6d Mon Sep 17 00:00:00 2001 From: Johannes Nussbaum Date: Wed, 4 Oct 2023 17:46:01 +0200 Subject: [PATCH] edit --- dsp_permissions_scripts/template.py | 25 +++++---- dsp_permissions_scripts/utils/doap_get.py | 4 +- dsp_permissions_scripts/utils/doap_set.py | 68 +++-------------------- 3 files changed, 24 insertions(+), 73 deletions(-) diff --git a/dsp_permissions_scripts/template.py b/dsp_permissions_scripts/template.py index 99c32177..5a86aa35 100644 --- a/dsp_permissions_scripts/template.py +++ b/dsp_permissions_scripts/template.py @@ -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 @@ -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. @@ -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( diff --git a/dsp_permissions_scripts/utils/doap_get.py b/dsp_permissions_scripts/utils/doap_get.py index 6f83ee93..1def6fad 100644 --- a/dsp_permissions_scripts/utils/doap_get.py +++ b/dsp_permissions_scripts/utils/doap_get.py @@ -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, @@ -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, diff --git a/dsp_permissions_scripts/utils/doap_set.py b/dsp_permissions_scripts/utils/doap_set.py index 2833d5b7..bbaa4a94 100644 --- a/dsp_permissions_scripts/utils/doap_set.py +++ b/dsp_permissions_scripts/utils/doap_set.py @@ -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, ) @@ -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, )