From afdd58f9b99811ad67b4638818cb5a22aa752f34 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Thu, 1 Apr 2021 17:36:54 +0200 Subject: [PATCH] views: use LDAPBackendPasswordLost to switch to LDAP account (#43585) --- src/authentic2/views.py | 3 +++ tests/test_ldap.py | 24 +++++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/authentic2/views.py b/src/authentic2/views.py index ad3c8acb..383726a1 100644 --- a/src/authentic2/views.py +++ b/src/authentic2/views.py @@ -1362,6 +1362,9 @@ class SuView(View): user = switch_user.resolve_token(uuid) if not user: raise Http404 + # LDAP ad-hoc behaviour + if user.userexternalid_set.exists(): + user = utils.authenticate(request, user=user) return utils.simulate_authentication(request, user, 'su') diff --git a/tests/test_ldap.py b/tests/test_ldap.py index ef367cab..ecd00681 100644 --- a/tests/test_ldap.py +++ b/tests/test_ldap.py @@ -36,7 +36,7 @@ from authentic2.a2_rbac.models import Role from authentic2.a2_rbac.utils import get_default_ou from authentic2.backends import ldap_backend from authentic2.models import Service -from authentic2.utils import authenticate +from authentic2.utils import authenticate, switch_user from django_rbac.utils import get_ou_model from . import utils @@ -1652,3 +1652,25 @@ def test_config_to_lowercase(): } ], } + + +def test_switch_user_ldap_user(slapd, settings, app, db): + settings.LDAP_AUTH_SETTINGS = [ + { + 'url': [slapd.ldap_url], + 'binddn': force_text(slapd.root_bind_dn), + 'bindpw': force_text(slapd.root_bind_password), + 'basedn': u'o=ôrga', + 'use_tls': False, + 'attributes': ['carLicense'], + } + ] + # get all users + management.call_command('sync-ldap-users', verbosity=2) + + user = User.objects.get(username=USERNAME + '@ldap') + url = switch_user.build_url(user) + response = app.get(url).follow() + assert app.session['_auth_user_backend'] == 'authentic2.backends.ldap_backend.LDAPBackendPasswordLost' + template_user = response.context['user'] + assert 'carlicense' in template_user.get_attributes(object(), {}) -- 2.30.1