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

Resolve issue with UserInfo crashing on permissions. #565

Merged
merged 1 commit into from
Nov 25, 2024
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
23 changes: 22 additions & 1 deletion mreg/api/v1/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from rest_framework.test import APIClient, APITestCase

from mreg.models.base import ExpiringToken
from mreg.models.network import Network
from mreg.models.network import Network, NetGroupRegexPermission
from mreg.models.host import Host, Ipaddress, PtrOverride
from mreg.models.zone import ForwardZone, ReverseZone
from mreg.models.resource_records import Txt
Expand Down Expand Up @@ -316,6 +316,27 @@ def test_meta_user_info_admin_other_target_200_ok(self):
response = self.assert_get("/api/meta/user?username=superuser")
self.assertTrue('username' in response.data)

def test_meta_user_info_admin_other_target_with_permissions_200_ok(self):
groupname = 'permission_group_testing'
username = 'permission_user_testing'
user, _ = get_user_model().objects.get_or_create(username=username, password='test')
group, _ = Group.objects.get_or_create(name=groupname)
user.groups.add(group)
permission, _ = NetGroupRegexPermission.objects.get_or_create(
group=group, range="10.0.0.0/24", regex='.*\\.example\\.org')

response = self.assert_get(f"/api/meta/user?username={username}")
self.assertTrue('username' in response.data)
self.assertTrue('permissions' in response.data)
self.assertEqual(len(response.data['permissions']), 1)
self.assertEqual(response.data['permissions'][0]['group'], groupname)
self.assertEqual(response.data['permissions'][0]['range'], "10.0.0.0/24")
self.assertEqual(response.data['permissions'][0]['regex'], '.*\\.example\\.org')

permission.delete()
group.delete()
user.delete()

def test_meta_user_info_user_other_target_403_forbidden(self):
self.client = self.get_token_client(superuser=False)
self.assert_get_and_403("/api/meta/user?username=superuser")
Expand Down
4 changes: 2 additions & 2 deletions mreg/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def get(self, request: Request):

if username and username != user.username:
# Only allow access to other user data if the requester is an mreg superuser
if not req_is_mreg_superuser or req_is_mreg_admin or req_is_mreg_group_admin or req_is_mreg_network_admin:
if not (req_is_mreg_superuser or req_is_mreg_admin or req_is_mreg_group_admin or req_is_mreg_network_admin):
raise PermissionDenied("You do not have permission to view other users' details.")
try:
target_user = User.objects.get(username=username)
Expand Down Expand Up @@ -159,7 +159,7 @@ def get(self, request: Request):
"permissions": [
{
"group": permission.group,
"range": permission.range,
"range": str(permission.range),
"regex": permission.regex,
"labels": [label.name for label in permission.labels.all()],
}
Expand Down