py.lib

Yohn Y. 2020-07-24 Child:84b54a8a6d4c

13:cab7fedf8432 Go to Latest

py.lib/db/ldap.py

.. Реструктуризация

History
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/db/ldap.py	Fri Jul 24 14:40:37 2020 +0300
     1.3 @@ -0,0 +1,84 @@
     1.4 +# coding: utf-8
     1.5 +
     1.6 +from ldap3 import Server, Connection, SIMPLE, SUBTREE
     1.7 +
     1.8 +LDAP_PAGE=1000
     1.9 +
    1.10 +class LdapError(Exception): pass
    1.11 +
    1.12 +class LdapRes():
    1.13 +	def __init__(self, dn, attrib):
    1.14 +		self.dn = dn
    1.15 +		self.attr = attrib
    1.16 +		
    1.17 +	def __getitem__(self, item):
    1.18 +		return self.attr[item]
    1.19 +	
    1.20 +	def __iter__(self):
    1.21 +		return iter(self.attr)
    1.22 +	
    1.23 +	def __repr__(self):
    1.24 +		return '<LdapRes: dn: %s>' % self.dn
    1.25 +	
    1.26 +	@classmethod
    1.27 +	def fromLdapQuery(cls, q):
    1.28 +		if not isinstance(q, dict):
    1.29 +			raise LdapError('LdapRes: Parsing Error, not ldap response item')
    1.30 +		if not (('dn' in q) and ('attributes' in q)):
    1.31 +			raise LdapError('LdapRes: Parsing Error, format mismatch')
    1.32 +		
    1.33 +		return cls(q['dn'], q['attributes'])
    1.34 +
    1.35 +class Ldap():
    1.36 +	def __init__(self, host, user, passwd, timeout=60, queryTimeout=300, **kwa):
    1.37 +		if 'baseDN' in kwa:
    1.38 +			self._baseDN = kwa['baseDN']
    1.39 +			del kwa['baseDN']
    1.40 +		else:
    1.41 +			self._baseDN = None
    1.42 +		ldapSrv = Server(host, connect_timeout=timeout, **kwa)
    1.43 +		self._conn = self._makeConnFabric(ldapSrv, authentication=SIMPLE,
    1.44 +			user=user, password=passwd,
    1.45 +			check_names=True, lazy=True,
    1.46 +			auto_referrals=False, raise_exceptions=True, auto_range=True
    1.47 +		)
    1.48 +		self.queryTimeout = queryTimeout
    1.49 +		
    1.50 +	def __call__(self, filter, attrib, queryTimeout=None, baseDN = None):
    1.51 +		if baseDN is None:
    1.52 +			if self._baseDN is None:
    1.53 +				raise LdapError('No base dn on query execution')
    1.54 +			baseDN = self._baseDN
    1.55 +		if queryTimeout is None:
    1.56 +			queryTimeout = self.queryTimeout
    1.57 +		try:
    1.58 +			conn = self._conn()
    1.59 +			with conn:
    1.60 +				conn.open()
    1.61 +				conn.bind()
    1.62 +			
    1.63 +				res = conn.extend.standard.paged_search(baseDN,
    1.64 +					filter, attributes=attrib, paged_size=LDAP_PAGE, generator=False,
    1.65 +					search_scope=SUBTREE, time_limit=queryTimeout
    1.66 +				)
    1.67 +
    1.68 +				for i in res:
    1.69 +					if i['type'] == 'searchResEntry':
    1.70 +						yield LdapRes.fromLdapQuery(i)
    1.71 +			
    1.72 +		except Exception as e:
    1.73 +			raise LdapError("Error on get data (%s): %s" % (type(e), str(e)), *e.args[1:])
    1.74 +		
    1.75 +	def getList(self, *a, **kwa):
    1.76 +		return [ i for i in self(*a, **kwa) ]
    1.77 +	
    1.78 +	@staticmethod
    1.79 +	def _makeConnFabric(*a, **kwa):
    1.80 +		def _func():
    1.81 +			return Connection(*a, **kwa)
    1.82 +		
    1.83 +		return _func
    1.84 +		
    1.85 +		
    1.86 +		
    1.87 +		
    1.88 \ No newline at end of file