Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

mateuszmandera
Copy link

@mateuszmandera mateuszmandera commented Oct 19, 2019

It seems like a fairly common scenario for an application to want to have the django username
constructed from the attributes loaded from ldap, rather than directly the ldap username as is currently allowed by the ldap_to_django_username hook.

This doesn't include updating the docs - but I'll add that after review of the code, once the final form of how this functionality should be structured is known.

This is a backward compatible change to supercede the
ldap_to_django_username hook with get_django_username_from_ldap_user
which takes _LDAPUser instance as argument and returns the django
username.
By default it just calls ldap_to_django_username(ldap_user._username) to
be compatible with code that's based on older versions of this library
and expects ldap_to_django_username to be the function translating from
ldap username to django.
New code can now in their LDAPBackend subclasses override
get_django_username_from_ldap_user with potentially more sophisticated
logic that constructs the django username from information available
in the _LDAPUser object - typically from the attributes.

This is a backward compatible change to supercede the
ldap_to_django_username hook with get_django_username_from_ldap_user
which takes _LDAPUser instance as argument and returns the django
username.
By default it just calls ldap_to_django_username(ldap_user._username) to
be compatible with code that's based on older versions of this library
and expects ldap_to_django_username to be the function translating from
ldap username to django.
New code can now in their LDAPBackend subclasses override
get_django_username_from_ldap_user with potentially more sophisticated
logic that constructs the django username from information available
in the _LDAPUser object - typically from the attributes.
timabbott pushed a commit to zulip/zulip that referenced this pull request Oct 22, 2019
Fixes #11878

Instead of a confusing mix of django_auth_backed applying
ldap_to_django_username in its internals for one part of the
translation, and then custom logic for grabbing it from the email
attribute of the ldapuser in ZulipLDAPAuthBackend.get_or_build_user
for the second part of the translation,
we put all the logic in a single function user_email_from_ldapuser
which will be used by get_or_build of both ZulipLDAPUserPopulator and
ZulipLDAPAuthBackend.

This, building on the previous commits with the email search feature,
fixes the ldap sync bug from issue #11878.

If we can get upstream django-auth-ldap to merge
django-auth-ldap/django-auth-ldap#154, we'll
be able to go back to using the version of ldap_to_django_username
that accepts a _LDAPUser object.
evo42 pushed a commit to evo42/zulip that referenced this pull request Oct 30, 2019
Fixes zulip#11878

Instead of a confusing mix of django_auth_backed applying
ldap_to_django_username in its internals for one part of the
translation, and then custom logic for grabbing it from the email
attribute of the ldapuser in ZulipLDAPAuthBackend.get_or_build_user
for the second part of the translation,
we put all the logic in a single function user_email_from_ldapuser
which will be used by get_or_build of both ZulipLDAPUserPopulator and
ZulipLDAPAuthBackend.

This, building on the previous commits with the email search feature,
fixes the ldap sync bug from issue zulip#11878.

If we can get upstream django-auth-ldap to merge
django-auth-ldap/django-auth-ldap#154, we'll
be able to go back to using the version of ldap_to_django_username
that accepts a _LDAPUser object.
YashRE42 pushed a commit to YashRE42/zulip that referenced this pull request Dec 12, 2019
Fixes zulip#11878

Instead of a confusing mix of django_auth_backed applying
ldap_to_django_username in its internals for one part of the
translation, and then custom logic for grabbing it from the email
attribute of the ldapuser in ZulipLDAPAuthBackend.get_or_build_user
for the second part of the translation,
we put all the logic in a single function user_email_from_ldapuser
which will be used by get_or_build of both ZulipLDAPUserPopulator and
ZulipLDAPAuthBackend.

This, building on the previous commits with the email search feature,
fixes the ldap sync bug from issue zulip#11878.

If we can get upstream django-auth-ldap to merge
django-auth-ldap/django-auth-ldap#154, we'll
be able to go back to using the version of ldap_to_django_username
that accepts a _LDAPUser object.
@francoisfreitag
Copy link
Member

AFAICT, there must be a symmetry between ldap_to_django_username and django_to_ldap_username. With this change, the symmetry is lost.

django-auth-ldap makes the assumption that a Django username can be transformed to an str that matches an LDAP username (and uniquely identifies an LDAP user). With this change, when an _LDAPUser is constructed from an authenticated user object, the _username/ldap_username will no longer necessarily correspond to a single entry in the LDAP.

Breaking this assumption has implications in the library, for example, using the dn property causes to search the LDAP for a user with that username and expects only one result. The username is also used to identify the user and lookup data from the cache.


Can you explain the motivation for this change?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants