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

Fix user_can_* checks handling of resource kwarg #11753

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions arches/app/permissions/arches_default_allow.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,6 @@ def check_resource_instance_permissions(
"""
result = ResourceInstancePermissions()
try:
if resourceid == settings.SYSTEM_SETTINGS_RESOURCE_ID:
if not user.groups.filter(name="System Administrator").exists():
result["permitted"] = False
return result

if not resource:
resource = ResourceInstance.objects.select_related(
"resource_instance_lifecycle_state"
Expand All @@ -274,6 +269,11 @@ def check_resource_instance_permissions(
raise ValueError("resourceid and resource are mutually incompatible")
result["resource"] = resource

if str(resource.pk) == settings.SYSTEM_SETTINGS_RESOURCE_ID:
if not user.groups.filter(name="System Administrator").exists():
result["permitted"] = False
return result

all_perms = self.get_perms(user, resource)

if len(all_perms) == 0: # no permissions assigned. permission implied
Expand Down
2 changes: 1 addition & 1 deletion arches/app/permissions/arches_default_deny.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def check_resource_instance_permissions(
resource = ResourceInstance.objects.get(resourceinstanceid=resourceid)
elif resourceid:
raise ValueError("resourceid and resource are mutually incompatible")
if resourceid == settings.SYSTEM_SETTINGS_RESOURCE_ID:
if str(resource.pk) == settings.SYSTEM_SETTINGS_RESOURCE_ID:
result["resource"] = resource
if not user.groups.filter(name="System Administrator").exists():
result["permitted"] = False
Expand Down
6 changes: 3 additions & 3 deletions arches/app/permissions/arches_permission_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def user_can_read_resource(
if user.is_authenticated:
if user.is_superuser:
return True
if resourceid is not None and resourceid != "":
if resourceid or resource:
result = self.check_resource_instance_permissions(
user, resourceid, "view_resourceinstance", resource=resource
)
Expand Down Expand Up @@ -363,7 +363,7 @@ def user_can_edit_resource(
if user.is_authenticated:
if user.is_superuser:
return True
if resourceid:
if resourceid or resource:
result = self.check_resource_instance_permissions(
user, resourceid, "change_resourceinstance", resource=resource
)
Expand Down Expand Up @@ -399,7 +399,7 @@ def user_can_delete_resource(
if user.is_authenticated:
if user.is_superuser:
return True
if resourceid:
if resourceid or resource:
result = self.check_resource_instance_permissions(
user, resourceid, "delete_resourceinstance", resource=resource
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

from unittest.mock import MagicMock, Mock, patch

from django.test import override_settings
from arches.app.models.resource import Resource
from tests.permissions.base_permissions_framework_test import (
ArchesPermissionFrameworkTestCase,
Expand Down
29 changes: 26 additions & 3 deletions tests/views/api/test_permissions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.conf import settings
from django.contrib.auth.models import Group, User
from django.db import connection
from django.test import TestCase
from django.test.utils import CaptureQueriesContext
Expand All @@ -15,11 +17,16 @@ class InstancePermissionsAPITest(TestCase):
def setUpTestData(cls):
cls.graph = Graph.new(
name="INSTANCE_PERMISSIONS_TEST_GRAPH",
is_resource=True,
is_resource=False, # creates a nodegroup, will undo this below.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I adjusted this test to create a nodegroup on this test graph because the resource permissions checks return False if there are no nodegroups, and the sense of the test is that (with default allow) the anonymous user should pass.

The fact that this was failing on my first push before I adjusted the test setup is a smell related to the bug I was fixing here.

author="ARCHES TEST",
)
cls.graph.isresource = True
cls.graph.resource_instance_lifecycle_id = (
settings.DEFAULT_RESOURCE_INSTANCE_LIFECYCLE_ID
)
cls.graph.save(validate=False)

def test_get(self):
def test_get_with_anonymous_user(self):
resource = ResourceInstance.objects.create(graph=self.graph)
with CaptureQueriesContext(connection) as queries:
response = self.client.get(
Expand All @@ -31,5 +38,21 @@ def test_get(self):
]
self.assertEqual(len(resource_selects), 1, list(queries))
self.assertEqual(
response.content.decode(), '{"delete": false, "edit": false, "read": false}'
response.content.decode(), '{"delete": true, "edit": true, "read": true}'
)

def test_get_with_resource_editor_role(self):
resource = ResourceInstance.objects.create(graph=self.graph)
editor_group = Group.objects.get(name="Resource Editor")
test_user = User.objects.create()
test_user.groups.add(editor_group)
self.client.force_login(test_user)

response = self.client.get(
reverse("api_instance_permissions"),
QUERY_STRING=f"resourceinstanceid={resource.pk}",
)

self.assertEqual(
response.content.decode(), '{"delete": true, "edit": true, "read": true}'
)
Loading