Skip to content

Commit

Permalink
finish code
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Aug 9, 2024
1 parent 9f8c84e commit a4df80c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
26 changes: 26 additions & 0 deletions dsp_permissions_scripts/ap/ap_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
from dsp_permissions_scripts.ap.ap_get import create_admin_route_object_from_ap
from dsp_permissions_scripts.ap.ap_get import create_ap_from_admin_route_object
from dsp_permissions_scripts.ap.ap_model import Ap
from dsp_permissions_scripts.ap.ap_model import ApValue
from dsp_permissions_scripts.models.errors import ApiError
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.project import get_project_iri_and_onto_iris_by_shortcode

logger = get_logger(__name__)

Expand Down Expand Up @@ -36,3 +39,26 @@ def apply_updated_scopes_of_aps_on_server(aps: list[Ap], host: str, dsp_client:
except ApiError as err:
logger.error(err)
logger.info(f"Finished updating scopes of {len(aps)} Administrative Permissions on {host}")


def create_new_ap_on_server(
forGroup: Group,
shortcode: str,
hasPermissions: list[ApValue],
dsp_client: DspClient,
) -> Ap | None:
proj_iri, _ = get_project_iri_and_onto_iris_by_shortcode(shortcode, dsp_client)
payload = {
"forGroup": forGroup.val,
"forProject": proj_iri,
"hasPermissions": [
{"additionalInformation": None, "name": ap_val, "permissionCode": None} for ap_val in hasPermissions
],
}
try:
response = dsp_client.post("/admin/permissions/ap", data=payload)
logger.info(f"Successfully created new AP for group {forGroup}")
return create_ap_from_admin_route_object(response["administrative_permission"])
except ApiError:
logger.error(f"Could not create new AP for group {forGroup}")
return None
10 changes: 6 additions & 4 deletions dsp_permissions_scripts/doap/doap_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,19 @@ def create_new_doap_on_server(
shortcode: str,
scope: PermissionScope,
dsp_client: DspClient,
) -> Doap:
proj_iri, _ = get_project_iri_and_onto_iris_by_shortcode(shortcode)
) -> Doap | None:
proj_iri, _ = get_project_iri_and_onto_iris_by_shortcode(shortcode, dsp_client)
payload = {
"forGroup": target.group,
"forGroup": target.group.val if target.group else None,
"forProject": proj_iri,
"forProperty": target.property,
"forResourceClass": target.resource_class,
"hasPermissions": create_admin_route_object_from_scope(scope),
}
try:
dsp_client.post("/admin/permissions/doap", data=payload)
response = dsp_client.post("/admin/permissions/doap", data=payload)
logger.info(f"Successfully created new DOAP for target {target}")
return create_doap_from_admin_route_response(response["default_object_access_permission"])
except ApiError:
logger.error(f"Could not create new DOAP for target {target}")
return None
1 change: 1 addition & 0 deletions dsp_permissions_scripts/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from dsp_permissions_scripts.ap.ap_model import ApValue
from dsp_permissions_scripts.ap.ap_serialize import serialize_aps_of_project
from dsp_permissions_scripts.ap.ap_set import apply_updated_scopes_of_aps_on_server
from dsp_permissions_scripts.ap.ap_set import create_new_ap_on_server
from dsp_permissions_scripts.doap.doap_get import get_doaps_of_project
from dsp_permissions_scripts.doap.doap_model import Doap
from dsp_permissions_scripts.doap.doap_model import NewDoapTarget
Expand Down

0 comments on commit a4df80c

Please sign in to comment.