163 lines
5 KiB
Python
163 lines
5 KiB
Python
"""
|
|
This module contains the signal handlers of the :py:mod:`osusers` app.
|
|
|
|
"""
|
|
from __future__ import absolute_import, unicode_literals
|
|
|
|
import logging
|
|
|
|
from django.db.models.signals import (
|
|
m2m_changed,
|
|
post_delete,
|
|
post_save,
|
|
)
|
|
from django.dispatch import receiver
|
|
|
|
from celery import chain, group
|
|
|
|
from fileservertasks.tasks import (
|
|
delete_file_mail_userdir,
|
|
delete_file_sftp_userdir,
|
|
set_file_ssh_authorized_keys,
|
|
setup_file_mail_userdir,
|
|
setup_file_sftp_userdir,
|
|
)
|
|
from ldaptasks.tasks import (
|
|
add_ldap_user_to_group,
|
|
create_ldap_group,
|
|
create_ldap_user,
|
|
delete_ldap_group,
|
|
delete_ldap_user,
|
|
remove_ldap_user_from_group,
|
|
set_ldap_user_password,
|
|
)
|
|
from taskresults.models import TaskResult
|
|
|
|
from .models import (
|
|
AdditionalGroup,
|
|
Group,
|
|
SshPublicKey,
|
|
User,
|
|
password_set,
|
|
)
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
@receiver(password_set, sender=User)
|
|
def handle_user_password_set(sender, instance, password, **kwargs):
|
|
taskresult = TaskResult.objects.create_task_result(
|
|
'handle_user_password_set',
|
|
set_ldap_user_password.s(instance.username, password))
|
|
_LOGGER.info(
|
|
'LDAP password change has been requested in task %s',
|
|
taskresult.task_id)
|
|
|
|
|
|
# @receiver(post_save)
|
|
# def handle_post_save(sender, **kwargs):
|
|
# _LOGGER.debug(
|
|
# 'handling post_save signal for %s with args %s',
|
|
# sender, kwargs)
|
|
|
|
|
|
@receiver(post_save, sender=Group)
|
|
def handle_group_created(sender, instance, created, **kwargs):
|
|
if created:
|
|
taskresult = TaskResult.objects.create_task_result(
|
|
'handle_group_created',
|
|
create_ldap_group.s(
|
|
instance.groupname, instance.gid, instance.descr))
|
|
_LOGGER.info(
|
|
'LDAP group creation has been requested in task %s',
|
|
taskresult.task_id)
|
|
_LOGGER.debug(
|
|
'group %s has been %s', instance, created and "created" or "updated")
|
|
|
|
|
|
@receiver(post_save, sender=User)
|
|
def handle_user_created(sender, instance, created, **kwargs):
|
|
if created:
|
|
chain = create_ldap_user.s(
|
|
instance.username, instance.uid, instance.group.gid,
|
|
instance.gecos, instance.homedir, instance.shell, None
|
|
) | setup_file_sftp_userdir.s(
|
|
instance.username
|
|
) | setup_file_mail_userdir.s(instance.username)
|
|
taskresult = TaskResult.objects.create_task_result(
|
|
'handle_user_created', chain)
|
|
_LOGGER.info(
|
|
'LDAP user creation has been requested in task %s',
|
|
taskresult.task_id)
|
|
_LOGGER.debug(
|
|
'user %s has been %s', instance, created and "created" or "updated")
|
|
|
|
|
|
@receiver(post_save, sender=AdditionalGroup)
|
|
def handle_user_added_to_group(sender, instance, created, **kwargs):
|
|
if created:
|
|
taskresult = TaskResult.objects.create_task_result(
|
|
'handle_user_added_to_group',
|
|
add_ldap_user_to_group.s(
|
|
instance.user.username, instance.group.groupname))
|
|
_LOGGER.info(
|
|
'Adding user to LDAP group has been requested in task %s',
|
|
taskresult.task_id)
|
|
|
|
|
|
@receiver(post_save, sender=SshPublicKey)
|
|
@receiver(post_delete, sender=SshPublicKey)
|
|
def handle_ssh_keys_changed(sender, instance, **kwargs):
|
|
sig = set_file_ssh_authorized_keys.s(
|
|
instance.user.username, [
|
|
str(key) for key in
|
|
SshPublicKey.objects.filter(user=instance.user)])
|
|
taskresult = TaskResult.objects.create_task_result(
|
|
'handle_ssh_keys_changed', sig)
|
|
_LOGGER.info(
|
|
'Change of SSH keys has been requested in task %s',
|
|
taskresult.task_id)
|
|
|
|
|
|
# @receiver(post_delete)
|
|
# def handle_post_delete(sender, **kwargs):
|
|
# _LOGGER.debug(
|
|
# 'handling post_delete signal for %s with args %s',
|
|
# sender, kwargs)
|
|
|
|
|
|
@receiver(post_delete, sender=Group)
|
|
def handle_group_deleted(sender, instance, **kwargs):
|
|
taskresult = TaskResult.objects.create_task_result(
|
|
'handle_group_deleted',
|
|
delete_ldap_group.s(instance.groupname))
|
|
_LOGGER.info(
|
|
'LDAP group deletion has been requested in task %s',
|
|
taskresult.task_id)
|
|
|
|
|
|
@receiver(post_delete, sender=User)
|
|
def handle_user_deleted(sender, instance, **kwargs):
|
|
chain = delete_file_mail_userdir.s(
|
|
instance.username
|
|
) | delete_file_sftp_userdir.s(
|
|
instance.username
|
|
) | delete_ldap_user.s(instance.username)
|
|
_LOGGER.debug('chain signature %s', chain)
|
|
taskresult = TaskResult.objects.create_task_result(
|
|
'handle_user_deleted', chain)
|
|
_LOGGER.info(
|
|
'LDAP user deletion has been requested in task %s',
|
|
taskresult.task_id)
|
|
|
|
|
|
@receiver(post_delete, sender=AdditionalGroup)
|
|
def handle_user_removed_from_group(sender, instance, **kwargs):
|
|
taskresult = TaskResult.objects.create_task_result(
|
|
'handle_user_removed_from_group',
|
|
remove_ldap_user_from_group.s(
|
|
instance.user.username, instance.group.groupname))
|
|
_LOGGER.info(
|
|
'Removing user from LDAP group has been requested in task %s',
|
|
taskresult.task_id)
|