user_list() function

This commit is contained in:
Kloadut 2012-10-28 17:27:47 +01:00
parent 0367811b6e
commit 34330a41cc
2 changed files with 51 additions and 8 deletions

View file

@ -37,15 +37,17 @@ def colorize(astr, color):
def pretty_print_dict(d, depth=0):
for k,v in sorted(d.items(), key=lambda x: x[0]):
k = colorize(k, 'purple')
if isinstance(v, list) and len(v) == 1:
v = v[0]
if isinstance(v, dict):
print((" ")*depth + ("%s: " % k))
print((" ") * depth + ("%s: " % k))
pretty_print_dict(v, depth+1)
if isinstance(v, list):
print((" ")*depth + ("%s: " % k))
elif isinstance(v, list):
print((" ") * depth + ("%s: " % k))
for value in v:
print((" ")*(depth+1) + "- " + value)
print((" ") * (depth+1) + "- " + value)
else:
print((" ")*depth + "%s: %s" % (k, v))
print((" ") * depth + "%s: %s" % (k, v))
def win_msg(astr):
"""

View file

@ -9,8 +9,49 @@ import string
import getpass
from yunohost import YunoHostError, win_msg, colorize, validate, get_required_args
def user_list(args, connections): # TODO : fix
print(args)
def user_list(args, connections):
"""
List YunoHost users from LDAP
Keyword argument:
args -- Dictionnary of values (can be empty)
connections -- LDAP connection
Returns:
Dict
"""
yldap = connections['ldap']
user_attrs = ['uid', 'mail', 'cn']
attrs = []
result_dict = {}
if args['offset']: offset = int(args['offset'])
else: offset = 0
if args['limit']: limit = int(args['limit'])
else: limit = 1000
if args['filter']: filter = args['filter']
else: filter = 'uid=*'
if args['fields']:
for attr in args['fields']:
if attr in user_attrs:
attrs.append(attr)
continue
else:
raise YunoHostError(22, _("Invalid field : ") + attr)
else:
attrs = user_attrs
result = yldap.search('ou=users,dc=yunohost,dc=org', filter, attrs)
if len(result) > (0 + offset) and limit > 0:
i = 0 + offset
for entry in result[i:]:
if i < limit:
result_dict[str(i)] = entry
i += 1
else:
result_dict = { 'Notice' : _("No user found") }
return result_dict
def user_create(args, connections):
@ -21,7 +62,7 @@ def user_create(args, connections):
args -- Dictionnary of values (can be empty)
Returns:
Boolean
Dict
"""
yldap = connections['ldap']
args = get_required_args(args, {