Skip to content

Commit

Permalink
[Fixes #11995] Implement the DELETE method for the User API refactor …
Browse files Browse the repository at this point in the history
…and docstrings added
  • Loading branch information
RegisSinjari committed Mar 8, 2024
1 parent 7cb4fb6 commit 7fa4765
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
10 changes: 3 additions & 7 deletions geonode/people/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from mock import MagicMock, PropertyMock, patch
from geonode.base.models import ResourceBase
from geonode.groups.models import GroupMember, GroupProfile
from geonode.people.utils import call_validators
from geonode.tests.base import GeoNodeBaseTestSupport

from django.core import mail
Expand Down Expand Up @@ -817,9 +816,8 @@ def test_valid_delete(self):
self.assertEqual(get_user_model().objects.filter(username="tim").first(), None)

@override_settings(USER_DELETION_RULES=[])
@patch("geonode.people.utils.user_deletion_modules", [])
def test_delete_without_validators(self):
# reset global
call_validators(None, reset=True)

norman = get_user_model().objects.get(username="norman")
admin = get_user_model().objects.get(username="admin")
Expand All @@ -839,10 +837,9 @@ def test_delete_without_validators(self):
# Ensure norman is now a member
self.assertTrue(self.bar.user_is_member(norman))

# promotion
# p romote norman to a manager
self.bar.promote(norman)
# Ensure norman is in the managers queryset
# self.bar.join(norman, role=GroupMember.MANAGER)
self.assertTrue(norman in self.bar.get_managers())

url = f"{reverse('users-list')}/{norman.pk}"
Expand Down Expand Up @@ -872,10 +869,9 @@ def test_delete_a_manger(self):
# Ensure norman is now a member
self.assertTrue(self.bar.user_is_member(norman))

# promotion
# promote norman to a manager
self.bar.promote(norman)
# Ensure norman is in the managers queryset
# self.bar.join(norman, role=GroupMember.MANAGER)
self.assertTrue(norman in self.bar.get_managers())

url = f"{reverse('users-list')}/{norman.pk}"
Expand Down
40 changes: 35 additions & 5 deletions geonode/people/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
from django.conf import settings
from django.utils.module_loading import import_string

# from geonode.people.models import Profile


def get_default_user():
"""Create a default user"""
Expand Down Expand Up @@ -147,19 +149,47 @@ def get_available_users(user):


def has_resources(profile) -> bool:
"""
checks if user has any resource in ownership
Args:
profile (Profile) : accepts a userprofile instance.
Returns:
bool: profile is the owner of any resources
"""
return ResourceBase.objects.filter(owner_id=profile.pk).exists()


def is_manager(profile) -> bool:
"""
Checks if user is the manager of any group
Args:
profile (Profile) : accepts a userprofile instance.
Returns:
bool: profile is mangager or not
"""
return GroupMember.objects.filter(user_id=profile.pk, role=GroupMember.MANAGER).exists()


def call_validators(profile, reset=False):
if reset:
globals()["user_deletion_modules"] = []
def call_user_deletion_rules(profile) -> None:
"""
calls a set of defined rules specific to the deletion of a user
which are read from settings.USER_DELETION_RULES
new rules can be added as long as they take as parameter the userprofile
and return a boolean
Args:
profile (Profile) : accepts a userprofile instance.
Returns:
None : Raises PermissionDenied Exception in case any of the deletion rule Fail
"""
if not globals().get("user_deletion_modules"):
storer_module_path = settings.USER_DELETION_RULES if hasattr(settings, "USER_DELETION_RULES") else []
globals()["user_deletion_modules"] = [import_string(storer_path) for storer_path in storer_module_path]
rule_path = settings.USER_DELETION_RULES if hasattr(settings, "USER_DELETION_RULES") else []
globals()["user_deletion_modules"] = [import_string(deletion_rule) for deletion_rule in rule_path]
for not_valid in globals().get("user_deletion_modules", []):
if not_valid(profile):
raise PermissionDenied()
4 changes: 2 additions & 2 deletions geonode/people/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
from geonode.security.utils import get_visible_resources
from guardian.shortcuts import get_objects_for_user
from rest_framework.exceptions import PermissionDenied
from geonode.people.utils import call_validators
from geonode.people.utils import call_user_deletion_rules


class SetUserLayerPermission(View):
Expand Down Expand Up @@ -209,7 +209,7 @@ def perform_destroy(self, instance):
# self delete check
if self.request.user.pk == int(self.kwargs["pk"]):
raise PermissionDenied()
call_validators(instance)
call_user_deletion_rules(instance)
instance.delete()

@extend_schema(
Expand Down

0 comments on commit 7fa4765

Please sign in to comment.