implement user creation in osusers.admin
- add osusers.admin.UserCreationForm - add dummy osusers.tasks implementation with create_ldap_group and create_ldap_user - fix UserManager.get_next_username - add proper transaction handling in UserManager.create_user - add calls to create_ldap_user and create_ldap_group to UserManager.create_user
This commit is contained in:
parent
86b8f03704
commit
caab322beb
3 changed files with 94 additions and 11 deletions
|
@ -1,7 +1,7 @@
|
|||
from datetime import date
|
||||
import os
|
||||
|
||||
from django.db import models, transaction
|
||||
from django.db import models
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.utils import timezone
|
||||
|
@ -13,6 +13,11 @@ from model_utils.models import TimeStampedModel
|
|||
from passlib.hash import sha512_crypt
|
||||
from passlib.utils import generate_password
|
||||
|
||||
from .tasks import (
|
||||
create_ldap_group,
|
||||
create_ldap_user,
|
||||
)
|
||||
|
||||
|
||||
class GroupManager(models.Manager):
|
||||
|
||||
|
@ -59,7 +64,7 @@ class UserManager(models.Manager):
|
|||
for user in self.values('username').filter(
|
||||
username__startswith=settings.OSUSER_USERNAME_PREFIX).order_by(
|
||||
'username'):
|
||||
if user == nextuser:
|
||||
if user['username'] == nextuser:
|
||||
count += 1
|
||||
nextuser = usernameformat.format(
|
||||
settings.OSUSER_USERNAME_PREFIX, count)
|
||||
|
@ -67,7 +72,7 @@ class UserManager(models.Manager):
|
|||
break
|
||||
return nextuser
|
||||
|
||||
def create_user(self, username=None, password=None):
|
||||
def create_user(self, username=None, password=None, commit=False):
|
||||
uid = self.get_next_uid()
|
||||
gid = Group.objects.get_next_gid()
|
||||
if username is None:
|
||||
|
@ -75,19 +80,16 @@ class UserManager(models.Manager):
|
|||
if password is None:
|
||||
password = generate_password()
|
||||
homedir = os.path.join(settings.OSUSER_HOME_BASEPATH, username)
|
||||
autocommit = transaction.get_autocommit()
|
||||
if autocommit:
|
||||
transaction.set_autocommit(False)
|
||||
group = Group.objects.create(groupname=username, gid=gid)
|
||||
create_ldap_group.delay(group)
|
||||
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)
|
||||
user.save()
|
||||
shadow.save()
|
||||
transaction.commit()
|
||||
if autocommit:
|
||||
transaction.set_autocommit(True)
|
||||
if commit:
|
||||
user.save()
|
||||
shadow.save()
|
||||
return user
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue