mirror of https://github.com/zulip/zulip.git
users: Modify do_create_user and create_user to accept role.
We change do_create_user and create_user to accept role as a parameter instead of 'is_realm_admin' and 'is_guest'. These changes are done to minimize data conversions between role and boolean fields.
This commit is contained in:
parent
cfc87e3925
commit
77d4be56a4
|
@ -59,7 +59,7 @@ class Command(BaseCommand):
|
||||||
with mock.patch("zerver.lib.create_user.timezone_now", return_value=installation_time):
|
with mock.patch("zerver.lib.create_user.timezone_now", return_value=installation_time):
|
||||||
shylock = create_user('shylock@analytics.ds', 'Shylock', realm,
|
shylock = create_user('shylock@analytics.ds', 'Shylock', realm,
|
||||||
full_name='Shylock', short_name='shylock',
|
full_name='Shylock', short_name='shylock',
|
||||||
is_realm_admin=True)
|
role=UserProfile.ROLE_REALM_ADMINISTRATOR)
|
||||||
do_change_user_role(shylock, UserProfile.ROLE_REALM_ADMINISTRATOR)
|
do_change_user_role(shylock, UserProfile.ROLE_REALM_ADMINISTRATOR)
|
||||||
stream = Stream.objects.create(
|
stream = Stream.objects.create(
|
||||||
name='all', realm=realm, date_created=installation_time)
|
name='all', realm=realm, date_created=installation_time)
|
||||||
|
|
|
@ -67,7 +67,7 @@ class AnalyticsTestCase(TestCase):
|
||||||
return create_user(kwargs['email'], 'password', kwargs['realm'],
|
return create_user(kwargs['email'], 'password', kwargs['realm'],
|
||||||
active=kwargs['is_active'],
|
active=kwargs['is_active'],
|
||||||
full_name=kwargs['full_name'], short_name=kwargs['short_name'],
|
full_name=kwargs['full_name'], short_name=kwargs['short_name'],
|
||||||
is_realm_admin=True, **pass_kwargs)
|
role=UserProfile.ROLE_REALM_ADMINISTRATOR, **pass_kwargs)
|
||||||
|
|
||||||
def create_stream_with_recipient(self, **kwargs: Any) -> Tuple[Stream, Recipient]:
|
def create_stream_with_recipient(self, **kwargs: Any) -> Tuple[Stream, Recipient]:
|
||||||
self.name_counter += 1
|
self.name_counter += 1
|
||||||
|
|
|
@ -498,8 +498,7 @@ def create_users(realm: Realm, name_list: Iterable[Tuple[str, str]], bot_type: O
|
||||||
bulk_create_users(realm, user_set, bot_type)
|
bulk_create_users(realm, user_set, bot_type)
|
||||||
|
|
||||||
def do_create_user(email: str, password: Optional[str], realm: Realm, full_name: str,
|
def do_create_user(email: str, password: Optional[str], realm: Realm, full_name: str,
|
||||||
short_name: str, bot_type: Optional[int]=None,
|
short_name: str, bot_type: Optional[int]=None, role: Optional[int]=None,
|
||||||
is_realm_admin: bool=False, is_guest: bool=False,
|
|
||||||
bot_owner: Optional[UserProfile]=None, tos_version: Optional[str]=None,
|
bot_owner: Optional[UserProfile]=None, tos_version: Optional[str]=None,
|
||||||
timezone: str="", avatar_source: str=UserProfile.AVATAR_FROM_GRAVATAR,
|
timezone: str="", avatar_source: str=UserProfile.AVATAR_FROM_GRAVATAR,
|
||||||
default_sending_stream: Optional[Stream]=None,
|
default_sending_stream: Optional[Stream]=None,
|
||||||
|
@ -513,8 +512,7 @@ def do_create_user(email: str, password: Optional[str], realm: Realm, full_name:
|
||||||
|
|
||||||
user_profile = create_user(email=email, password=password, realm=realm,
|
user_profile = create_user(email=email, password=password, realm=realm,
|
||||||
full_name=full_name, short_name=short_name,
|
full_name=full_name, short_name=short_name,
|
||||||
is_realm_admin=is_realm_admin, is_guest=is_guest,
|
role=role, bot_type=bot_type, bot_owner=bot_owner,
|
||||||
bot_type=bot_type, bot_owner=bot_owner,
|
|
||||||
tos_version=tos_version, timezone=timezone, avatar_source=avatar_source,
|
tos_version=tos_version, timezone=timezone, avatar_source=avatar_source,
|
||||||
default_sending_stream=default_sending_stream,
|
default_sending_stream=default_sending_stream,
|
||||||
default_events_register_stream=default_events_register_stream,
|
default_events_register_stream=default_events_register_stream,
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
from django.contrib.auth.models import UserManager
|
from django.contrib.auth.models import UserManager
|
||||||
from django.utils.timezone import now as timezone_now
|
from django.utils.timezone import now as timezone_now
|
||||||
from zerver.models import UserProfile, Recipient, Subscription, Realm, Stream, \
|
from zerver.models import UserProfile, Recipient, Subscription, Realm, Stream, \
|
||||||
get_fake_email_domain
|
PreregistrationUser, get_fake_email_domain
|
||||||
from zerver.lib.upload import copy_avatar
|
from zerver.lib.upload import copy_avatar
|
||||||
from zerver.lib.hotspots import copy_hotpots
|
from zerver.lib.hotspots import copy_hotpots
|
||||||
from zerver.lib.utils import generate_api_key
|
from zerver.lib.utils import generate_api_key
|
||||||
|
@ -36,6 +36,13 @@ def get_display_email_address(user_profile: UserProfile, realm: Realm) -> str:
|
||||||
return "user%s@%s" % (user_profile.id, get_fake_email_domain())
|
return "user%s@%s" % (user_profile.id, get_fake_email_domain())
|
||||||
return user_profile.delivery_email
|
return user_profile.delivery_email
|
||||||
|
|
||||||
|
def get_role_for_new_user(invited_as: int, realm_creation: bool=False) -> int:
|
||||||
|
if invited_as == PreregistrationUser.INVITE_AS['REALM_ADMIN'] or realm_creation:
|
||||||
|
return UserProfile.ROLE_REALM_ADMINISTRATOR
|
||||||
|
elif invited_as == PreregistrationUser.INVITE_AS['GUEST_USER']:
|
||||||
|
return UserProfile.ROLE_GUEST
|
||||||
|
return UserProfile.ROLE_MEMBER
|
||||||
|
|
||||||
# create_user_profile is based on Django's User.objects.create_user,
|
# create_user_profile is based on Django's User.objects.create_user,
|
||||||
# except that we don't save to the database so it can used in
|
# except that we don't save to the database so it can used in
|
||||||
# bulk_creates
|
# bulk_creates
|
||||||
|
@ -76,8 +83,7 @@ def create_user_profile(realm: Realm, email: str, password: Optional[str],
|
||||||
|
|
||||||
def create_user(email: str, password: Optional[str], realm: Realm,
|
def create_user(email: str, password: Optional[str], realm: Realm,
|
||||||
full_name: str, short_name: str, active: bool = True,
|
full_name: str, short_name: str, active: bool = True,
|
||||||
is_realm_admin: bool = False,
|
role: Optional[int] = None,
|
||||||
is_guest: bool = False,
|
|
||||||
bot_type: Optional[int] = None,
|
bot_type: Optional[int] = None,
|
||||||
bot_owner: Optional[UserProfile] = None,
|
bot_owner: Optional[UserProfile] = None,
|
||||||
tos_version: Optional[str] = None, timezone: str = "",
|
tos_version: Optional[str] = None, timezone: str = "",
|
||||||
|
@ -90,14 +96,12 @@ def create_user(email: str, password: Optional[str], realm: Realm,
|
||||||
user_profile = create_user_profile(realm, email, password, active, bot_type,
|
user_profile = create_user_profile(realm, email, password, active, bot_type,
|
||||||
full_name, short_name, bot_owner,
|
full_name, short_name, bot_owner,
|
||||||
is_mirror_dummy, tos_version, timezone)
|
is_mirror_dummy, tos_version, timezone)
|
||||||
if is_realm_admin:
|
|
||||||
user_profile.role = UserProfile.ROLE_REALM_ADMINISTRATOR
|
|
||||||
if is_guest:
|
|
||||||
user_profile.role = UserProfile.ROLE_GUEST
|
|
||||||
user_profile.avatar_source = avatar_source
|
user_profile.avatar_source = avatar_source
|
||||||
user_profile.timezone = timezone
|
user_profile.timezone = timezone
|
||||||
user_profile.default_sending_stream = default_sending_stream
|
user_profile.default_sending_stream = default_sending_stream
|
||||||
user_profile.default_events_register_stream = default_events_register_stream
|
user_profile.default_events_register_stream = default_events_register_stream
|
||||||
|
if role is not None:
|
||||||
|
user_profile.role = role
|
||||||
# Allow the ORM default to be used if not provided
|
# Allow the ORM default to be used if not provided
|
||||||
if default_all_public_streams is not None:
|
if default_all_public_streams is not None:
|
||||||
user_profile.default_all_public_streams = default_all_public_streams
|
user_profile.default_all_public_streams = default_all_public_streams
|
||||||
|
|
|
@ -12,8 +12,7 @@ from zerver.context_processors import get_realm_from_request, login_context
|
||||||
from zerver.models import UserProfile, Realm, Stream, MultiuseInvite, \
|
from zerver.models import UserProfile, Realm, Stream, MultiuseInvite, \
|
||||||
name_changes_disabled, email_to_username, \
|
name_changes_disabled, email_to_username, \
|
||||||
get_realm, get_user_by_delivery_email, get_default_stream_groups, DisposableEmailError, \
|
get_realm, get_user_by_delivery_email, get_default_stream_groups, DisposableEmailError, \
|
||||||
DomainNotAllowedForRealmError, get_source_profile, EmailContainsPlusError, \
|
DomainNotAllowedForRealmError, get_source_profile, EmailContainsPlusError
|
||||||
PreregistrationUser
|
|
||||||
from zerver.lib.email_validation import email_allowed_for_realm, \
|
from zerver.lib.email_validation import email_allowed_for_realm, \
|
||||||
validate_email_not_already_in_realm
|
validate_email_not_already_in_realm
|
||||||
from zerver.lib.send_email import send_email, FromAddress
|
from zerver.lib.send_email import send_email, FromAddress
|
||||||
|
@ -25,6 +24,7 @@ from zerver.forms import RegistrationForm, HomepageForm, RealmCreationForm, \
|
||||||
from django_auth_ldap.backend import LDAPBackend, _LDAPUser
|
from django_auth_ldap.backend import LDAPBackend, _LDAPUser
|
||||||
from zerver.decorator import require_post, \
|
from zerver.decorator import require_post, \
|
||||||
do_login
|
do_login
|
||||||
|
from zerver.lib.create_user import get_role_for_new_user
|
||||||
from zerver.lib.onboarding import send_initial_realm_messages, setup_realm_internal_bots
|
from zerver.lib.onboarding import send_initial_realm_messages, setup_realm_internal_bots
|
||||||
from zerver.lib.sessions import get_expirable_session_var
|
from zerver.lib.sessions import get_expirable_session_var
|
||||||
from zerver.lib.subdomains import get_subdomain, is_root_domain_available
|
from zerver.lib.subdomains import get_subdomain, is_root_domain_available
|
||||||
|
@ -88,8 +88,8 @@ def accounts_register(request: HttpRequest) -> HttpResponse:
|
||||||
email = prereg_user.email
|
email = prereg_user.email
|
||||||
realm_creation = prereg_user.realm_creation
|
realm_creation = prereg_user.realm_creation
|
||||||
password_required = prereg_user.password_required
|
password_required = prereg_user.password_required
|
||||||
is_realm_admin = prereg_user.invited_as == PreregistrationUser.INVITE_AS['REALM_ADMIN'] or realm_creation
|
|
||||||
is_guest = prereg_user.invited_as == PreregistrationUser.INVITE_AS['GUEST_USER']
|
role = get_role_for_new_user(prereg_user.invited_as, realm_creation)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
validators.validate_email(email)
|
validators.validate_email(email)
|
||||||
|
@ -338,8 +338,7 @@ def accounts_register(request: HttpRequest) -> HttpResponse:
|
||||||
if user_profile is None:
|
if user_profile is None:
|
||||||
user_profile = do_create_user(email, password, realm, full_name, short_name,
|
user_profile = do_create_user(email, password, realm, full_name, short_name,
|
||||||
prereg_user=prereg_user,
|
prereg_user=prereg_user,
|
||||||
is_realm_admin=is_realm_admin,
|
role=role,
|
||||||
is_guest=is_guest,
|
|
||||||
tos_version=settings.TOS_VERSION,
|
tos_version=settings.TOS_VERSION,
|
||||||
timezone=timezone,
|
timezone=timezone,
|
||||||
newsletter_data={"IP": request.META['REMOTE_ADDR']},
|
newsletter_data={"IP": request.META['REMOTE_ADDR']},
|
||||||
|
|
|
@ -18,7 +18,7 @@ class Command(ZulipBaseCommand):
|
||||||
name = '%02d-user' % (
|
name = '%02d-user' % (
|
||||||
UserProfile.objects.filter(email__contains='user@').count(),)
|
UserProfile.objects.filter(email__contains='user@').count(),)
|
||||||
user = do_create_user('%s@%s.zulip.com' % (name, string_id),
|
user = do_create_user('%s@%s.zulip.com' % (name, string_id),
|
||||||
'password', realm, name, name, is_realm_admin=True)
|
'password', realm, name, name, role=UserProfile.ROLE_REALM_ADMINISTRATOR)
|
||||||
bulk_add_subscriptions([realm.signup_notifications_stream], [user])
|
bulk_add_subscriptions([realm.signup_notifications_stream], [user])
|
||||||
|
|
||||||
send_initial_realm_messages(realm)
|
send_initial_realm_messages(realm)
|
||||||
|
|
|
@ -57,6 +57,7 @@ from zerver.lib.actions import do_create_user, do_reactivate_user, do_deactivate
|
||||||
do_update_user_custom_profile_data_if_changed
|
do_update_user_custom_profile_data_if_changed
|
||||||
from zerver.lib.avatar import is_avatar_new, avatar_url
|
from zerver.lib.avatar import is_avatar_new, avatar_url
|
||||||
from zerver.lib.avatar_hash import user_avatar_content_hash
|
from zerver.lib.avatar_hash import user_avatar_content_hash
|
||||||
|
from zerver.lib.create_user import get_role_for_new_user
|
||||||
from zerver.lib.dev_ldap_directory import init_fakeldap
|
from zerver.lib.dev_ldap_directory import init_fakeldap
|
||||||
from zerver.lib.email_validation import email_allowed_for_realm, \
|
from zerver.lib.email_validation import email_allowed_for_realm, \
|
||||||
validate_email_not_already_in_realm
|
validate_email_not_already_in_realm
|
||||||
|
@ -706,9 +707,7 @@ class ZulipLDAPAuthBackend(ZulipLDAPAuthBackendBase):
|
||||||
invited_as = self._prereg_user.invited_as
|
invited_as = self._prereg_user.invited_as
|
||||||
realm_creation = self._prereg_user.realm_creation
|
realm_creation = self._prereg_user.realm_creation
|
||||||
opts['prereg_user'] = self._prereg_user
|
opts['prereg_user'] = self._prereg_user
|
||||||
opts['is_realm_admin'] = (
|
opts['role'] = get_role_for_new_user(invited_as, realm_creation)
|
||||||
invited_as == PreregistrationUser.INVITE_AS['REALM_ADMIN']) or realm_creation
|
|
||||||
opts['is_guest'] = invited_as == PreregistrationUser.INVITE_AS['GUEST_USER']
|
|
||||||
opts['realm_creation'] = realm_creation
|
opts['realm_creation'] = realm_creation
|
||||||
# TODO: Ideally, we should add a mechanism for the user
|
# TODO: Ideally, we should add a mechanism for the user
|
||||||
# entering which default stream groups they've selected in
|
# entering which default stream groups they've selected in
|
||||||
|
|
Loading…
Reference in New Issue