server/src/remie/signals.py
author durandn
Fri, 18 Sep 2015 17:21:49 +0200
changeset 156 8c8fd6a5bba9
parent 142 8d5035e5adaf
permissions -rw-r--r--
fixed playing video on segment click, was deleted by mistake
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
142
8d5035e5adaf change the way the user is created after a cas login
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
from django.conf import settings
8d5035e5adaf change the way the user is created after a cas login
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
from django.contrib.auth.models import Group
8d5035e5adaf change the way the user is created after a cas login
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
from django.dispatch import receiver
8d5035e5adaf change the way the user is created after a cas login
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
from django_cas_ng.signals import cas_user_authenticated
8d5035e5adaf change the way the user is created after a cas login
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
8d5035e5adaf change the way the user is created after a cas login
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
@receiver(cas_user_authenticated)
8d5035e5adaf change the way the user is created after a cas login
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
def update_created_user(sender, **kwargs):
8d5035e5adaf change the way the user is created after a cas login
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
8d5035e5adaf change the way the user is created after a cas login
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
    created = kwargs.get('created', False)
8d5035e5adaf change the way the user is created after a cas login
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
    user = kwargs.get('user', None)
8d5035e5adaf change the way the user is created after a cas login
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
    attributes = kwargs.get('attributes', {})
8d5035e5adaf change the way the user is created after a cas login
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
8d5035e5adaf change the way the user is created after a cas login
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
    if not created or not user:
8d5035e5adaf change the way the user is created after a cas login
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
        return
8d5035e5adaf change the way the user is created after a cas login
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
8d5035e5adaf change the way the user is created after a cas login
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
    user.first_name = attributes.get('first_name','')
8d5035e5adaf change the way the user is created after a cas login
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
    user.last_name = attributes.get('last_name', '')
8d5035e5adaf change the way the user is created after a cas login
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
    cas_user_group_name = getattr(settings, 'CAS_USER_DEFAULT_GROUP', None)
8d5035e5adaf change the way the user is created after a cas login
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
    if cas_user_group_name and cas_user_group_name not in [g.name for g in user.groups.all()]:
8d5035e5adaf change the way the user is created after a cas login
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
        cas_user_group, _ = Group.objects.get_or_create(name=cas_user_group_name)
8d5035e5adaf change the way the user is created after a cas login
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
        user.groups.add(cas_user_group)
8d5035e5adaf change the way the user is created after a cas login
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
    user.save()