Commit 461341ed authored by Daniel Klaffenbach's avatar Daniel Klaffenbach 🐍

Use a restartable LDAP connection to avoid errors

parent 38a9c459
Pipeline #3900 passed with stage
in 34 seconds
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import ssl
from ldap3 import Server, Connection
from ldap3 import Server, Connection, STRATEGY_SYNC_RESTARTABLE
from ldap3.core.tls import Tls
from ldap3.utils.uri import parse_uri
from django.conf import settings
......@@ -27,13 +27,18 @@ class Ldap(object):
def connection(self):
tls = Tls(ca_certs_file="/etc/pki/tls/certs/ca-bundle.crt", validate=ssl.CERT_REQUIRED)
s=Server(self.LDAP_PARAMS['host'], use_ssl=True, tls=tls)
c = Connection(s, auto_bind=True)
c = Connection(
s,
auto_bind=True,
# Make the connection reusable
client_strategy=STRATEGY_SYNC_RESTARTABLE,
)
return c
def get_attributes(self, username):
conn = self.connection
model_attrs = {}
if conn.search(self.LDAP_PARAMS['base'], '(uid=%s)' %username, attributes=tuple(self.LDAP_SYNC_USER_ATTRIBUTES)):
if conn.search(self.LDAP_PARAMS['base'], '(uid=%s)' %username, attributes=self.LDAP_SYNC_USER_ATTRIBUTES.keys()):
for attr in self.LDAP_SYNC_USER_ATTRIBUTES:
if attr in conn.response[0]['attributes']:
model_attrs[self.LDAP_SYNC_USER_ATTRIBUTES[attr]] = conn.response[0]['attributes'][attr][0]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment