Browse Source

[Update] 解决ldap映射is_active等字段为bool值的问题 (#2716) (#2721)

pull/2728/head
xiaomao 6 years ago committed by 老广
parent
commit
b791073802
  1. 14
      apps/authentication/backends/ldap.py
  2. 9
      apps/settings/utils.py

14
apps/authentication/backends/ldap.py

@ -86,7 +86,18 @@ class LDAPUser(_LDAPUser):
return user_dn return user_dn
def _populate_user_from_attributes(self): 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 hasattr(self._user, 'email') or '@' not in self._user.email:
if '@' not in self._user.username: if '@' not in self._user.username:
email = '{}@{}'.format(self._user.username, settings.EMAIL_SUFFIX) email = '{}@{}'.format(self._user.username, settings.EMAIL_SUFFIX)
@ -95,4 +106,3 @@ class LDAPUser(_LDAPUser):
setattr(self._user, 'email', email) setattr(self._user, 'email', email)

9
apps/settings/utils.py

@ -60,6 +60,8 @@ class LDAPUtil:
for field, value in user_item.items(): for field, value in user_item.items():
if not hasattr(user, field): if not hasattr(user, field):
continue continue
if isinstance(getattr(user, field), bool):
value = bool(int(value == 'true'))
setattr(user, field, value) setattr(user, field, value)
user.save() user.save()
@ -82,6 +84,13 @@ class LDAPUtil:
@staticmethod @staticmethod
def create_user(user_item): 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 user_item['source'] = User.SOURCE_LDAP
try: try:
User.objects.create(**user_item) User.objects.create(**user_item)

Loading…
Cancel
Save