From 4aedc77c25f8df4aac37643d4b2799e648ae8963 Mon Sep 17 00:00:00 2001 From: Robin <16273164+robines@users.noreply.github.com> Date: Mon, 23 Sep 2024 17:45:10 +0200 Subject: [PATCH] Remove gang-specific permission fields (#1388) --- ..._remove_gang_event_admin_group_and_more.py | 25 ++++++ backend/samfundet/models/general.py | 11 +-- backend/samfundet/signals.py | 46 +---------- backend/samfundet/tests/test_signals.py | 82 +------------------ 4 files changed, 29 insertions(+), 135 deletions(-) create mode 100644 backend/samfundet/migrations/0003_remove_gang_event_admin_group_and_more.py diff --git a/backend/samfundet/migrations/0003_remove_gang_event_admin_group_and_more.py b/backend/samfundet/migrations/0003_remove_gang_event_admin_group_and_more.py new file mode 100644 index 000000000..2a1e8ef8d --- /dev/null +++ b/backend/samfundet/migrations/0003_remove_gang_event_admin_group_and_more.py @@ -0,0 +1,25 @@ +# Generated by Django 5.1.1 on 2024-09-23 14:24 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('samfundet', '0002_role_usergangrole_usergangsectionrole_userorgrole'), + ] + + operations = [ + migrations.RemoveField( + model_name='gang', + name='event_admin_group', + ), + migrations.RemoveField( + model_name='gang', + name='gang_leader_group', + ), + migrations.RemoveField( + model_name='gang', + name='recruitment_admin_group', + ), + ] diff --git a/backend/samfundet/models/general.py b/backend/samfundet/models/general.py index 92a29a6e0..1a9c25197 100644 --- a/backend/samfundet/models/general.py +++ b/backend/samfundet/models/general.py @@ -16,7 +16,7 @@ from django.utils import timezone from django.core.exceptions import ValidationError from django.utils.translation import gettext as _ -from django.contrib.auth.models import Group, AbstractUser +from django.contrib.auth.models import AbstractUser from root.utils import permissions from root.utils.mixins import CustomBaseModel, FullCleanSaveMixin @@ -315,15 +315,6 @@ class Gang(CustomBaseModel): gang_type = models.ForeignKey(to=GangType, related_name='gangs', verbose_name='Gruppetype', blank=True, null=True, on_delete=models.SET_NULL) info_page = models.ForeignKey(to='samfundet.InformationPage', verbose_name='Infoside', blank=True, null=True, on_delete=models.SET_NULL) - # Gang related permission groups - gang_leader_group = models.OneToOneField(Group, related_name='gang_as_leader', verbose_name='Gangleder', blank=True, null=True, on_delete=models.SET_NULL) - event_admin_group = models.OneToOneField( - Group, related_name='gang_as_event_admin_group', verbose_name='Arrangementgruppe', blank=True, null=True, on_delete=models.SET_NULL - ) - recruitment_admin_group = models.OneToOneField( - Group, related_name='gang_as_recruitment_admin_group', verbose_name='Innganggruppe', blank=True, null=True, on_delete=models.SET_NULL - ) - class Meta: verbose_name = 'Gang' verbose_name_plural = 'Gangs' diff --git a/backend/samfundet/signals.py b/backend/samfundet/signals.py index 23dc22fd1..b52154997 100644 --- a/backend/samfundet/signals.py +++ b/backend/samfundet/signals.py @@ -2,14 +2,10 @@ from typing import Any -from guardian.shortcuts import assign_perm, remove_perm - from django.dispatch import receiver -from django.db.models.signals import pre_save, post_save, m2m_changed - -from samfundet.permissions import SAMFUNDET_CHANGE_EVENT, SAMFUNDET_DELETE_EVENT +from django.db.models.signals import pre_save, post_save -from .models import Gang, User, Event, Profile, UserPreference +from .models import User, Profile, UserPreference from .models.recruitment import Recruitment, RecruitmentStatistics, RecruitmentApplication from .models.model_choices import RecruitmentStatusChoices @@ -28,44 +24,6 @@ def create_profile(sender: User, instance: User, *, created: bool, **kwargs: Any Profile.objects.get_or_create(user=instance) -@receiver(m2m_changed, sender=Event.editors.through) -def update_editor_permissions( # noqa: C901 - sender: User, - instance: Event, - action: str, - model: Gang, - pk_set: set[int], - *, - reverse: bool, - **kwargs: dict, -) -> None: - if action in ['post_add', 'post_remove', 'post_clear']: - current_editors: set[Gang] = set(instance.editors.all()) - - # In the case of a removal or clear, the related objects have already been removed by the time the - # signal handler is called, so we can calculate the set of removed objects by subtracting the current - # set of related objects from the set of all related object primary keys. - if action in ['post_remove', 'post_clear']: - removed_gangs: set[Gang] = set(model.objects.filter(pk__in=pk_set)) - current_editors - for gang in removed_gangs: - if gang.event_admin_group: - remove_perm(perm=SAMFUNDET_CHANGE_EVENT, user_or_group=gang.event_admin_group, obj=instance) - remove_perm(perm=SAMFUNDET_DELETE_EVENT, user_or_group=gang.event_admin_group, obj=instance) - if gang.gang_leader_group: - remove_perm(perm=SAMFUNDET_CHANGE_EVENT, user_or_group=gang.gang_leader_group, obj=instance) - remove_perm(perm=SAMFUNDET_DELETE_EVENT, user_or_group=gang.gang_leader_group, obj=instance) - - # In the case of an add, the related objects have already been added by the time the signal handler is called. - if action == 'post_add': - for gang in current_editors: - if gang.event_admin_group: - assign_perm(perm=SAMFUNDET_CHANGE_EVENT, user_or_group=gang.event_admin_group, obj=instance) - assign_perm(perm=SAMFUNDET_DELETE_EVENT, user_or_group=gang.event_admin_group, obj=instance) - if gang.gang_leader_group: - assign_perm(perm=SAMFUNDET_CHANGE_EVENT, user_or_group=gang.gang_leader_group, obj=instance) - assign_perm(perm=SAMFUNDET_DELETE_EVENT, user_or_group=gang.gang_leader_group, obj=instance) - - @receiver(post_save, sender=Recruitment) def create_recruitment_statistics(sender: Recruitment, instance: Recruitment, *, created: bool, **kwargs: Any) -> None: """Ensures stats are created when an recruitment is created""" diff --git a/backend/samfundet/tests/test_signals.py b/backend/samfundet/tests/test_signals.py index c3cacf56b..6a4de9dcc 100644 --- a/backend/samfundet/tests/test_signals.py +++ b/backend/samfundet/tests/test_signals.py @@ -1,10 +1,6 @@ from __future__ import annotations -from guardian.shortcuts import get_perms - -from django.contrib.auth.models import Group - -from samfundet.models import Gang, User, Event, Profile, UserPreference +from samfundet.models import User, Profile, UserPreference class TestUserSignals: @@ -41,79 +37,3 @@ def test_create_profile(self): ### Cleanup ### user.delete() - - -class TestEditorPermissions: - def test_update_editor_permissions_add( - self, - fixture_event: Event, - fixture_group: Group, - fixture_gang: Gang, - ): - ### Arrange ### - - ### Act ### - fixture_gang.event_admin_group = fixture_group - fixture_gang.save() - fixture_event.editors.add(fixture_gang) - fixture_event.save() - editor_perms = get_perms(fixture_gang.event_admin_group, fixture_event) - - ### Assert ### - assert 'change_event' in editor_perms - assert 'delete_event' in editor_perms - - def test_update_editor_permissions_remove( - self, - fixture_event: Event, - fixture_group: Group, - fixture_gang: Gang, - ): - ### Arrange ### - - ### Act ### - fixture_gang.event_admin_group = fixture_group - fixture_gang.save() - fixture_event.editors.remove(fixture_gang) - editor_perms = get_perms(fixture_gang.event_admin_group, fixture_event) - - ### Assert ### - assert 'change_event' not in editor_perms - assert 'delete_event' not in editor_perms - - def test_group_leader_permissions_add( - self, - fixture_event: Event, - fixture_group: Group, - fixture_gang: Gang, - ): - ### Arrange ### - - ### Act ### - fixture_gang.gang_leader_group = fixture_group - fixture_gang.save() - fixture_event.editors.add(fixture_gang) - fixture_event.save() - editor_perms = get_perms(fixture_gang.gang_leader_group, fixture_event) - - ### Assert ### - assert 'change_event' in editor_perms - assert 'delete_event' in editor_perms - - def test_group_leader_permissions_remove( - self, - fixture_event: Event, - fixture_group: Group, - fixture_gang: Gang, - ): - ### Arrange ### - - ### Act ### - fixture_gang.gang_leader_group = fixture_group - fixture_gang.save() - fixture_event.editors.remove(fixture_gang) - editor_perms = get_perms(fixture_gang.gang_leader_group, fixture_event) - - ### Assert ### - assert 'change_event' not in editor_perms - assert 'delete_event' not in editor_perms