Skip to content

Commit

Permalink
Add a setting to override the username field
Browse files Browse the repository at this point in the history
  • Loading branch information
jbittel committed Feb 3, 2016
1 parent 2720f3f commit 6b56141
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 9 additions & 1 deletion docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Settings
The root of the LDAP tree to search for user account information. The
contents of this tree can be further refined using the filtering settings.
This should be a string specifying the complete root path::

LDAP_SYNC_BASE = "OU=Users,DC=example,DC=com"

.. attribute:: LDAP_SYNC_BASE_USER
Expand Down Expand Up @@ -73,6 +73,14 @@ Settings
"mail": "email",
}

.. attribute:: LDAP_SYNC_USERNAME_FIELD

:default: ``None``

An optional field on the synchronized User model to use as the unique key for
each user. If not specified, the User model's ``USERNAME_FIELD`` will be used.
If specified, the field must be included in ``LDAP_SYNC_USER_ATTRIBUTES``.

.. attribute:: LDAP_SYNC_GROUP_FILTER

:default: ``""``
Expand Down
8 changes: 7 additions & 1 deletion ldap_sync/management/commands/syncldap.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ def sync_ldap_users(self, ldap_users):
"""
model = get_user_model()
attributes = getattr(settings, 'LDAP_SYNC_USER_ATTRIBUTES', None)
username_field = getattr(model, 'USERNAME_FIELD', 'username')
username_field = getattr(settings, 'LDAP_SYNC_USERNAME_FIELD', None)
if username_field is None:
username_field = getattr(model, 'USERNAME_FIELD', 'username')

if not model._meta.get_field(username_field).unique:
error_msg = ("Username field '%s' must be unique" % username_field)
raise ImproperlyConfigured(error_msg)

if username_field not in attributes.values():
error_msg = ("LDAP_SYNC_USER_ATTRIBUTES must contain the "
Expand Down

0 comments on commit 6b56141

Please sign in to comment.