From de5cd55bb991edec10db80fef522342f846cb4f5 Mon Sep 17 00:00:00 2001 From: Sam AL-Arbid Date: Wed, 15 Nov 2023 01:51:26 +0100 Subject: [PATCH] ui: Conditional New Community btn visibility --- invenio_communities/config.py | 4 ++++ invenio_communities/views/ui.py | 15 ++++++++++--- tests/communities/tests_views.py | 36 ++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/invenio_communities/config.py b/invenio_communities/config.py index 25267d2be..580a0432f 100644 --- a/invenio_communities/config.py +++ b/invenio_communities/config.py @@ -3,6 +3,7 @@ # This file is part of Invenio. # Copyright (C) 2016-2022 CERN. # Copyright (C) 2023 Graz University of Technology. +# Copyright (C) 2023 KTH Royal Institute of Technology. # # Invenio is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. @@ -312,3 +313,6 @@ ) COMMUNITIES_OAI_SETS_PREFIX = "community-" + +COMMUNITIES_CREATE_PERM_CHECK = False +"""Controls visibility of 'New Community' btn based on user's permission when set to True.""" diff --git a/invenio_communities/views/ui.py b/invenio_communities/views/ui.py index 049c43e4a..7dce1da62 100644 --- a/invenio_communities/views/ui.py +++ b/invenio_communities/views/ui.py @@ -3,6 +3,7 @@ # This file is part of Invenio. # Copyright (C) 2016-2021 CERN. # Copyright (C) 2023 Graz University of Technology. +# Copyright (C) 2023 KTH Royal Institute of Technology. # # Invenio is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. @@ -82,9 +83,16 @@ def record_permission_denied_error(error): def _can_create_community(): - """Function used to check if a user has permissions to create a community.""" - can_create = current_communities.service.check_permission(g.identity, "create") - return can_create + """ + Determine if the 'New community' button should be visible. + + If the 'COMMUNITIES_CREATE_PERM_CHECK' config is set and True, + check the user's permission to create a community. If the config is + not set or false, the button is always visible. + """ + if current_app.config.get("COMMUNITIES_CREATE_PERM_CHECK", False): + return current_communities.service.check_permission(g.identity, "create") + return True def _has_about_page_content(): @@ -192,6 +200,7 @@ def register_menus(): "invenio_communities.communities_new", _("New community"), order=3, + visible_when=_can_create_community, ) communities = current_menu.submenu("communities") diff --git a/tests/communities/tests_views.py b/tests/communities/tests_views.py index f495e0b72..63a51096f 100644 --- a/tests/communities/tests_views.py +++ b/tests/communities/tests_views.py @@ -1,13 +1,19 @@ # -*- coding: utf-8 -*- # # Copyright (C) 2022 CERN. +# Copyright (C) 2023 KTH Royal Institute of Technology. # # Invenio-Communities is free software; you can redistribute it and/or modify # it under the terms of the MIT License; see LICENSE file for more details. """Test community views.""" +from flask import g +from invenio_records_permissions.generators import SystemProcess + +from invenio_communities.permissions import CommunityPermissionPolicy from invenio_communities.views.communities import _filter_roles +from invenio_communities.views.ui import _can_create_community def _test_filter_roles(app, members, action, member_types, community_id): @@ -60,3 +66,33 @@ def test_filter_roles_when_adding_groups( {"group"}, community.id, ) + + +def test_can_create_community(app, users, superuser_identity): + """Test the _can_create_community function under different config settings.""" + test_users = ["reader", "curator", "manager", "owner"] + PERM_CHECK_CONFIG = "COMMUNITIES_CREATE_PERM_CHECK" + + # Test default config allows community creation + assert app.config.get(PERM_CHECK_CONFIG) == False + assert _can_create_community() == True + + # Test with config set to True + app.config[PERM_CHECK_CONFIG] = True + assert app.config.get(PERM_CHECK_CONFIG) == True + assert _can_create_community() == True + + # Test with different user identities + for user in test_users: + g.identity = users[user].identity + assert _can_create_community() == True + + # Test with community creation disabled + CommunityPermissionPolicy.can_create = [SystemProcess()] + for user in test_users: + g.identity = users[user].identity + assert _can_create_community() == False + + # Test superuser + g.identity = superuser_identity + assert _can_create_community() == True