Skip to content

Commit

Permalink
Remove gang-specific permission fields
Browse files Browse the repository at this point in the history
  • Loading branch information
robines committed Sep 23, 2024
1 parent b6b7477 commit 4194c1b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 135 deletions.
Original file line number Diff line number Diff line change
@@ -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',
),
]
11 changes: 1 addition & 10 deletions backend/samfundet/models/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down
46 changes: 2 additions & 44 deletions backend/samfundet/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"""
Expand Down
82 changes: 1 addition & 81 deletions backend/samfundet/tests/test_signals.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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

0 comments on commit 4194c1b

Please sign in to comment.