Self hosted FLOSS fitness/workout, nutrition and weight tracker
Refuse (de)activation of accounts above the actor
Roland Geidereb9350cee06f
diff --git a/wger/core/views/user.py b/wger/core/views/user.pyindex 987fd212e..333a6a64f 100644--- a/wger/core/views/user.py+++ b/wger/core/views/user.py@@ -90,6 +90,7 @@ from wger.gym.helpers import (holds_gym_permission,holds_permissions_beyond,is_same_gym,+ may_change_activation,outranks,)from wger.gym.models import (@@ -398,19 +399,8 @@ class UserDeactivateView() and not is_same_gym(request.user, edit_user):return HttpResponseForbidden()- # A user with only the trainer permission must not be able to (de)activate- # other gym staff (managers, fellow trainers, general managers). Group- # membership is checked directly so the rule still applies when the- # target account is currently deactivated (``has_perm`` returns ``False``- # for inactive users).- if (- request.user.has_perm('gym.gym_trainer')- and not request.user.has_perm('gym.manage_gym')- and not request.user.has_perm('gym.manage_gyms')- and edit_user.groups.filter(- name__in=('gym_trainer', 'gym_manager', 'general_gym_manager')- ).exists()- ):+ # Gym staff must not lock out the accounts above them+ if not may_change_activation(request.user, edit_user):return HttpResponseForbidden()return super(UserDeactivateView, self).dispatch(request, *args, **kwargs)@@ -446,19 +436,8 @@ class UserActivateView() and not is_same_gym(request.user, edit_user):return HttpResponseForbidden()- # A user with only the trainer permission must not be able to- # (de)activate other gym staff (managers, fellow trainers,- # general managers). Group membership is checked directly so the- # rule still applies when the target account is currently- # deactivated (``has_perm`` returns ``False`` for inactive users).- if (- request.user.has_perm('gym.gym_trainer')- and not request.user.has_perm('gym.manage_gym')- and not request.user.has_perm('gym.manage_gyms')- and edit_user.groups.filter(- name__in=('gym_trainer', 'gym_manager', 'general_gym_manager')- ).exists()- ):+ # Gym staff must not reactivate the accounts above them+ if not may_change_activation(request.user, edit_user):return HttpResponseForbidden()return super(UserActivateView, self).dispatch(request, *args, **kwargs)diff --git a/wger/gym/helpers.py b/wger/gym/helpers.pyindex 916e191ed..14e7ac321 100644--- a/wger/gym/helpers.py+++ b/wger/gym/helpers.py@@ -152,6 +152,23 @@ def outranks(actor, target) -> bool:) and not holds_permissions_beyond(target, actor)+def may_change_activation(actor, target) -> bool:+ """+ Whether the actor may deactivate or reactivate the target's account++ - superusers and general managers can (de)activate every account+ - managers can't (de)activate general managers or superusers+ - trainers only (de)activate accounts without any gym role+ """+ if actor.has_perm('gym.manage_gyms'):+ return True++ if actor.has_perm('gym.manage_gym'):+ return not holds_gym_permission(target, 'manage_gyms')++ return not holds_gym_permission(target, 'manage_gyms', 'manage_gym', 'gym_trainer')++def get_permission_list(user):"""Calculate available user permissionsdiff --git a/wger/gym/tests/test_user.py b/wger/gym/tests/test_user.pyindex 3a7640938..5b6966da6 100644--- a/wger/gym/tests/test_user.py+++ b/wger/gym/tests/test_user.py@@ -458,6 +458,74 @@ class DeleteUserTargetRoleTestCase(WgerTestCase):self.assertEqual(self.delete('manager1', 14), 302)+class ActivationTargetRoleTestCase(WgerTestCase):+ """+ Gym staff can't (de)activate accounts above them+ """++ def set_active(self, username: str, target_pk: int, active: bool) -> int:+ """+ (De)activates the target as the given user and returns the status code++ Also checks that the flag only changed when the request succeeded.+ """+ User.objects.filter(pk=target_pk).update(is_active=not active)+ self.user_login(username)++ url_name = 'core:user:activate' if active else 'core:user:deactivate'+ response = self.client.post(reverse(url_name, kwargs={'pk': target_pk}))++ changed = User.objects.get(pk=target_pk).is_active == active+ self.assertEqual(changed, response.status_code == 302)+ return response.status_code++ def test_manager_cannot_deactivate_admin(self):+ self.assertEqual(self.set_active('manager1', 1, False), 403)++ def test_manager_cannot_deactivate_general_manager(self):+ self.assertEqual(self.set_active('manager1', 12, False), 403)++ def test_manager_cannot_deactivate_superuser(self):+ User.objects.filter(pk=21).update(is_superuser=True)+ self.assertEqual(self.set_active('manager1', 21, False), 403)++ def test_manager_cannot_activate_general_manager(self):+ self.assertEqual(self.set_active('manager1', 12, True), 403)++ def test_manager_can_deactivate_fellow_manager(self):+ self.assertEqual(self.set_active('manager1', 10, False), 302)++ def test_manager_can_deactivate_trainer(self):+ self.assertEqual(self.set_active('manager1', 4, False), 302)++ def test_trainer_cannot_deactivate_admin(self):+ self.assertEqual(self.set_active('trainer1', 1, False), 403)++ def test_trainer_cannot_deactivate_superuser(self):+ User.objects.filter(pk=21).update(is_superuser=True)+ self.assertEqual(self.set_active('trainer1', 21, False), 403)++ def test_trainer_cannot_activate_superuser(self):+ User.objects.filter(pk=21).update(is_superuser=True)+ self.assertEqual(self.set_active('trainer1', 21, True), 403)++ def test_trainer_cannot_deactivate_direct_manager(self):+ content_type = ContentType.objects.get_for_model(Gym)+ permission = Permission.objects.get(content_type=content_type, codename='manage_gym')+ User.objects.get(pk=14).user_permissions.add(permission)+ self.assertEqual(self.set_active('trainer1', 14, False), 403)++ def test_trainer_can_deactivate_member(self):+ self.assertEqual(self.set_active('trainer1', 14, False), 302)++ def test_general_manager_can_deactivate_manager(self):+ self.assertEqual(self.set_active('general_manager1', 9, False), 302)++ def test_superuser_can_deactivate_general_manager(self):+ User.objects.filter(pk=1).update(is_superuser=True)+ self.assertEqual(self.set_active('admin', 12, False), 302)++class EditUserPermissionTargetRoleTestCase(WgerTestCase):"""The gym roles of an account can only be edited when the user could grant all of them