create directories for new users

- use new file tasks to create SFTP and mail base directories for users
- use json serializer as default
- remove TaskResult classes that don't provide any significant benefit
This commit is contained in:
Jan Dittberner 2014-12-26 15:10:36 +01:00
parent 2428a39f19
commit 0b6ac2a478
4 changed files with 72 additions and 212 deletions

View file

@ -282,9 +282,9 @@ CELERY_ROUTES = (
'osusers.tasks.LdapRouter',
'osusers.tasks.FileRouter',
)
CELERY_ACCEPT_CONTENT = ['yaml']
CELERY_TASK_SERIALIZER = 'yaml'
CELERY_RESULT_SERIALIZER = 'yaml'
CELERY_ACCEPT_CONTENT = ['pickle', 'yaml', 'json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
########## END CELERY CONFIGURATION

View file

@ -4,12 +4,9 @@ from django.contrib import admin
from .models import (
AdditionalGroup,
DeleteTaskResult,
Group,
GroupTaskResult,
Shadow,
User,
UserTaskResult,
)
PASSWORD_MISMATCH_ERROR = _("Passwords don't match")
@ -129,45 +126,5 @@ class GroupAdmin(admin.ModelAdmin):
return actions
class TaskResultAdmin(admin.ModelAdmin):
def has_add_permission(self, request, obj=None):
return False
def has_delete_permission(self, request, obj=None):
return obj is None or obj.is_finished
def get_queryset(self, request):
qs = super(TaskResultAdmin, self).get_queryset(request)
for entry in qs:
entry.update_taskstatus()
return qs
class DeleteTaskResultAdmin(TaskResultAdmin):
readonly_fields = ['task_uuid', 'task_name', 'modeltype', 'modelname',
'is_finished', 'is_success', 'state', 'result_body']
list_display = ('task_uuid', 'task_name', 'modeltype', 'modelname',
'is_finished', 'state')
class GroupTaskResultAdmin(TaskResultAdmin):
readonly_fields = [
'task_uuid', 'task_name', 'group', 'is_finished', 'is_success',
'state', 'result_body'
]
list_display = ('task_uuid', 'task_name', 'group', 'is_finished', 'state')
class UserTaskResultAdmin(TaskResultAdmin):
readonly_fields = [
'task_uuid', 'task_name', 'user', 'is_finished', 'is_success', 'state',
'result_body'
]
list_display = ('task_uuid', 'task_name', 'user', 'is_finished', 'state')
admin.site.register(Group, GroupAdmin)
admin.site.register(User, UserAdmin)
admin.site.register(DeleteTaskResult, DeleteTaskResultAdmin)
admin.site.register(GroupTaskResult, GroupTaskResultAdmin)
admin.site.register(UserTaskResult, UserTaskResultAdmin)

View file

@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('osusers', '0001_initial'),
]
operations = [
migrations.DeleteModel(
name='DeleteTaskResult',
),
migrations.RemoveField(
model_name='grouptaskresult',
name='group',
),
migrations.DeleteModel(
name='GroupTaskResult',
),
migrations.RemoveField(
model_name='usertaskresult',
name='user',
),
migrations.DeleteModel(
name='UserTaskResult',
),
]

View file

@ -1,6 +1,7 @@
from __future__ import unicode_literals
from datetime import date
import logging
import os
from django.db import models, transaction
@ -21,42 +22,23 @@ from .tasks import (
add_ldap_user_to_group,
create_ldap_group,
create_ldap_user,
delete_file_mail_userdir,
delete_file_sftp_userdir,
delete_ldap_group_if_empty,
delete_ldap_user,
remove_ldap_user_from_group,
setup_file_mail_userdir,
setup_file_sftp_userdir,
)
logger = logging.getLogger(__name__)
CANNOT_USE_PRIMARY_GROUP_AS_ADDITIONAL = _(
"You can not use a user's primary group.")
class TaskResult(TimeStampedModel, models.Model):
task_uuid = models.CharField(primary_key=True, max_length=64, blank=False)
task_name = models.CharField(max_length=255, blank=False, db_index=True)
is_finished = models.BooleanField(default=False)
is_success = models.BooleanField(default=False)
state = models.CharField(max_length=10)
result_body = models.TextField(blank=True)
class Meta:
abstract = True
def _set_result_fields(self, asyncresult):
if not self.is_finished:
result = asyncresult.get(no_ack=False)
self.is_finished = True
self.is_success = asyncresult.state == 'SUCCESS'
self.state = asyncresult.state
self.result_body = str(result)
def update_taskstatus(self):
asyncresult = AsyncResult(self.task_uuid)
self._set_result_fields(asyncresult)
self.save()
class GroupManager(models.Manager):
def get_next_gid(self):
@ -88,85 +70,17 @@ class Group(TimeStampedModel, models.Model):
@transaction.atomic
def save(self, *args, **kwargs):
super(Group, self).save(*args, **kwargs)
GroupTaskResult.objects.create_grouptaskresult(
self,
create_ldap_group.delay(self.groupname, self.gid, self.descr),
'create_ldap_group'
)
dn = create_ldap_group.delay(
self.groupname, self.gid, self.descr).get()
logger.info("created LDAP group with dn %s", dn)
return self
@transaction.atomic
def delete(self, *args, **kwargs):
DeleteTaskResult.objects.create_deletetaskresult(
'group', self.groupname,
delete_ldap_group_if_empty.delay(self.groupname),
'delete_ldap_group_if_empty'
)
delete_ldap_group_if_empty.delay(self.groupname).get()
super(Group, self).delete(*args, **kwargs)
class TaskResultManager(models.Manager):
def create(self, asyncresult, task_name):
result = self.model(
task_uuid=asyncresult.task_id, task_name=task_name
)
result._set_result_fields(asyncresult)
return result
class DeleteTaskResultManager(TaskResultManager):
def create_deletetaskresult(
self, modeltype, modelname, asyncresult, task_name
):
taskresult = super(DeleteTaskResultManager, self).create(
asyncresult, task_name)
taskresult.modeltype = modeltype
taskresult.modelname = modelname
taskresult.save()
return taskresult
@python_2_unicode_compatible
class DeleteTaskResult(TaskResult):
modeltype = models.CharField(max_length=20, db_index=True)
modelname = models.CharField(max_length=255)
objects = DeleteTaskResultManager()
def __str__(self):
return "{task_uuid} {task_name} {modeltype} {modelname}".format(
task_uuid=self.task_uuid, task_name=self.task_name,
modeltype=self.modeltype, modelname=self.modelname)
class GroupTaskResultManager(TaskResultManager):
def create_grouptaskresult(
self, group, asyncresult, task_name, commit=False
):
taskresult = super(GroupTaskResultManager, self).create(
asyncresult, task_name)
taskresult.group = group
taskresult.save()
return taskresult
@python_2_unicode_compatible
class GroupTaskResult(TaskResult):
group = models.ForeignKey(Group)
objects = GroupTaskResultManager()
def __str__(self):
return "{task_uuid} {task_name} {group}".format(
task_uuid=self.task_uuid, task_name=self.task_name,
group=self.group)
class UserManager(models.Manager):
def get_next_uid(self):
@ -238,72 +152,39 @@ class User(TimeStampedModel, models.Model):
self.shadow = Shadow.objects.create_shadow(
user=self, password=password
)
UserTaskResult.objects.create_usertaskresult(
self,
create_ldap_user.delay(
self.username, self.uid, self.group.gid, self.gecos,
self.homedir, self.shell, password
),
'create_ldap_user',
commit=True
)
dn = create_ldap_user.delay(
self.username, self.uid, self.group.gid, self.gecos,
self.homedir, self.shell, password
).get()
logging.info("set LDAP password for %s", dn)
@transaction.atomic
def save(self, *args, **kwargs):
UserTaskResult.objects.create_usertaskresult(
self,
create_ldap_user.delay(
self.username, self.uid, self.group.gid, self.gecos,
self.homedir, self.shell, password=None
),
'create_ldap_user'
)
dn = create_ldap_user.delay(
self.username, self.uid, self.group.gid, self.gecos,
self.homedir, self.shell, password=None).get()
sftp_dir = setup_file_sftp_userdir.delay(self.username).get()
mail_dir = setup_file_mail_userdir.delay(self.username).get()
logger.info(
"created user %(user)s with LDAP dn %(dn)s, home directory "
"%(homedir)s and mail base directory %(maildir)s.", {
'user': self, 'dn': dn,
'homedir': sftp_dir, 'maildir': mail_dir
})
return super(User, self).save(*args, **kwargs)
@transaction.atomic
def delete(self, *args, **kwargs):
for group in [
ag.group for ag in AdditionalGroup.objects.filter(user=self)
]:
DeleteTaskResult.objects.create_deletetaskresult(
'usergroup',
'{0} in {1}'.format(self.username, group.groupname),
remove_ldap_user_from_group.delay(
self.username, group.groupname),
'remove_ldap_user_from_group',
)
DeleteTaskResult.objects.create_deletetaskresult(
'user', self.username,
delete_ldap_user.delay(self.username),
'delete_ldap_user'
)
delete_file_mail_userdir.delay(self.username).get()
delete_file_sftp_userdir.delay(self.username).get()
for group in [ag.group for ag in self.additionalgroup_set.all()]:
remove_ldap_user_from_group.delay(
self.username, group.groupname).get()
delete_ldap_user.delay(self.username).get()
self.group.delete()
super(User, self).delete(*args, **kwargs)
class UserTaskResultManager(TaskResultManager):
def create_usertaskresult(
self, user, asyncresult, task_name, commit=False
):
taskresult = self.create(asyncresult, task_name)
taskresult.user = user
taskresult.save()
return taskresult
@python_2_unicode_compatible
class UserTaskResult(TaskResult):
user = models.ForeignKey(User)
objects = UserTaskResultManager()
def __str__(self):
return "{task_uuid} {task_name} {user}".format(
task_uuid=self.task_uuid, task_name=self.task_name, user=self.user)
class ShadowManager(models.Manager):
@transaction.atomic
@ -382,23 +263,14 @@ class AdditionalGroup(TimeStampedModel, models.Model):
@transaction.atomic
def save(self, *args, **kwargs):
GroupTaskResult.objects.create_grouptaskresult(
self.group,
add_ldap_user_to_group.delay(
self.user.username, self.group.groupname),
'add_ldap_user_to_group'
)
add_ldap_user_to_group.delay(
self.user.username, self.group.groupname).get()
super(AdditionalGroup, self).save(*args, **kwargs)
@transaction.atomic
def delete(self, *args, **kwargs):
DeleteTaskResult.objects.create_deletetaskresult(
'usergroup',
str(self),
remove_ldap_user_from_group.delay(
self.user.username, self.group.groupname),
'remove_ldap_user_from_group'
)
remove_ldap_user_from_group.delay(
self.user.username, self.group.groupname).get()
super(AdditionalGroup, self).delete(*args, **kwargs)
def __str__(self):