add more ldap tasks

- add custom celery router osusers.tasks.LdapRouter
- add tasks add_ldap_user_to_group, delete_ldap_group_if_empty,
  delete_ldap_user and remove_ldap_user_from_group
- implement osusers.models.Group.save and osusers.models.Group.delete
- implement save, delete and set_password methods in osusers.models.User
- implement save and delete methods in osusers.models.AdditionalGroup
This commit is contained in:
Jan Dittberner 2014-05-30 17:10:22 +02:00
parent 1e717556ba
commit 6eb74f5b79
3 changed files with 75 additions and 7 deletions

View file

@ -284,10 +284,9 @@ BROKER_URL = get_env_variable('GVA_BROKER_URL')
CELERY_RESULT_BACKEND = 'amqp'
CELERY_RESULT_PERSISTENT = True
CELERY_TASK_RESULT_EXPIRES = None
CELERY_ROUTES = {
'osusers.tasks.create_ldap_group': {'queue': 'ldap'},
'osusers.tasks.create_ldap_user': {'queue': 'ldap'},
}
CELERY_ROUTES = (
'osusers.tasks.LdapRouter',
)
########## END CELERY CONFIGURATION

View file

@ -14,8 +14,12 @@ from passlib.hash import sha512_crypt
from passlib.utils import generate_password
from .tasks import (
add_ldap_user_to_group,
create_ldap_group,
create_ldap_user,
delete_ldap_group_if_empty,
delete_ldap_user,
remove_ldap_user_from_group,
)
@ -47,6 +51,15 @@ class Group(TimeStampedModel, models.Model):
def __str__(self):
return '{0} ({1})'.format(self.groupname, self.gid)
def save(self, *args, **kwargs):
super(Group, self).save(*args, **kwargs)
create_ldap_group.delay(self)
return self
def delete(self, *args, **kwargs):
delete_ldap_group_if_empty.delay(self)
super(Group, self).delete(*args, **kwargs)
class UserManager(models.Manager):
@ -85,11 +98,10 @@ class UserManager(models.Manager):
user = self.create(username=username, group=group, uid=uid,
homedir=homedir,
shell=settings.OSUSER_DEFAULT_SHELL)
create_ldap_user.delay(user, password)
shadow = Shadow.objects.create_shadow(user=user, password=password)
Shadow.objects.create_shadow(user=user, password=password)
user.set_password(password)
if commit:
user.save()
shadow.save()
return user
@ -113,6 +125,23 @@ class User(TimeStampedModel, models.Model):
def __str__(self):
return '{0} ({1})'.format(self.username, self.uid)
def set_password(self, password):
create_ldap_user.delay(self, password)
def save(self, *args, **kwargs):
create_ldap_user.delay(self, password=None)
return super(User, self).save(*args, **kwargs)
def delete(self, *args, **kwargs):
for group in [
ag.group for ag in AdditionalGroup.objects.filter(user=self)
]:
remove_ldap_user_from_group.delay(self.username, group.groupname)
delete_ldap_user.delay(self)
delete_ldap_group_if_empty.delay(self.group)
self.group.delete()
super(User, self).delete(*args, **kwargs)
class ShadowManager(models.Manager):
@ -187,5 +216,15 @@ class AdditionalGroup(TimeStampedModel, models.Model):
raise ValidationError(_(
"You can not use a user's primary group."))
def save(self, *args, **kwargs):
add_ldap_user_to_group.delay(
self.user.username, self.group.groupname)
super(AdditionalGroup, self).save(*args, **kwargs)
def delete(self, *args, **kwargs):
remove_ldap_user_from_group.delay(
self.user.username, self.group.groupname)
super(AdditionalGroup, self).delete(*args, **kwargs)
def __str__(self):
return '{0} in {1}'.format(self.user, self.group)

View file

@ -3,6 +3,16 @@ from __future__ import absolute_import
from celery import shared_task
class LdapRouter(object):
def route_for_task(self, task, args=None, kwargs=None):
if 'ldap' in task:
return {'exchange': 'ldap',
'exchange_type': 'direct',
'queue': 'ldap'}
return None
@shared_task
def create_ldap_group(group):
return group.groupname
@ -11,3 +21,23 @@ def create_ldap_group(group):
@shared_task
def create_ldap_user(user, password):
return user.username
@shared_task
def add_ldap_user_to_group(username, groupname):
pass
@shared_task
def remove_ldap_user_from_group(username, groupname):
pass
@shared_task
def delete_ldap_user(user):
pass
@shared_task
def delete_ldap_group_if_empty(group):
pass