add default group to user at creation + custom perms
This commit is contained in:
parent
5da5e41701
commit
65b4d8244d
|
@ -30,7 +30,7 @@ for:
|
|||
|
||||
* cron: daily cron configuration for the generation of the diffusions
|
||||
* supervisorctl: audio stream generation, website, sounds monitoring
|
||||
* nginx: sampe config file (must be adapted)
|
||||
* nginx: sample config file (must be adapted)
|
||||
|
||||
The scripts are written with a combination of `cron`, `supervisord`, `nginx`
|
||||
and `gunicorn` in mind.
|
||||
|
|
|
@ -6,6 +6,14 @@ from django.conf import settings
|
|||
def ensure (key, default):
|
||||
globals()[key] = getattr(settings, key, default)
|
||||
|
||||
# name of the group assigned by default to all users that are created
|
||||
ensure('AIRCOX_DEFAULT_USER_GROUP', 'Radio Hosts')
|
||||
ensure('AIRCOX_DEFAULT_USER_GROUP_PERMS', (
|
||||
'change_program', 'change_diffusion',
|
||||
'change_sound',
|
||||
'add_track', 'change_track', 'delete_track',
|
||||
'add_tag', 'change_tag', 'delete_tag',
|
||||
))
|
||||
|
||||
# Directory for the programs data
|
||||
ensure('AIRCOX_PROGRAMS_DIR',
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
from django.db.models.signals import post_save, pre_delete
|
||||
from django.db.models.signals import post_save, pre_save, pre_delete, m2m_changed
|
||||
from django.contrib.auth.models import User, Group, Permission
|
||||
|
||||
from django.dispatch import receiver
|
||||
from django.utils.translation import ugettext as _, ugettext_lazy
|
||||
|
@ -6,6 +7,31 @@ from django.contrib.contenttypes.models import ContentType
|
|||
|
||||
import aircox.models as models
|
||||
import aircox.utils as utils
|
||||
import aircox.settings as settings
|
||||
|
||||
# Add a default group to a user when it is created. It also assigns a list
|
||||
# of permissions to the group if it is created.
|
||||
#
|
||||
# - group name: settings.AIRCOX_DEFAULT_USER_GROUP
|
||||
# - group permissions: settings.AIRCOX_DEFAULT_USER_GROUP_PERMS
|
||||
#
|
||||
@receiver(post_save, sender=User)
|
||||
def user_default_groups(sender, instance, created, *args, **kwargs):
|
||||
groupName = settings.AIRCOX_DEFAULT_USER_GROUP
|
||||
if not created or instance.is_superuser or \
|
||||
instance.groups.filter(name = groupName).count():
|
||||
return
|
||||
|
||||
group, created = Group.objects.get_or_create(name = groupName)
|
||||
if created:
|
||||
for codename in settings.AIRCOX_DEFAULT_USER_GROUP_PERMS:
|
||||
permission = Permission.objects.filter(codename = codename).first()
|
||||
if permission:
|
||||
group.permissions.add(permission)
|
||||
group.save()
|
||||
|
||||
instance.groups.add(group)
|
||||
|
||||
|
||||
# FIXME: avoid copy of the code in schedule_post_saved and
|
||||
# schedule_pre_delete
|
||||
|
|
Loading…
Reference in New Issue
Block a user