Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated ENABLE_RESTRICTED_APP_ACCESS for potential list value #1107

Merged
merged 4 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions tests/unit_tests/test_tethys_apps/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,16 +858,36 @@ def test_user_can_access_app(self, mock_settings):
result1 = utilities.user_can_access_app(user, app)
self.assertFalse(result1)

# test restricted access with restricted apps list
mock_settings.ENABLE_RESTRICTED_APP_ACCESS = [app.package]
result2 = utilities.user_can_access_app(user, app)
self.assertFalse(result2)

# test restricted access with no restricted apps list
mock_settings.ENABLE_RESTRICTED_APP_ACCESS = []
result3 = utilities.user_can_access_app(user, app)
self.assertTrue(result3)

# test restricted access with restricted apps list of a different app
mock_settings.ENABLE_RESTRICTED_APP_ACCESS = ["some_other_app"]
result4 = utilities.user_can_access_app(user, app)
self.assertTrue(result4)

# test with permission
assign_perm(f"{app.package}:access_app", user, app)

result2 = utilities.user_can_access_app(user, app)
self.assertTrue(result2)
result5 = utilities.user_can_access_app(user, app)
self.assertTrue(result5)

# test open portal mode case
mock_settings.ENABLE_OPEN_PORTAL = True
result3 = utilities.user_can_access_app(user, app)
self.assertTrue(result3)
result6 = utilities.user_can_access_app(user, app)
self.assertTrue(result6)

# test restricted access with restricted apps list
mock_settings.ENABLE_RESTRICTED_APP_ACCESS = [app.package]
result7 = utilities.user_can_access_app(user, app)
self.assertTrue(result7)

def test_get_installed_tethys_items_apps(self):
# Get list of apps installed in the tethysapp directory
Expand Down
11 changes: 9 additions & 2 deletions tethys_apps/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,10 +586,17 @@ def get_service_model_from_type(service_type):
def user_can_access_app(user, app):
from django.conf import settings

RESTRICTED_APP_ACCESS = getattr(settings, "ENABLE_RESTRICTED_APP_ACCESS", False)

if getattr(settings, "ENABLE_OPEN_PORTAL", False):
return True
elif getattr(settings, "ENABLE_RESTRICTED_APP_ACCESS", False):
return user.has_perm(f"{app.package}:access_app", app)
elif RESTRICTED_APP_ACCESS:
if isinstance(RESTRICTED_APP_ACCESS, bool):
return user.has_perm(f"{app.package}:access_app", app)
elif app.package in RESTRICTED_APP_ACCESS:
return user.has_perm(f"{app.package}:access_app", app)
else:
return True
else:
return True

Expand Down
3 changes: 2 additions & 1 deletion tethys_portal/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@
# Set to True to allow Open Portal mode. This mode supersedes any specific user/group app access permissions
ENABLE_OPEN_PORTAL = TETHYS_PORTAL_CONFIG.pop("ENABLE_OPEN_PORTAL", False)

# Set to True to allow Open Portal mode. This mode supersedes any specific user/group app access permissions
# Set to True to allow restricted app access. This mode removes access to apps for nonadmins unless given explicit permission.
# A list can also be provided to restrict specific applications unless users are given explicit permission
ENABLE_RESTRICTED_APP_ACCESS = TETHYS_PORTAL_CONFIG.pop(
"ENABLE_RESTRICTED_APP_ACCESS", False
)
Expand Down