Skip to content

Commit

Permalink
ui: Conditional New Community btn visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Samk13 committed Nov 15, 2023
1 parent 6c9669d commit de5cd55
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
4 changes: 4 additions & 0 deletions invenio_communities/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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."""
15 changes: 12 additions & 3 deletions invenio_communities/views/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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")
Expand Down
36 changes: 36 additions & 0 deletions tests/communities/tests_views.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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

0 comments on commit de5cd55

Please sign in to comment.