1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
#
#-*- coding:utf-8 -*-
try:
import ldap
except ImportError:
import sys
# py3.2
if sys.hexversion >= 0x30200f0:
print('To run "ldap-seeds" in python 3, it requires a python3 '
'compatible version of dev-python/python-ldap be installed\n'
'Currently only dev-python/python-ldap-9999 has that capability.')
raise
from gkeyldap.config import default_criteria, default_fields, UID
from gkeyldap.connect import LdapConnect
from gkeys.log import logger
class LdapSearch(object):
'''Class to perform searches on the configured LDAP server
'''
def __init__(self, fields=None, criteria=None):
self.fields = fields or default_fields
self.criteria = criteria or default_criteria
logger.debug('LdapSearch: __init__; fields...: %s' % self.fields)
logger.debug('LdapSearch: __init__; criteria.: %s' % self.criteria)
self.ldap_connection = LdapConnect().connect(action='Search')
self.status = True
if not self.ldap_connection:
self.status = False
def search(self, target, search_field=UID, fields=None, criteria=None):
'''Perform the LDAP search
'''
if not target:
logger.debug('LdapSearch: search; invalid target: "%s"' % target)
return {}
if not fields:
fields = self.fields
else:
logger.debug('LdapSearch: search; new fields: %s' % str(fields))
if not criteria:
criteria = self.criteria
else:
logger.debug('LdapSearch: search; new criteria: %s' % criteria)
results = self.ldap_connection.search_s(criteria,
ldap.SCOPE_ONELEVEL, search_field % target, fields)
#logger.debug('LdapSearch: search; result = %s' % str(results))
return results
def result2dict(self, results, key='uid'):
''' Convert results from LDAP attributes
to Gentoo Keys compatible attributes
@param results: dictionary with results
@param key: string to use as a key in the dictionary
'''
_dict = {}
for entry in results:
info = entry[1]
key_value = info[key][0]
_dict[key_value] = info
return _dict
|