From 34330a41cc7fa56d683f6be722a8ff4cc8586d6a Mon Sep 17 00:00:00 2001 From: Kloadut Date: Sun, 28 Oct 2012 17:27:47 +0100 Subject: [PATCH] user_list() function --- yunohost.py | 12 +++++++----- yunohost_user.py | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/yunohost.py b/yunohost.py index c5f4de65..b58027e1 100644 --- a/yunohost.py +++ b/yunohost.py @@ -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): """ diff --git a/yunohost_user.py b/yunohost_user.py index cede6745..5cab685d 100644 --- a/yunohost_user.py +++ b/yunohost_user.py @@ -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, {