py.lib
43:6f8bea109183
Go to Latest
py.lib/db/ldap.py
. Наведение порядка в коде логирования
3 from ldap3 import Server, Connection, SIMPLE, SUBTREE
8 class LdapError(Exception): pass
12 def __init__(self, dn, attrib):
16 def __getitem__(self, item):
17 return self.attr[item]
20 return iter(self.attr)
23 return '<LdapRes: dn: %s>' % self.dn
26 def fromLdapQuery(cls, q):
27 if not isinstance(q, dict):
28 raise LdapError('LdapRes: Parsing Error, not ldap response item')
29 if not (('dn' in q) and ('attributes' in q)):
30 raise LdapError('LdapRes: Parsing Error, format mismatch')
32 return cls(q['dn'], q['attributes'])
36 def __init__(self, host, user, passwd, timeout=60, queryTimeout=300, **kwa):
38 self._baseDN = kwa['baseDN']
42 ldapSrv = Server(host, connect_timeout=timeout, **kwa)
43 self._conn = self._makeConnFabric(ldapSrv, authentication=SIMPLE,
44 user=user, password=passwd,
45 check_names=True, lazy=True,
46 auto_referrals=False, raise_exceptions=True, auto_range=True
48 self.queryTimeout = queryTimeout
50 def __call__(self, filter, attrib, queryTimeout=None, baseDN=None):
52 if self._baseDN is None:
53 raise LdapError('No base dn on query execution')
55 if queryTimeout is None:
56 queryTimeout = self.queryTimeout
63 res = conn.extend.standard.paged_search(baseDN,
64 filter, attributes=attrib, paged_size=LDAP_PAGE,
66 search_scope=SUBTREE, time_limit=queryTimeout
70 if i['type'] == 'searchResEntry':
71 yield LdapRes.fromLdapQuery(i)
73 except Exception as e:
74 raise LdapError("Error on get data (%s): %s" % (type(e), str(e)), *e.args[1:])
76 def getList(self, *a, **kwa):
77 return [i for i in self(*a, **kwa)]
80 def _makeConnFabric(*a, **kwa):
82 return Connection(*a, **kwa)