2018-02-27 04:18:36 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
|
|
|
from django.dispatch import receiver
|
2019-12-16 08:53:29 +00:00
|
|
|
from django.db.models.signals import m2m_changed
|
2020-04-26 12:36:17 +00:00
|
|
|
from django_auth_ldap.backend import populate_user
|
|
|
|
from django.conf import settings
|
2020-03-12 08:24:38 +00:00
|
|
|
from django_cas_ng.signals import cas_user_authenticated
|
2018-02-27 04:18:36 +00:00
|
|
|
|
2020-04-28 14:29:56 +00:00
|
|
|
from jms_oidc_rp.signals import openid_create_or_update_user
|
2020-04-26 12:36:17 +00:00
|
|
|
|
2020-09-27 11:47:13 +00:00
|
|
|
from perms.tasks import create_rebuild_user_tree_task
|
2018-02-27 04:18:36 +00:00
|
|
|
from common.utils import get_logger
|
2018-04-03 08:28:58 +00:00
|
|
|
from .signals import post_user_create
|
2019-11-06 09:18:39 +00:00
|
|
|
from .models import User
|
2018-02-27 04:18:36 +00:00
|
|
|
|
2020-04-26 12:36:17 +00:00
|
|
|
|
2018-02-27 04:18:36 +00:00
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
2018-04-03 08:28:58 +00:00
|
|
|
@receiver(post_user_create)
|
|
|
|
def on_user_create(sender, user=None, **kwargs):
|
|
|
|
logger.debug("Receive user `{}` create signal".format(user.name))
|
|
|
|
from .utils import send_user_created_mail
|
|
|
|
logger.info(" - Sending welcome mail ...".format(user.name))
|
2020-03-13 04:33:09 +00:00
|
|
|
if user.can_send_created_mail():
|
2018-04-03 08:28:58 +00:00
|
|
|
send_user_created_mail(user)
|
2019-11-06 09:18:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(m2m_changed, sender=User.groups.through)
|
2020-09-27 11:47:13 +00:00
|
|
|
def on_user_groups_change(instance, action, reverse, pk_set, **kwargs):
|
2019-11-06 09:18:39 +00:00
|
|
|
if action.startswith('post'):
|
2020-09-27 11:47:13 +00:00
|
|
|
if reverse:
|
|
|
|
create_rebuild_user_tree_task(pk_set)
|
|
|
|
else:
|
|
|
|
create_rebuild_user_tree_task([instance.id])
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(cas_user_authenticated)
|
|
|
|
def on_cas_user_authenticated(sender, user, created, **kwargs):
|
|
|
|
if created:
|
|
|
|
user.source = user.SOURCE_CAS
|
|
|
|
user.save()
|
2020-04-26 12:36:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(populate_user)
|
|
|
|
def on_ldap_create_user(sender, user, ldap_user, **kwargs):
|
|
|
|
if user and user.username not in ['admin']:
|
|
|
|
exists = User.objects.filter(username=user.username).exists()
|
|
|
|
if not exists:
|
|
|
|
user.source = user.SOURCE_LDAP
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
|
2020-04-28 14:29:56 +00:00
|
|
|
@receiver(openid_create_or_update_user)
|
|
|
|
def on_openid_create_or_update_user(sender, request, user, created, name, username, email, **kwargs):
|
2020-04-28 13:00:22 +00:00
|
|
|
if created:
|
2020-04-28 16:43:54 +00:00
|
|
|
logger.debug(
|
|
|
|
"Receive OpenID user created signal: {}, "
|
|
|
|
"Set user source is: {}".format(user, User.SOURCE_OPENID)
|
|
|
|
)
|
2020-04-28 13:00:22 +00:00
|
|
|
user.source = User.SOURCE_OPENID
|
|
|
|
user.save()
|
2020-04-28 14:29:56 +00:00
|
|
|
elif not created and settings.AUTH_OPENID_ALWAYS_UPDATE_USER:
|
2020-04-28 16:43:54 +00:00
|
|
|
logger.debug(
|
|
|
|
"Receive OpenID user updated signal: {}, "
|
|
|
|
"Update user info: {}"
|
|
|
|
"".format(user, "name: {}|username: {}|email: {}".format(name, username, email))
|
|
|
|
)
|
2020-04-28 13:00:22 +00:00
|
|
|
user.name = name
|
|
|
|
user.username = username
|
|
|
|
user.email = email
|
|
|
|
user.save()
|