60 lines
2.0 KiB
Python
60 lines
2.0 KiB
Python
from threading import Lock
|
|
from ldap3.utils.log import set_library_log_detail_level, PROTOCOL
|
|
|
|
from phi.logging import get_logger
|
|
from phi.ldap.connection import make_connection
|
|
from phi.ldap.connection import open_connection, close_connection
|
|
|
|
log = get_logger(__name__)
|
|
set_library_log_detail_level(PROTOCOL)
|
|
|
|
|
|
class Client:
|
|
def __init__(self,
|
|
host=None, port=389,
|
|
encryption=None, ciphers=None, validate=False, ca_certs=None,
|
|
username=None, password=None,
|
|
base_dn=None,
|
|
attribute_id='uid', attribute_mail='mail'):
|
|
log.info("Initializing LDAP Client.")
|
|
|
|
self.host = host
|
|
self.port = port
|
|
|
|
self.encryption = encryption
|
|
self.ciphers = ciphers
|
|
self.validate = validate
|
|
self.ca_certs = ca_certs
|
|
|
|
self.username = username
|
|
self.password = password
|
|
|
|
self.base_dn = base_dn
|
|
|
|
self.attribute_id = attribute_id
|
|
self.attribute_mail = attribute_mail
|
|
|
|
self.connection_lock = Lock()
|
|
self.connection = make_connection(host=self.host, port=self.port,
|
|
encryption=self.encryption,
|
|
ciphers=self.ciphers,
|
|
validate=self.validate,
|
|
ca_certs=self.ca_certs,
|
|
username=self.username,
|
|
password=self.password)
|
|
|
|
def open(self):
|
|
self.connection_lock.acquire()
|
|
if self.connection.closed is True:
|
|
open_connection(self.connection)
|
|
self.connection_lock.release()
|
|
else:
|
|
self.connection_lock.release()
|
|
raise Exception("Trying to open a connection, "
|
|
"but it is already open.")
|
|
|
|
def close(self):
|
|
self.connection_lock.acquire()
|
|
close_connection(self.connection)
|
|
self.connection_lock.release()
|