def handle(self, *args, **kwargs): conn = get_ldap() owner_pages = Paginator(CIOwner.objects.all(), 100) print("Found {} CIOwners.".format(owner_pages.count)) for i in owner_pages.page_range: page = owner_pages.page(i) for ci_owner in page: filter_ = d( ldap.filter.filter_format( '(&(givenName=%s)(sn=%s))', ( ci_owner.first_name, ci_owner.last_name, ), )) result = conn.search_s( settings.AUTH_LDAP_USER_SEARCH_BASE, ldap.SCOPE_SUBTREE, filter_, attrlist=[d('sAMAccountName')], ) if not result: print('No LDAP data found for {}'.format(ci_owner)) continue if len(result) > 1: print( 'Multiple LDAP records found for {}'.format(ci_owner)) continue dn, data = result[0] account_name = data.get('sAMAccountName') if account_name is not None: ci_owner.sAMAccountName = account_name[0] ci_owner.save() print('Chunk {} of {} done.'.format(i, owner_pages.page_range))
def handle(self, *args, **kwargs): conn = get_ldap() owner_pages = Paginator(CIOwner.objects.all(), 100) print("Found {} CIOwners.".format(owner_pages.count)) for i in owner_pages.page_range: page = owner_pages.page(i) for ci_owner in page: filter_ = d(ldap.filter.filter_format( '(&(givenName=%s)(sn=%s))', ( ci_owner.first_name, ci_owner.last_name, ), )) result = conn.search_s( settings.AUTH_LDAP_USER_SEARCH_BASE, ldap.SCOPE_SUBTREE, filter_, attrlist=[d('sAMAccountName')], ) if not result: print('No LDAP data found for {}'.format(ci_owner)) continue if len(result) > 1: print( 'Multiple LDAP records found for {}'.format(ci_owner) ) continue dn, data = result[0] account_name = data.get('sAMAccountName') if account_name is not None: ci_owner.sAMAccountName = account_name[0] ci_owner.save() print('Chunk {} of {} done.'.format(i, owner_pages.page_range))
def _run_ldap_query(self, query): self.conn = get_ldap() lc = SimplePagedResultsControl(size=LDAP_RESULTS_PAGE_SIZE, cookie='') msgid = self.conn.search_ext( settings.AUTH_LDAP_USER_SEARCH_BASE, ldap.SCOPE_SUBTREE, query, serverctrls=[lc], ) page_num = 0 while True: page_num += 1 r_type, r_data, r_msgid, serverctrls = self.conn.result3(msgid) print "Pack of %s users loaded (page %s)" % ( LDAP_RESULTS_PAGE_SIZE, page_num, ) for item in r_data: yield item if serverctrls: if serverctrls[0].cookie: lc.size = LDAP_RESULTS_PAGE_SIZE lc.cookie = serverctrls[0].cookie msgid = self.conn.search_ext( settings.AUTH_LDAP_USER_SEARCH_BASE, ldap.SCOPE_SUBTREE, query, serverctrls=[lc], ) else: break else: logger.error( 'LDAP::_run_ldap_query\tQuery: %s\t' 'Server ignores RFC 2696 control', ) sys.exit(-1) self._disconnect()