From 4621c551249265ab7fa34b1b6c9ea60872c4737f Mon Sep 17 00:00:00 2001 From: ducica Date: Wed, 4 Dec 2024 11:38:59 +0100 Subject: [PATCH] checking permission for new record btn --- .../invenio_communities/details/header.html | 2 +- .../ui/oarepo_communities/components.py | 24 ++++++++++++++++--- setup.cfg | 2 +- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/oarepo_communities/ui/communities_components/templates/oarepo/invenio_communities/details/header.html b/oarepo_communities/ui/communities_components/templates/oarepo/invenio_communities/details/header.html index b5fbda3..c2e1a89 100644 --- a/oarepo_communities/ui/communities_components/templates/oarepo/invenio_communities/details/header.html +++ b/oarepo_communities/ui/communities_components/templates/oarepo/invenio_communities/details/header.html @@ -3,7 +3,7 @@ {%- block subheader_buttons %}
- {%- if not community_use_jinja_header %} + {% if active_community_header_menu_item == "search" and permissions.can_create_record %} diff --git a/oarepo_communities/ui/oarepo_communities/components.py b/oarepo_communities/ui/oarepo_communities/components.py index 53970b4..f343d08 100644 --- a/oarepo_communities/ui/oarepo_communities/components.py +++ b/oarepo_communities/ui/oarepo_communities/components.py @@ -8,13 +8,31 @@ def before_ui_search( self, *, search_options, extra_context, identity, view_args, **kwargs ): community = view_args.get("community") + community_id = str(community.id) + + workflow = community["custom_fields"].get("workflow", "default") + from oarepo_workflows.errors import InvalidWorkflowError + from oarepo_workflows.proxies import current_oarepo_workflows + + if workflow not in current_oarepo_workflows.record_workflows: + raise InvalidWorkflowError( + f"Workflow {workflow} does not exist in the configuration." + ) + + wf = current_oarepo_workflows.record_workflows[workflow] + permissions = wf.permissions( + "create", data={"parent": {"communities": {"default": community_id}}} + ) + can_create_record = permissions.allows(identity) + # for consistency with invenio-communities routes # needed to check if there is something in the curation policy and # about page, so that those tabs would render in the menu request.community = community.to_dict() permissions = community.has_permissions_to(HEADER_PERMISSIONS) + permissions["can_create_record"] = can_create_record extra_context["community"] = community extra_context["permissions"] = permissions - search_options["overrides"][ - "ui_endpoint" - ] = f"/communities/{community.id}/records" + search_options["overrides"]["ui_endpoint"] = ( + f"/communities/{community_id}/records" + ) diff --git a/setup.cfg b/setup.cfg index 1baa07b..c7137b0 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = oarepo-communities -version = 5.1.0 +version = 5.1.2 description = authors = Ronald Krist readme = README.md