From b79107380226f14324918794ac8832f001e6e766 Mon Sep 17 00:00:00 2001 From: xiaomao Date: Thu, 23 May 2019 18:55:28 +0800 Subject: [PATCH] =?UTF-8?q?[Update]=20=E8=A7=A3=E5=86=B3ldap=E6=98=A0?= =?UTF-8?q?=E5=B0=84is=5Factive=E7=AD=89=E5=AD=97=E6=AE=B5=E4=B8=BAbool?= =?UTF-8?q?=E5=80=BC=E7=9A=84=E9=97=AE=E9=A2=98=20(#2716)=20(#2721)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/authentication/backends/ldap.py | 14 ++++++++++++-- apps/settings/utils.py | 9 +++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/apps/authentication/backends/ldap.py b/apps/authentication/backends/ldap.py index b35903420..2b202847b 100644 --- a/apps/authentication/backends/ldap.py +++ b/apps/authentication/backends/ldap.py @@ -86,7 +86,18 @@ class LDAPUser(_LDAPUser): return user_dn def _populate_user_from_attributes(self): - super()._populate_user_from_attributes() + for field, attr in self.settings.USER_ATTR_MAP.items(): + try: + value = self.attrs[attr][0] + except LookupError: + logger.warning("{} does not have a value for the attribute {}".format(self.dn, attr)) + else: + if not hasattr(self._user, field): + continue + if isinstance(getattr(self._user, field), bool): + value = bool(int(value == 'true')) + setattr(self._user, field, value) + if not hasattr(self._user, 'email') or '@' not in self._user.email: if '@' not in self._user.username: email = '{}@{}'.format(self._user.username, settings.EMAIL_SUFFIX) @@ -95,4 +106,3 @@ class LDAPUser(_LDAPUser): setattr(self._user, 'email', email) - diff --git a/apps/settings/utils.py b/apps/settings/utils.py index 6893974f7..56a7812e8 100644 --- a/apps/settings/utils.py +++ b/apps/settings/utils.py @@ -60,6 +60,8 @@ class LDAPUtil: for field, value in user_item.items(): if not hasattr(user, field): continue + if isinstance(getattr(user, field), bool): + value = bool(int(value == 'true')) setattr(user, field, value) user.save() @@ -82,6 +84,13 @@ class LDAPUtil: @staticmethod def create_user(user_item): + user = User() + for field, value in user_item.items(): + if not hasattr(user, field): + continue + if isinstance(getattr(user, field), bool): + value = bool(int(value == 'true')) + user_item[field] = value user_item['source'] = User.SOURCE_LDAP try: User.objects.create(**user_item)