def user_search(self, user_id, institute, attributes=None): """Perform an LDAP search for the given user and institute. Note that both user_id and institute are mandatory here. If this is not what you want, you should instead use user_filter_search. @type user_id: string representing the user login in the given institute @type institute: string representing the institute @type attributes: list of string describing LDAP attributes. If this is None (default) then all attributes are returned. @returns: a dictionary, with the values for the requested attributes for the given user """ login_filter = LdapFilter("instituteLogin=%s" % (user_id)) institute_filter = LdapFilter("institute=%s" % (institute)) result = self.user_filter_search(login_filter & institute_filter, attributes) self.log.debug("user_search for %s, %s yields %s" % (user_id, institute, result)) if not result is None and len(result) > 0: return result[0] else: self.log.debug("user_search returning None") return None
def next(self): ldap_filter = None attributes = copy.deepcopy(self.attributes) attribute_choice = SingleChoiceGenerator(attributes) size = random.randint(1, random.randint(1, len(self.attributes))) for d in range(0, size): op = random.choice(self.operators) at = attribute_choice.next() new = LdapFilter("%s=%s" % (at, ''.join( [random.choice(string.printable) for x in range(16)]))) if not ldap_filter: ldap_filter = LdapFilter(new) else: if op == LdapFilter.negate: ldap_filter = op(ldap_filter) else: if random.choice([True, False]): ldap_filter = op(new, ldap_filter) else: ldap_filter = op(ldap_filter, new) return ldap_filter
def test_from_list_and(self): """Test the formation of a filters from a given list of filters using the and operator.""" fs = [LFG.next() for x in xrange(random.randint(2,30))] combination = LdapFilter.from_list(lambda x, y: x & y, fs) combination_string = "%s" % (combination) self.assertTrue(len(combination_string) <= 3 + sum(map(lambda f: len("%s" % (f)), fs))) self.assertTrue(combination_string[0] == '(') self.assertTrue(combination_string[1] == '&') self.assertTrue(combination_string[-1] == ')')
def test_from_list_and(self): """Test the formation of a filters from a given list of filters using the and operator.""" fs = [LFG.next() for x in range(random.randint(2, 30))] combination = LdapFilter.from_list(lambda x, y: x & y, fs) combination_string = "%s" % (combination) self.assertTrue( len(combination_string) <= 3 + sum(map(lambda f: len("%s" % (f)), fs))) self.assertTrue(combination_string[0] == '(') self.assertTrue(combination_string[1] == '&') self.assertTrue(combination_string[-1] == ')')
def test_from_list_and(self, fs): """Test the formation of a filters from a given list of filters using the and operator.""" if not fs or len(fs) < 2: return combination = LdapFilter.from_list(lambda x, y: x & y, fs) combination_string = "%s" % (combination) self.assertTrue(len(combination_string) <= 3 + sum(map(lambda f: len("%s" % (f)), fs))) self.assertTrue(combination_string[0] == '(') self.assertTrue(combination_string[1] == '&') self.assertTrue(combination_string[-1] == ')')
def get_user_with_status(status): """Get the users from the HPC LDAP that match the given status. @type ldap: vsc.ldap.utils.LdapQuery instance @type status: string represeting a valid status in the HPC LDAP @returns: list of VscLdapUser nametuples of matching users. """ logger.info("Retrieving users from the HPC LDAP with status=%s." % (status)) ldap_filter = LdapFilter("status=%s" % (status)) users = VscLdapUser.lookup(ldap_filter) logger.info("Found %d users in the %s state." % (len(users), status)) logger.debug("The following users are in the %s state: %s" % (status, [u.user_id for u in users])) return users