Fix permissions setup of associations.

- Permissions of 'gestion' app are correctly added to the staff groups
of associations.
- Add tests to ensure staff groups of COF and BDS are correctly setup.
- Shortcut functions are added to retrieve COF and BDS association.
This commit is contained in:
Aurélien Delobelle 2017-09-05 15:02:33 +02:00
parent 3842b5d160
commit 29d288c567
7 changed files with 152 additions and 65 deletions

View file

@ -1,36 +1,18 @@
from django.apps import AppConfig
from django.db.models.signals import post_migrate
def setup_groups(sender, apps, **kwargs):
"""
Add the appropriate permissions to the "member" and "buro" groups after the
`post_migrate` signal since the permissions will only be inserted in the
database at the very end of the migrations.
"""
Group = apps.get_model("auth", "Group")
Permission = apps.get_model("auth", "Permission")
# Buro members have perms cof.* and gestion.*
buro, _ = Group.objects.get_or_create(name="cof_buro")
app_perms = Permission.objects.filter(
content_type__app_label__in=["cof", "gestion"]
)
buro.permissions.add(*app_perms)
# Members have perm cof.member
members, _ = Group.objects.get_or_create(name="cof_members")
perm = Permission.objects.get(
codename="member",
content_type__app_label="cof"
)
members.permissions.add(perm)
from gestion.apps import setup_assoc_perms
class COFConfig(AppConfig):
name = "cof"
verbose_name = "Application de gestion du COF"
def ready(self):
# https://docs.djangoproject.com/en/1.11/ref/signals/#post-migrate
post_migrate.connect(setup_groups, sender=self)
def setup_cof_perms(sender, apps, **kwargs):
from cof.models import get_cof_assoc
setup_assoc_perms(apps, get_cof_assoc, buro_of_apps=['gestion', 'cof'])
# Setup permissions of defaults groups of BDS association after Permission
# instances have been created, i.e. after applying migrations.
post_migrate.connect(setup_cof_perms)

View file

@ -6,11 +6,16 @@ from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import python_2_unicode_compatible
import django.utils.six as six
from gestion.models import Profile
from gestion.models import Association, Profile
from bda.models import Spectacle
from .petits_cours_models import choices_length
def get_cof_assoc():
return Association.objects.get(name='COF')
TYPE_COTIZ_CHOICES = (
('etudiant', _("Normalien étudiant")),
('normalien', _("Normalien élève")),