Skip to content

Commit

Permalink
implement search
Browse files Browse the repository at this point in the history
  • Loading branch information
kakwa committed May 31, 2015
1 parent c9b971e commit 53660ee
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
15 changes: 13 additions & 2 deletions ldapcherry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,18 @@ def reload(self, config = None):
exit(1)

def _search(self, searchstring):
if searchstring is None:
return {}
ret = {}
for b in self.backends:
ret[b] = self.backends[b].search(searchstring)
tmp = self.backends[b].search(searchstring)
for u in tmp:
if not u in ret:
ret[u] = {}
for attr in tmp[u]:
if not attr in ret[u]:
ret[u][attr] = tmp[u][attr]
return ret

def _check_auth(self, must_admin):
if self.auth_mode == 'none':
Expand Down Expand Up @@ -418,7 +427,9 @@ def searchuser(self, searchstring):
def searchadmin(self, searchstring=None):
""" search user page """
self._check_auth(must_admin=True)
return self.temp_searchadmin.render(searchresult=['test', 'toto', 'tata'])
res = self._search(searchstring)
attrs_list = self.attributes.get_search_attributes()
return self.temp_searchadmin.render(searchresult = res, attrs_list = attrs_list)

@cherrypy.expose
def adduser(self, **params):
Expand Down
4 changes: 2 additions & 2 deletions ldapcherry/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, attributes_file):
self.backends = Set([])
self.self_attributes = Set([])
self.backend_attributes = {}
self.displayed_attributes = []
self.displayed_attributes = {}
self.key = None
try:
stream = open(attributes_file, 'r')
Expand Down Expand Up @@ -51,7 +51,7 @@ def __init__(self, attributes_file):
self.backend_attributes[b] = []
self.backend_attributes[b].append(attr['backends'][b])
if 'search_displayed' in attr and attr['search_displayed']:
self.displayed_attributes.append(attrid)
self.displayed_attributes[attrid] = attr['display_name']

if self.key is None:
raise MissingUserKey()
Expand Down
14 changes: 11 additions & 3 deletions resources/templates/searchadmin.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
<table id="RecordTable" class="table table-hover table-condensed tablesorter">
<thead>
<tr>
%for attr in attrs_list:
<th>
User
${attrs_list[attr]}
</th>
% endfor
<th class="sorter-false">
Modify
</th>
Expand All @@ -35,9 +37,13 @@
<tbody>
%for user in searchresult:
<tr>
%for attr in attrs_list:
<td>
${user}
% if attr in searchresult[user]:
${searchresult[user][attr]}
% endif
</td>
% endfor
<td>
<a href="/modify?user=${user}">
<button type="submit" class="btn btn-xs blue">
Expand All @@ -56,9 +62,11 @@
% endfor
</tbody>
<tr>
%for attr in attrs_list:
<th>
User
${attrs_list[attr]}
</th>
% endfor
<th class="sorter-false">
Modify
</th>
Expand Down
13 changes: 12 additions & 1 deletion tests/test_Attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,24 @@ def testGetSelfAttributes(self):
expected = Set(['ldap', 'ad'])
assert ret == expected

def testGetSearchAttributes(self):
inv = Attributes('./tests/cfg/attributes.yml')
ret = inv.get_search_attributes()
expected = {'first-name': 'First Name', 'cn': 'Display Name', 'name': 'Name', 'uid': 'UID', 'email': 'Name'}
assert ret == expected

def testGetBackendAttributes(self):
inv = Attributes('./tests/cfg/attributes.yml')
ret = inv.get_backend_attributes('ldap')
expected = ['shell', 'cn', 'uid', 'uidNumber', 'gidNumber', 'home', 'userPassword', 'givenName', 'email', 'sn']
assert ret == expected

def testGetKey(self):
inv = Attributes('./tests/cfg/attributes.yml')
ret = inv.get_key()
expected = 'uid'
assert ret == expected

def testWrongGetBackendAttributes(self):
inv = Attributes('./tests/cfg/attributes.yml')
try:
Expand All @@ -44,7 +56,6 @@ def testWrongGetBackendAttributes(self):
return
else:
raise AssertionError("expected an exception")


def testNoFile(self):
try:
Expand Down

0 comments on commit 53660ee

Please sign in to comment.