# Buggy code
def get_or_create_user(self, username, ldap_user):
    # type: (str, _LDAPUser) -> Tuple[UserProfile, bool]
    try:
        user_profile = get_user_profile_by_email(username)
        if not user_profile.is_active or user_profile.realm.deactivated:
            raise ZulipLDAPException("Realm has been deactivated")
        if not ldap_auth_enabled(user_profile.realm):
            raise ZulipLDAPException("LDAP Authentication is not enabled")
        return user_profile, False
    except UserProfile.DoesNotExist:
        # No need to check for an inactive user since they don't exist yet
        if self._realm.deactivated:
            raise ZulipLDAPException("Realm has been deactivated")

        full_name_attr = settings.AUTH_LDAP_USER_ATTR_MAP["full_name"]
        short_name = full_name = ldap_user.attrs[full_name_attr][0]
        try:
            full_name = check_full_name(full_name)
        except JsonableError as e:
            raise ZulipLDAPException(e.error)
        if "short_name" in settings.AUTH_LDAP_USER_ATTR_MAP:
            short_name_attr = settings.AUTH_LDAP_USER_ATTR_MAP["short_name"]
            short_name = ldap_user.attrs[short_name_attr][0]

        user_profile = do_create_user(
            username, None, self._realm, full_name, short_name
        )
        return user_profile, True


# Traceback (AttributeError)
2017-06-16 02:56:29,703 ERROR    Internal Server Error: /accounts/login/
Traceback (most recent call last):
File "/home/zulip/deployments/2017-06-05-14-27-45/zulip-venv/lib/python2.7/site-packages/django/core/handlers/exception.py", line 42, in inner
response = get_response(request)
File "/home/zulip/deployments/2017-06-05-14-27-45/zulip-venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "/home/zulip/deployments/2017-06-05-14-27-45/zulip-venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/zulip/deployments/2017-06-05-14-27-45/zulip-venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "./zerver/views/auth.py", line 445, in login_page
extra_context=extra_context, **kwargs)
File "/home/zulip/deployments/2017-06-05-14-27-45/zulip-venv/lib/python2.7/site-packages/django/contrib/auth/views.py", line 47, in inner
return func(*args, **kwargs)
File "/home/zulip/deployments/2017-06-05-14-27-45/zulip-venv/lib/python2.7/site-packages/django/views/decorators/debug.py", line 76, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "/home/zulip/deployments/2017-06-05-14-27-45/zulip-venv/lib/python2.7/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/zulip/deployments/2017-06-05-14-27-45/zulip-venv/lib/python2.7/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home/zulip/deployments/2017-06-05-14-27-45/zulip-venv/lib/python2.7/site-packages/django/contrib/auth/views.py", line 81, in login
if form.is_valid():
File "/home/zulip/deployments/2017-06-05-14-27-45/zulip-venv/lib/python2.7/site-packages/django/forms/forms.py", line 169, in is_valid
return self.is_bound and not self.errors
File "/home/zulip/deployments/2017-06-05-14-27-45/zulip-venv/lib/python2.7/site-packages/django/forms/forms.py", line 161, in errors
self.full_clean()
File "/home/zulip/deployments/2017-06-05-14-27-45/zulip-venv/lib/python2.7/site-packages/django/forms/forms.py", line 371, in full_clean
self._clean_form()
File "/home/zulip/deployments/2017-06-05-14-27-45/zulip-venv/lib/python2.7/site-packages/django/forms/forms.py", line 398, in _clean_form
cleaned_data = self.clean()
File "/home/zulip/deployments/2017-06-05-14-27-45/zulip-venv/lib/python2.7/site-packages/django/contrib/auth/forms.py", line 191, in clean
self.user_cache = authenticate(username=username, password=password)
File "/home/zulip/deployments/2017-06-05-14-27-45/zulip-venv/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 74, in authenticate
user = backend.authenticate(**credentials)
File "./zproject/backends.py", line 422, in authenticate
user_profile = ZulipLDAPAuthBackendBase.authenticate(self, username, password)
File "/home/zulip/deployments/2017-06-05-14-27-45/zulip-venv/lib/python2.7/site-packages/django_auth_ldap/backend.py", line 169, in authenticate
user = ldap_user.authenticate(password)
File "/home/zulip/deployments/2017-06-05-14-27-45/zulip-venv/lib/python2.7/site-packages/django_auth_ldap/backend.py", line 344, in authenticate
self._get_or_create_user()
File "/home/zulip/deployments/2017-06-05-14-27-45/zulip-venv/lib/python2.7/site-packages/django_auth_ldap/backend.py", line 553, in _get_or_create_user
self._user, created = self.backend.get_or_create_user(username, self)
File "./zproject/backends.py", line 444, in get_or_create_user
if self._realm.deactivated:
AttributeError: 'NoneType' object has no attribute 'deactivated'

# Fixed code
def get_or_create_user(self, username, ldap_user):
    # type: (str, _LDAPUser) -> Tuple[UserProfile, bool]
    try:
        user_profile = get_user_profile_by_email(username)
        if not user_profile.is_active or user_profile.realm.deactivated:
            raise ZulipLDAPException("Realm has been deactivated")
        if not ldap_auth_enabled(user_profile.realm):
            raise ZulipLDAPException("LDAP Authentication is not enabled")
        return user_profile, False
    except UserProfile.DoesNotExist:
        if self._realm is None:
            raise ZulipLDAPException("Realm is None")
        # No need to check for an inactive user since they don't exist yet
        if self._realm.deactivated:
            raise ZulipLDAPException("Realm has been deactivated")

        full_name_attr = settings.AUTH_LDAP_USER_ATTR_MAP["full_name"]
        short_name = full_name = ldap_user.attrs[full_name_attr][0]
        try:
            full_name = check_full_name(full_name)
        except JsonableError as e:
            raise ZulipLDAPException(e.error)
        if "short_name" in settings.AUTH_LDAP_USER_ATTR_MAP:
            short_name_attr = settings.AUTH_LDAP_USER_ATTR_MAP["short_name"]
            short_name = ldap_user.attrs[short_name_attr][0]

        user_profile = do_create_user(
            username, None, self._realm, full_name, short_name
        )
        return user_profile, True