From a4c3224328f09feff5c9e958e277f99d874d81fe Mon Sep 17 00:00:00 2001 From: sahil839 Date: Fri, 30 Apr 2021 18:07:48 +0530 Subject: [PATCH] users: Remove redundant get_role_for_new_user in lib/create_user.py. The function get_role_for_new_user was added to get role from the invited_as value, as invited_as values were one of (1,2,3,4) previously, but it was then changed to be the actual role value, i.e. one of (100, 200, 400, 600), in 1f8f227444. So, we can safely remove this function now and use invited_as value directly and handle realm_creation case by using an if condition. --- zerver/lib/create_user.py | 20 +------------------- zerver/views/registration.py | 5 +++-- zproject/backends.py | 7 +++++-- 3 files changed, 9 insertions(+), 23 deletions(-) diff --git a/zerver/lib/create_user.py b/zerver/lib/create_user.py index 69a24ea30f..d63c39e7b4 100644 --- a/zerver/lib/create_user.py +++ b/zerver/lib/create_user.py @@ -7,15 +7,7 @@ from django.utils.timezone import now as timezone_now from zerver.lib.hotspots import copy_hotspots from zerver.lib.upload import copy_avatar from zerver.lib.utils import generate_api_key -from zerver.models import ( - PreregistrationUser, - Realm, - Recipient, - Stream, - Subscription, - UserProfile, - get_fake_email_domain, -) +from zerver.models import Realm, Recipient, Stream, Subscription, UserProfile, get_fake_email_domain def copy_user_settings(source_profile: UserProfile, target_profile: UserProfile) -> None: @@ -57,16 +49,6 @@ def get_display_email_address(user_profile: UserProfile) -> str: return user_profile.delivery_email -def get_role_for_new_user(invited_as: int, realm_creation: bool = False) -> int: - if realm_creation or invited_as == PreregistrationUser.INVITE_AS["REALM_OWNER"]: - return UserProfile.ROLE_REALM_OWNER - elif invited_as == PreregistrationUser.INVITE_AS["REALM_ADMIN"]: - 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, # except that we don't save to the database so it can used in # bulk_creates diff --git a/zerver/views/registration.py b/zerver/views/registration.py index de68a00f93..dc6bf5ccd9 100644 --- a/zerver/views/registration.py +++ b/zerver/views/registration.py @@ -45,7 +45,6 @@ from zerver.lib.actions import ( do_set_user_display_setting, lookup_default_stream_groups, ) -from zerver.lib.create_user import get_role_for_new_user from zerver.lib.email_validation import email_allowed_for_realm, validate_email_not_already_in_realm from zerver.lib.onboarding import send_initial_realm_messages, setup_realm_internal_bots from zerver.lib.pysa import mark_sanitized @@ -134,7 +133,9 @@ def accounts_register(request: HttpRequest) -> HttpResponse: realm_creation = prereg_user.realm_creation password_required = prereg_user.password_required - role = get_role_for_new_user(prereg_user.invited_as, realm_creation) + role = prereg_user.invited_as + if realm_creation: + role = UserProfile.ROLE_REALM_OWNER try: validators.validate_email(email) diff --git a/zproject/backends.py b/zproject/backends.py index be442fbd4a..ad827f8c62 100644 --- a/zproject/backends.py +++ b/zproject/backends.py @@ -64,7 +64,6 @@ from zerver.lib.actions import ( ) from zerver.lib.avatar import avatar_url, is_avatar_new 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.email_validation import email_allowed_for_realm, validate_email_not_already_in_realm from zerver.lib.mobile_auth_otp import is_valid_otp @@ -863,7 +862,11 @@ class ZulipLDAPAuthBackend(ZulipLDAPAuthBackendBase): invited_as = self._prereg_user.invited_as realm_creation = self._prereg_user.realm_creation opts["prereg_user"] = self._prereg_user - opts["role"] = get_role_for_new_user(invited_as, realm_creation) + + opts["role"] = invited_as + if realm_creation: + opts["role"] = UserProfile.ROLE_REALM_OWNER + opts["realm_creation"] = realm_creation # TODO: Ideally, we should add a mechanism for the user # entering which default stream groups they've selected in