From 18971f01a66a892795d2c8502772e9a8c61b456b Mon Sep 17 00:00:00 2001 From: catborise Date: Tue, 15 Jun 2021 13:44:05 +0300 Subject: [PATCH] fix indentation error for ldapbackend.py --- webvirtcloud/ldapbackend.py | 56 ++++++++++++++++--------------- webvirtcloud/settings.py.template | 2 +- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/webvirtcloud/ldapbackend.py b/webvirtcloud/ldapbackend.py index 2d38e8f..6f246c6 100644 --- a/webvirtcloud/ldapbackend.py +++ b/webvirtcloud/ldapbackend.py @@ -11,33 +11,35 @@ try: #/srv/webvirtcloud/ldap/ldapbackend.py class LdapAuthenticationBackend(ModelBackend): - def get_LDAP_user(self, username, password, filterString): - print('get_LDAP_user') - try: - server = Server(settings.LDAP_URL, port=settings.LDAP_PORT, - use_ssl=settings.USE_SSL,get_info=ALL) - connection = Connection(server, - settings.LDAP_MASTER_DN, - settings.LDAP_MASTER_PW, auto_bind=True) + def get_LDAP_user(self, username, password, filterString): + print('get_LDAP_user') + try: + server = Server(settings.LDAP_URL, port=settings.LDAP_PORT, + use_ssl=settings.USE_SSL,get_info=ALL) + connection = Connection(server, + settings.LDAP_MASTER_DN, + settings.LDAP_MASTER_PW, auto_bind=True) + + connection.search(settings.LDAP_ROOT_DN, + '(&({attr}={login})({filter}))'.format( + attr=settings.LDAP_USER_UID_PREFIX, + login=username, + filter=filterString), attributes=['*']) + + if len(connection.response) == 0: + print('get_LDAP_user-no response') + return None + specificUser = connection.response[0] + userDn = str(specificUser.get('raw_dn'),'utf-8') + with Connection(server, + userDn, + password) as con: + return username + except: + return None + return None - connection.search(settings.LDAP_ROOT_DN, - '(&({attr}={login})({filter}))'.format( - attr=settings.LDAP_USER_UID_PREFIX, - login=username, - filter=filterString), attributes=['*']) - - if len(connection.response) == 0: - print('get_LDAP_user-no response') - return None - specificUser = connection.response[0] - userDn = str(specificUser.get('raw_dn'),'utf-8') - with Connection(server, - userDn, - password) as con: - return username - return None - - def authenticate(self, request, username=None, password=None, **kwargs): + def authenticate(self, request, username=None, password=None, **kwargs): if not settings.LDAP_ENABLED: return None print("authenticate_ldap") @@ -96,7 +98,7 @@ try: print("authenticate-user created") return user - def get_user(self, user_id): + def get_user(self, user_id): if not settings.LDAP_ENABLED: return None print("get_user_ldap") diff --git a/webvirtcloud/settings.py.template b/webvirtcloud/settings.py.template index cc0312a..ab371d3 100644 --- a/webvirtcloud/settings.py.template +++ b/webvirtcloud/settings.py.template @@ -110,7 +110,7 @@ DATABASES = { AUTHENTICATION_BACKENDS = [ "django.contrib.auth.backends.ModelBackend", - #"webvirtcloud.ldapbackend.LdapAuthenticationBackend", + "webvirtcloud.ldapbackend.LdapAuthenticationBackend", ] LOGIN_URL = "/accounts/login/"