Skip to content

Commit

Permalink
[Fixes #9919] User endpoint return 404 if user is not in registered m…
Browse files Browse the repository at this point in the history
…embers (#9941)

* - [Fixes #9919] User endpoint return 404 if user is not in registered members

* - fix tests

* - test user with no groups
  • Loading branch information
marthamareal authored Aug 30, 2022
1 parent cc841cb commit 2e2bc67
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
35 changes: 35 additions & 0 deletions geonode/base/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,41 @@ def test_users_list(self):
group_user.delete()
groupx.delete()

def test_get_self_user_details_outside_registered_member(self):
try:
user = get_user_model().objects.create_user(
username='non_registered_member',
email="non_registered_member@geonode.org",
password='password')
# remove user from registered members group
reg_mem_group = Group.objects.get(name='registered-members')
reg_mem_group.user_set.remove(user)

url = reverse('users-detail', kwargs={'pk': user.pk})

self.assertTrue(self.client.login(username="non_registered_member", password="password"))
response = self.client.get(url, format='json')
self.assertEqual(response.status_code, 200)
finally:
user.delete()

def test_get_self_user_details_with_no_group(self):
try:
user = get_user_model().objects.create_user(
username='no_group_member',
email="no_group_member@geonode.org",
password='password')
# remove user from all groups
user.groups.clear()

url = reverse('users-detail', kwargs={'pk': user.pk})

self.assertTrue(self.client.login(username="no_group_member", password="password"))
response = self.client.get(url, format='json')
self.assertEqual(response.status_code, 200)
finally:
user.delete()

def test_register_users(self):
"""
Ensure users are created with default groups.
Expand Down
9 changes: 7 additions & 2 deletions geonode/people/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,16 @@ def get_available_users(user):
if user.is_superuser:
return get_user_model().objects.exclude(Q(username='AnonymousUser') | Q(is_active=False))

member_ids = []
if not user.is_anonymous:
# Append current user profile in the list of users to be returned
member_ids.extend([user.id])

# Only return user that are members of any group profile the current user is member of
member_ids = list(GroupMember.objects.filter(
member_ids.extend(list(GroupMember.objects.filter(
group__in=GroupProfile.objects.filter(
Q(access='public') | Q(group__in=user.groups.all()))
).select_related('user').values_list('user__id', flat=True))
).select_related('user').values_list('user__id', flat=True)))
if Group.objects.filter(name=groups_settings.REGISTERED_MEMBERS_GROUP_NAME).exists():
# Retrieve all members in Registered member's group
rm_group = Group.objects.get(name=groups_settings.REGISTERED_MEMBERS_GROUP_NAME)
Expand Down

0 comments on commit 2e2bc67

Please sign in to comment.