2013-04-23 18:51:17 +02:00
|
|
|
|
2013-02-12 21:14:48 +01:00
|
|
|
from django.contrib.auth.models import UserManager
|
2017-04-15 04:03:56 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2016-06-16 22:22:26 +02:00
|
|
|
from zerver.models import UserProfile, Recipient, Subscription, Realm, Stream
|
2013-02-12 21:14:48 +01:00
|
|
|
import base64
|
2013-06-18 23:55:55 +02:00
|
|
|
import ujson
|
2013-07-30 01:59:24 +02:00
|
|
|
import os
|
2013-05-30 20:21:26 +02:00
|
|
|
import string
|
2013-05-08 15:27:27 +02:00
|
|
|
|
2016-12-21 13:17:53 +01:00
|
|
|
from typing import Optional, Text
|
2016-06-16 22:22:26 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def random_api_key() -> Text:
|
2013-07-30 01:59:24 +02:00
|
|
|
choices = string.ascii_letters + string.digits
|
2016-01-24 05:18:35 +01:00
|
|
|
altchars = ''.join([choices[ord(os.urandom(1)) % 62] for _ in range(2)]).encode("utf-8")
|
|
|
|
return base64.b64encode(os.urandom(24), altchars=altchars).decode("utf-8")
|
2013-07-19 17:34:58 +02:00
|
|
|
|
2013-04-01 16:57:50 +02:00
|
|
|
# create_user_profile is based on Django's User.objects.create_user,
|
2013-02-12 21:14:48 +01:00
|
|
|
# except that we don't save to the database so it can used in
|
|
|
|
# bulk_creates
|
2013-04-01 16:57:50 +02:00
|
|
|
#
|
|
|
|
# Only use this for bulk_create -- for normal usage one should use
|
|
|
|
# create_user (below) which will also make the Subscription and
|
|
|
|
# Recipient objects
|
2017-12-19 18:51:39 +01:00
|
|
|
def create_user_profile(realm: Realm, email: Text, password: Optional[Text],
|
|
|
|
active: bool, bot_type: Optional[int], full_name: Text,
|
|
|
|
short_name: Text, bot_owner: Optional[UserProfile],
|
|
|
|
is_mirror_dummy: bool, tos_version: Text,
|
|
|
|
timezone: Optional[Text],
|
|
|
|
tutorial_status: Optional[Text] = UserProfile.TUTORIAL_WAITING,
|
|
|
|
enter_sends: bool = False) -> UserProfile:
|
2017-04-15 04:03:56 +02:00
|
|
|
now = timezone_now()
|
2013-02-12 21:14:48 +01:00
|
|
|
email = UserManager.normalize_email(email)
|
2014-10-01 02:17:11 +02:00
|
|
|
|
2013-04-01 16:57:50 +02:00
|
|
|
user_profile = UserProfile(email=email, is_staff=False, is_active=active,
|
|
|
|
full_name=full_name, short_name=short_name,
|
|
|
|
last_login=now, date_joined=now, realm=realm,
|
2016-05-18 20:23:03 +02:00
|
|
|
pointer=-1, is_bot=bool(bot_type), bot_type=bot_type,
|
2017-01-12 11:14:13 +01:00
|
|
|
bot_owner=bot_owner, is_mirror_dummy=is_mirror_dummy,
|
2017-05-04 15:19:06 +02:00
|
|
|
tos_version=tos_version, timezone=timezone,
|
2017-03-22 23:26:35 +01:00
|
|
|
tutorial_status=tutorial_status,
|
2017-03-29 23:21:44 +02:00
|
|
|
enter_sends=enter_sends,
|
2016-08-04 17:32:41 +02:00
|
|
|
onboarding_steps=ujson.dumps([]),
|
|
|
|
default_language=realm.default_language)
|
2013-02-12 21:14:48 +01:00
|
|
|
|
2016-05-18 20:23:03 +02:00
|
|
|
if bot_type or not active:
|
2013-11-12 18:20:05 +01:00
|
|
|
password = None
|
|
|
|
|
|
|
|
user_profile.set_password(password)
|
2013-05-03 00:25:43 +02:00
|
|
|
|
2013-07-19 17:34:58 +02:00
|
|
|
user_profile.api_key = random_api_key()
|
2013-04-01 16:57:50 +02:00
|
|
|
return user_profile
|
2013-02-12 21:14:48 +01:00
|
|
|
|
2017-12-19 18:51:39 +01:00
|
|
|
def create_user(email: Text, password: Optional[Text], realm: Realm,
|
|
|
|
full_name: Text, short_name: Text, active: bool = True,
|
|
|
|
is_realm_admin: bool = False, bot_type: Optional[int] = None,
|
|
|
|
bot_owner: Optional[UserProfile] = None,
|
|
|
|
tos_version: Optional[Text] = None, timezone: Text = "",
|
|
|
|
avatar_source: Text = UserProfile.AVATAR_FROM_GRAVATAR,
|
|
|
|
is_mirror_dummy: bool = False,
|
|
|
|
default_sending_stream: Optional[Stream] = None,
|
|
|
|
default_events_register_stream: Optional[Stream] = None,
|
|
|
|
default_all_public_streams: Optional[bool] = None,
|
|
|
|
user_profile_id: Optional[int] = None) -> UserProfile:
|
2016-05-18 20:23:03 +02:00
|
|
|
user_profile = create_user_profile(realm, email, password, active, bot_type,
|
2014-01-07 18:57:54 +01:00
|
|
|
full_name, short_name, bot_owner,
|
2017-05-04 15:19:06 +02:00
|
|
|
is_mirror_dummy, tos_version, timezone)
|
2017-08-18 07:12:22 +02:00
|
|
|
user_profile.is_realm_admin = is_realm_admin
|
2013-06-14 20:03:11 +02:00
|
|
|
user_profile.avatar_source = avatar_source
|
2017-05-04 15:19:06 +02:00
|
|
|
user_profile.timezone = timezone
|
2014-02-11 18:43:30 +01:00
|
|
|
user_profile.default_sending_stream = default_sending_stream
|
|
|
|
user_profile.default_events_register_stream = default_events_register_stream
|
|
|
|
# Allow the ORM default to be used if not provided
|
|
|
|
if default_all_public_streams is not None:
|
|
|
|
user_profile.default_all_public_streams = default_all_public_streams
|
|
|
|
|
2014-05-03 03:04:16 +02:00
|
|
|
if user_profile_id is not None:
|
|
|
|
user_profile.id = user_profile_id
|
|
|
|
|
2013-04-01 16:57:50 +02:00
|
|
|
user_profile.save()
|
|
|
|
recipient = Recipient.objects.create(type_id=user_profile.id,
|
|
|
|
type=Recipient.PERSONAL)
|
|
|
|
Subscription.objects.create(user_profile=user_profile, recipient=recipient)
|
|
|
|
return user_profile
|