From 5d9ac817b2fdd54d0078d8392dd6c1cd5b5c1abf Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Mon, 17 Feb 2020 15:52:32 +0100 Subject: [PATCH 1/2] Allow login with username instead of email for LDAP. see #1798 --- modoboa/lib/authbackends.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/modoboa/lib/authbackends.py b/modoboa/lib/authbackends.py index 87bfd676d..5d01859d2 100644 --- a/modoboa/lib/authbackends.py +++ b/modoboa/lib/authbackends.py @@ -87,7 +87,15 @@ try: if group == "SimpleUsers": lpart, domain = split_mailbox(username) if domain is None: - return None + # Try to find associated email + email = None + for attr in ['mail', 'userPrincipalName']: + if attr in ldap_user.attrs: + email = ldap_user.attrs[attr][0] + break + if email is None: + return None + username = email user, created = User.objects.get_or_create( username__iexact=username, defaults={ From 9c85cba068070460f760be53afcd34477d3abeb8 Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Mon, 17 Feb 2020 16:45:09 +0100 Subject: [PATCH 2/2] Fixed test. --- modoboa/core/tests/test_ldap.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/modoboa/core/tests/test_ldap.py b/modoboa/core/tests/test_ldap.py index 1b8cfdf56..1e47f9c60 100644 --- a/modoboa/core/tests/test_ldap.py +++ b/modoboa/core/tests/test_ldap.py @@ -107,11 +107,9 @@ class LDAPAuthenticationTestCase(LDAPTestCaseMixin, ModoTestCase): self.client.logout() self.directbind_mode() - # 1: must fail because usernames of simple users must be email - # addresses username = "testuser" - with self.assertRaises(TypeError): - self.client.login(username=username, password="test") + self.client.login(username=username, password="test") + self.check_created_user(username + "@example.com") # 1: must work because usernames of domain admins are not # always email addresses