mirror of https://github.com/zulip/zulip.git
40 lines
1.5 KiB
Python
40 lines
1.5 KiB
Python
from django.conf import settings
|
|
from django.contrib.auth.models import User
|
|
from django.contrib.auth.models import UserManager
|
|
from django.utils import timezone
|
|
from zephyr.models import UserProfile
|
|
import base64
|
|
import hashlib
|
|
|
|
# create_user_hack is the same as Django's User.objects.create_user,
|
|
# except that we don't save to the database so it can used in
|
|
# bulk_creates
|
|
def create_user_hack(username, password, email, active):
|
|
now = timezone.now()
|
|
email = UserManager.normalize_email(email)
|
|
user = User(username=username, email=email,
|
|
is_staff=False, is_active=active, is_superuser=False,
|
|
last_login=now, date_joined=now)
|
|
|
|
if active:
|
|
user.set_password(password)
|
|
else:
|
|
user.set_unusable_password()
|
|
return user
|
|
|
|
def create_user_base(email, password, active=True):
|
|
# NB: the result of Base32 + truncation is not a valid Base32 encoding.
|
|
# It's just a unique alphanumeric string.
|
|
# Use base32 instead of base64 so we don't have to worry about mixed case.
|
|
# Django imposes a limit of 30 characters on usernames.
|
|
email_hash = hashlib.sha256(settings.HASH_SALT + email).digest()
|
|
username = base64.b32encode(email_hash)[:30]
|
|
return create_user_hack(username, password, email, active)
|
|
|
|
def create_user(email, password, realm, full_name, short_name,
|
|
active=True):
|
|
user = create_user_base(email=email, password=password,
|
|
active=active)
|
|
user.save()
|
|
return UserProfile.create(user, realm, full_name, short_name)
|