def __init__(self, connector): """ connector LDAPConnector instance. """ self.baseDN = '' self._connector = connector self._con = None self._cache = None if connector._cache: cachefactory = queryUtility(ICacheProviderFactory) if cachefactory is None: cachefactory = nullcacheProviderFactory cacheprovider = cachefactory() self._cache = ICacheManager(cacheprovider) self._cache.setTimeout(connector._cachetimeout) if not INullCacheProvider.providedBy(self._cache): logger.debug( u"LDAP Caching activated for instance '{0:s}'. " u"Use '{1:s}' as cache provider".format( repr(self._cache), repr(cacheprovider) ) ) else: logger.debug( u"LDAP Caching activated for instance '{0:s}'.".format( repr(self._cache), ) )
def getMembers(self): """Return the Users in the following form. { 'id': 'mmustermann', 'fullname': 'Max Mustermann', } """ #start = time.time() filter = self._allocateFilter() group = self.currentgroupid if group != 'ignore' and group != '': key = 'userandgroupselectionwidget:%s' % group manager = ICacheManager(CACHEPROVIDER) if isinstance(key, unicode): # The CacheManager can't handle unicode key = key.encode('utf-8') users = manager.getData(self._readGroupMembers, key, args=[group]) else: users = self._searchUsers() reduce = True for fil in filter: if fil == '*': reduce == False if reduce: users = self._reduceMembers(users, filter) return users
def __init__(self, connector): """ connector LDAPConnector instance. """ self.baseDN = '' self._connector = connector self._con = None self._cache = None if connector._cache: cachefactory = queryUtility(ICacheProviderFactory) if cachefactory is None: cachefactory = nullcacheProviderFactory cacheprovider = cachefactory() self._cache = ICacheManager(cacheprovider) self._cache.setTimeout(connector._cachetimeout) logger.debug(u"LDAP Caching activated for instance '%s'. Use '%s' " "as cache provider" % (repr(self._cache), repr(cacheprovider)))
def __init__(self, connector): """Takes LDAPConnector object as argument. """ self.baseDN = '' self._connector = connector self._con = None self._cache = None if connector._cache: cachefactory = queryUtility(ICacheProviderFactory) if cachefactory is None: cachefactory = nullcacheProviderFactory cacheprovider = cachefactory() self._cache = ICacheManager(cacheprovider) self._cache.setTimeout(connector._cachetimeout) logger.debug(u"LDAP Caching activated for instance '%s'. Use '%s' " "as cache provider" % (repr(self._cache), repr(cacheprovider)))
class LDAPCommunicator(object): """Class LDAPCommunicator is responsible for the communication with the LDAP Server. It provides methods to search, add, modify and delete entries in the directory. """ def __init__(self, connector): """ connector LDAPConnector instance. """ self.baseDN = '' self._connector = connector self._con = None self._cache = None if connector._cache: cachefactory = queryUtility(ICacheProviderFactory) if cachefactory is None: cachefactory = nullcacheProviderFactory cacheprovider = cachefactory() self._cache = ICacheManager(cacheprovider) self._cache.setTimeout(connector._cachetimeout) logger.debug(u"LDAP Caching activated for instance '%s'. Use '%s' " "as cache provider" % (repr(self._cache), repr(cacheprovider))) def bind(self): """Bind to LDAP Server. """ self._con = self._connector.bind() def unbind(self): """Unbind from LDAP Server. """ self._connector.unbind() self._con = None def search(self, queryFilter, scope, baseDN=None, force_reload=False, attrlist=None, attrsonly=0, page_size=None, cookie=None): """Search the directory. queryFilter LDAP query filter scope LDAP search scope baseDN Search base. Defaults to ``self.baseDN`` force_reload Force reload of result if cache enabled. attrlist LDAP attrlist to query. attrsonly Flag whether to return only attribute names, without corresponding values. page_size Number of items per page, when doing pagination. cookie Cookie string returned by previous search with pagination. """ if baseDN is None: baseDN = self.baseDN if not baseDN: raise ValueError(u"baseDN unset.") if page_size: if cookie is None: cookie = '' pagedresults = ldap.controls.libldap.SimplePagedResultsControl( criticality=True, size=page_size, cookie=cookie) serverctrls = [pagedresults] else: if cookie: raise ValueError('cookie passed without page_size') serverctrls = [] def _search(baseDN, scope, queryFilter, attrlist, attrsonly, serverctrls): # we have to do async search to also retrieve server controls # in case we do pagination of results msgid = self._con.search_ext(baseDN, scope, queryFilter, attrlist, attrsonly, serverctrls=serverctrls) rtype, results, rmsgid, rctrls = self._con.result3(msgid) ctype = ldap.controls.libldap.SimplePagedResultsControl.controlType pctrls = [c for c in rctrls if c.controlType == ctype] if pctrls: return results, pctrls[0].cookie else: return results args = [baseDN, scope, queryFilter, attrlist, attrsonly, serverctrls] if self._cache: key = '%s-%s-%s-%s-%s-%i-%s-%s' % ( self._connector._bindDN, baseDN, sorted(attrlist or []), attrsonly, queryFilter, scope, page_size, cookie) key = md5digest(key) return self._cache.getData(_search, key, force_reload, args) else: return _search(*args) def add(self, dn, data): """Insert an entry into directory. dn adding DN data dict containing key/value pairs of entry attributes """ attributes = [(k, v) for k, v in data.items()] self._con.add_s(dn, attributes) def modify(self, dn, modlist): """Modify an existing entry in the directory. Takes the DN of the entry and the modlist, which is a list of tuples containing modifation descriptions. The first element gives the type of the modification (MOD_REPLACE, MOD_DELETE, or MOD_ADD), the second gives the name of the field to modify, and the third gives the new value for the field (for MOD_ADD and MOD_REPLACE). """ self._con.modify_s(dn, modlist) def delete(self, deleteDN): """Delete an entry from the directory. Take the DN to delete from the directory as argument. """ self._con.delete_s(deleteDN) def passwd(self, userdn, oldpw, newpw): self._con.passwd_s(userdn, oldpw, newpw)
class LDAPCommunicator(object): """Class LDAPCommunicator is responsible for the communication with the LDAP Server. It provides methods to search, add, modify and delete entries in the directory. """ def __init__(self, connector): """ connector LDAPConnector instance. """ self.baseDN = '' self._connector = connector self._con = None self._cache = None if connector._cache: cachefactory = queryUtility(ICacheProviderFactory) if cachefactory is None: cachefactory = nullcacheProviderFactory cacheprovider = cachefactory() self._cache = ICacheManager(cacheprovider) self._cache.setTimeout(connector._cachetimeout) if not INullCacheProvider.providedBy(self._cache): logger.debug( u"LDAP Caching activated for instance '{0:s}'. " u"Use '{1:s}' as cache provider".format( repr(self._cache), repr(cacheprovider) ) ) else: logger.debug( u"LDAP Caching activated for instance '{0:s}'.".format( repr(self._cache), ) ) def bind(self): """Bind to LDAP Server. """ self._con = self._connector.bind() def unbind(self): """Unbind from LDAP Server. """ self._connector.unbind() self._con = None def search(self, queryFilter, scope, baseDN=None, force_reload=False, attrlist=None, attrsonly=0, page_size=None, cookie=None): """Search the directory. queryFilter LDAP query filter scope LDAP search scope baseDN Search base. Defaults to ``self.baseDN`` force_reload Force reload of result if cache enabled. attrlist LDAP attrlist to query. attrsonly Flag whether to return only attribute names, without corresponding values. page_size Number of items per page, when doing pagination. cookie Cookie string returned by previous search with pagination. """ if baseDN is None: baseDN = self.baseDN if not baseDN: raise ValueError(u"baseDN unset.") if page_size: if cookie is None: cookie = '' pagedresults = ldap.controls.libldap.SimplePagedResultsControl( criticality=True, size=page_size, cookie=cookie) serverctrls = [pagedresults] else: if cookie: raise ValueError('cookie passed without page_size') serverctrls = [] def _search(baseDN, scope, queryFilter, attrlist, attrsonly, serverctrls): # we have to do async search to also retrieve server controls # in case we do pagination of results if type(attrlist) in (list, tuple): attrlist = [str(_) for _ in attrlist] msgid = self._con.search_ext( baseDN, scope, queryFilter, attrlist, attrsonly, serverctrls=serverctrls ) rtype, results, rmsgid, rctrls = self._con.result3(msgid) ctype = ldap.controls.libldap.SimplePagedResultsControl.controlType pctrls = [c for c in rctrls if c.controlType == ctype] if pctrls: return results, pctrls[0].cookie else: return results args = [baseDN, scope, queryFilter, attrlist, attrsonly, serverctrls] if self._cache: key_items = [ self._connector._bindDN, baseDN, sorted(attrlist or []), attrsonly, queryFilter, scope, page_size, cookie ] key = '-'.join([str(_) for _ in key_items]) key = md5digest(key) return self._cache.getData( _search, key, force_reload, args ) return _search(*args) def add(self, dn, data): """Insert an entry into directory. dn adding DN data dict containing key/value pairs of entry attributes """ attributes = [(k, v) for k, v in data.items()] self._con.add_s(dn, attributes) def modify(self, dn, modlist): """Modify an existing entry in the directory. Takes the DN of the entry and the modlist, which is a list of tuples containing modifation descriptions. The first element gives the type of the modification (MOD_REPLACE, MOD_DELETE, or MOD_ADD), the second gives the name of the field to modify, and the third gives the new value for the field (for MOD_ADD and MOD_REPLACE). """ self._con.modify_s(dn, modlist) def delete(self, deleteDN): """Delete an entry from the directory. Take the DN to delete from the directory as argument. """ self._con.delete_s(deleteDN) def passwd(self, userdn, oldpw, newpw): self._con.passwd_s(userdn, oldpw, newpw)
class LDAPCommunicator(object): """Class LDAPCommunicator is responsible for the communication with the LDAP Server. It provides methods to search, add, modify and delete entries in the directory. Usage: c = LDAPConnector('localhost', 389, 'cn=admin,dc=foo,dc=bar', 'secret') lc = LDAPCommunicator(c) lc.setBaseDN('ou=customers,dc=foo,dc=bar') lc.bind() result = lc.search('[email protected]', lc.SUBTREE) # do soething with result ... lc.unbind() """ def __init__(self, connector): """Takes LDAPConnector object as argument. """ self.baseDN = '' self._connector = connector self._con = None self._cache = None if connector._cache: cachefactory = queryUtility(ICacheProviderFactory) if cachefactory is None: cachefactory = nullcacheProviderFactory cacheprovider = cachefactory() self._cache = ICacheManager(cacheprovider) self._cache.setTimeout(connector._cachetimeout) logger.debug(u"LDAP Caching activated for instance '%s'. Use '%s' " "as cache provider" % (repr(self._cache), repr(cacheprovider))) def bind(self): """Bind to LDAP Server. """ self._con = self._connector.bind() def unbind(self): """Unbind from LDAP Server. """ self._connector.unbind() self._con = None def setBaseDN(self, baseDN): """Set the base DN you want to work on. Deprecated: This function will be removed in version 1.5. Use ``baseDN`` property directly instead. """ self.baseDN = baseDN def getBaseDN(self): """Returns the current set base DN. Deprecated: This function will be removed in version 1.5. Use ``baseDN`` property directly instead. """ return self.baseDN def search(self, queryFilter, scope, baseDN=None, force_reload=False, attrlist=None, attrsonly=0): """Search the directory. ``queryFilter`` LDAP query filter ``scope`` LDAP search scope ``baseDN`` Search base. Defaults to ``self.baseDN`` ``force_reload`` Force cache to be ignored if enabled. ``attrlist`` LDAP attrlist to query. ``attrsonly`` Flag wether to load DN's (?) only. """ if baseDN is None: baseDN = self.baseDN if self._cache: # XXX: Consider attrlist and attrsonly in cachekey. key = '%s-%s-%s-%i' % (self._connector._bindDN, baseDN, queryFilter, scope) key = md5digest(key) args = [baseDN, scope, queryFilter, attrlist, attrsonly] return self._cache.getData(self._con.search_s, key, force_reload, args) return self._con.search_s(baseDN, scope, queryFilter, attrlist, attrsonly) def add(self, dn, data): """Insert an entry into directory. Takes the DN of the entry and the data this object contains. data is a dict and looks like this: >>> data = { ... 'uid':'foo', ... 'givenname':'foo', ... 'cn':'foo 0815', ... 'sn':'bar', ... 'telephonenumber':'123-4567', ... 'facsimiletelephonenumber':'987-6543', ... 'objectclass':('Remote-Address','person', 'Top'), ... 'physicaldeliveryofficename':'Development', ... 'mail':'*****@*****.**', ... 'title':'programmer', ... } """ attributes = [ (k,v) for k,v in data.items() ] self._con.add_s(dn, attributes) def modify(self, dn, modlist): """Modify an existing entry in the directory. Takes the DN of the entry and the modlist, which is a list of tuples containing modifation descriptions. The first element gives the type of the modification (MOD_REPLACE, MOD_DELETE, or MOD_ADD), the second gives the name of the field to modify, and the third gives the new value for the field (for MOD_ADD and MOD_REPLACE). """ self._con.modify_s(dn, modlist) def delete(self, deleteDN): """Delete an entry from the directory. Take the DN to delete from the directory as argument. """ self._con.delete_s(deleteDN) def passwd(self, userdn, oldpw, newpw): self._con.passwd_s(userdn, oldpw, newpw)
class LDAPCommunicator(object): """Class LDAPCommunicator is responsible for the communication with the LDAP Server. It provides methods to search, add, modify and delete entries in the directory. """ def __init__(self, connector): """ connector LDAPConnector instance. """ self.baseDN = '' self._connector = connector self._con = None self._cache = None if connector._cache: cachefactory = queryUtility(ICacheProviderFactory) if cachefactory is None: cachefactory = nullcacheProviderFactory cacheprovider = cachefactory() self._cache = ICacheManager(cacheprovider) self._cache.setTimeout(connector._cachetimeout) logger.debug(u"LDAP Caching activated for instance '%s'. Use '%s' " "as cache provider" % (repr(self._cache), repr(cacheprovider))) def bind(self): """Bind to LDAP Server. """ self._con = self._connector.bind() def unbind(self): """Unbind from LDAP Server. """ self._connector.unbind() self._con = None def search(self, queryFilter, scope, baseDN=None, force_reload=False, attrlist=None, attrsonly=0): """Search the directory. queryFilter LDAP query filter scope LDAP search scope baseDN Search base. Defaults to ``self.baseDN`` force_reload Force reload of result if cache enabled. attrlist LDAP attrlist to query. attrsonly Flag whether to return only attribute names, without corresponding values. """ if baseDN is None: baseDN = self.baseDN if not baseDN: raise ValueError(u"baseDN unset.") if self._cache: # XXX: Consider attrlist and attrsonly in cachekey. key = '%s-%s-%s-%i' % (self._connector._bindDN, baseDN, queryFilter, scope) key = md5digest(key) args = [baseDN, scope, queryFilter, attrlist, attrsonly] return self._cache.getData(self._con.search_s, key, force_reload, args) return self._con.search_s(baseDN, scope, queryFilter, attrlist, attrsonly) def add(self, dn, data): """Insert an entry into directory. dn adding DN data dict containing key/value pairs of entry attributes """ attributes = [ (k,v) for k,v in data.items() ] self._con.add_s(dn, attributes) def modify(self, dn, modlist): """Modify an existing entry in the directory. Takes the DN of the entry and the modlist, which is a list of tuples containing modifation descriptions. The first element gives the type of the modification (MOD_REPLACE, MOD_DELETE, or MOD_ADD), the second gives the name of the field to modify, and the third gives the new value for the field (for MOD_ADD and MOD_REPLACE). """ self._con.modify_s(dn, modlist) def delete(self, deleteDN): """Delete an entry from the directory. Take the DN to delete from the directory as argument. """ self._con.delete_s(deleteDN) def passwd(self, userdn, oldpw, newpw): self._con.passwd_s(userdn, oldpw, newpw)