Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
ldap: Fix dev/test-specific bugs with AUTH_LDAP_USER_FLAGS_BY_GROUP.
Mateusz Mandera4dd1826532e3
diff --git a/zproject/backends.py b/zproject/backends.pyindex f60aebb4516..65ef21d0f17 100644--- a/zproject/backends.py+++ b/zproject/backends.py@@ -35,7 +35,7 @@ from django.http import HttpRequest, HttpResponse, HttpResponseRedirectfrom django.shortcuts import renderfrom django.urls import reversefrom django.utils.translation import gettext as _-from django_auth_ldap.backend import LDAPBackend, _LDAPUser, ldap_error+from django_auth_ldap.backend import LDAPBackend, _LDAPUser, _LDAPUserGroups, ldap_errorfrom lxml.etree import XMLSyntaxErrorfrom onelogin.saml2 import compat as onelogin_saml2_compatfrom onelogin.saml2.auth import OneLogin_Saml2_Auth@@ -1062,12 +1062,23 @@ class ZulipLDAPAuthBackend(ZulipLDAPAuthBackendBase):return_data["no_matching_ldap_user"] = Truereturn None- # Call into (ultimately) the django-auth-ldap authenticate- # function. This will check the username/password pair+ # Now we would want to run the django-auth-ldap authenticate+ # function. We're, however, forced to fork it here in order+ # to use our own ZulipLDAPUser class instead of its _LDAPUser.+ #+ # This will check the username/password pair# against the LDAP database, and assuming those are correct,# end up calling `self.get_or_build_user` with the# authenticated user's data from LDAP.- return super().authenticate(request=request, username=username, password=password)++ if not password:+ self.logger.debug("Rejecting empty password for %s", username)+ return None++ ldap_user = ZulipLDAPUser(self, username=username.strip(), request=request, realm=realm)+ user = self.authenticate_ldap_user(ldap_user, password)++ return userdef get_or_build_user(self, username: str, ldap_user: _LDAPUser) -> tuple[UserProfile, bool]:"""The main function of our authentication backend extension of@@ -1197,6 +1208,25 @@ class ZulipLDAPUser(_LDAPUser):super().__init__(*args, **kwargs)+ def _get_groups(self) -> _LDAPUserGroups:+ groups = super()._get_groups()+ if settings.DEVELOPMENT:+ # HACK: We force the loading of all group dns, to coerce+ # django-auth-ldap into caching the result - which+ # makes it use this result when running is_member_of+ # to check if the user is a member of a specific group.+ #+ # When this result isn't already loaded, django-auth-ldap+ # runs a different ldap query, attempting to query for membership+ # in the specific member group - which doesn't work in our fakeldap+ # setup for mocking LDAP in tests and development.+ #+ # This hack is necessary for us to be able to test+ # AUTH_LDAP_USER_FLAGS_BY_GROUP functionality.+ groups.get_group_dns()++ return groups+class ZulipLDAPUserPopulator(ZulipLDAPAuthBackendBase):"""Just like ZulipLDAPAuthBackend, but doesn't let you log in. Used