Better LDAP gestion

This commit is contained in:
Kloadut 2012-10-10 16:01:54 +02:00
parent 4854365576
commit c4bc8e2bce
2 changed files with 70 additions and 50 deletions

View file

@ -7,19 +7,15 @@ import crypt
import random import random
import string import string
import getpass import getpass
from yunohost import YunoHostError, YunoHostLDAP, win_msg from yunohost import YunoHostError, win_msg, colorize
# Initialize LDAP def user_list(args, yldap): # TODO : fix
yldap = YunoHostLDAP()
def user_list(args): # TODO : fix
result = yldap.search() result = yldap.search()
#print(result) #print(result)
def user_add(args): def user_add(args, yldap):
""" """
Add user to LDAP Add user to LDAP
@ -36,14 +32,14 @@ def user_add(args):
for arg in required_args: for arg in required_args:
if not args[arg]: if not args[arg]:
if os.isatty(1): if os.isatty(1):
args[arg] = raw_input(arg.capitalize()+': ') args[arg] = raw_input(colorize(arg.capitalize()+': ', 'yellow'))
else: else:
raise Exception raise Exception
# Password # Password
if not args['password']: if not args['password']:
if os.isatty(1): if os.isatty(1):
args['password'] = getpass.getpass() args['password'] = getpass.getpass(colorize('Password: ', 'yellow'))
pwd2 = getpass.getpass('Retype password:') pwd2 = getpass.getpass(colorize('Retype password:', 'yellow'))
if args['password'] != pwd2: if args['password'] != pwd2:
raise YunoHostError(22, _("Passwords doesn't match")) raise YunoHostError(22, _("Passwords doesn't match"))
else: else:

View file

@ -28,11 +28,14 @@ import json
if not __debug__: if not __debug__:
import traceback import traceback
sys.path.append('lib')
gettext.install('YunoHost') gettext.install('YunoHost')
from yunohost import YunoHostError, str_to_func, colorize, pretty_print_dict try:
sys.path.append('lib')
from yunohost import YunoHostError, YunoHostLDAP, str_to_func, colorize, pretty_print_dict
except ImportError:
sys.stderr.write('Require YunoHost lib')
sys.exit(1)
""" """
Category/actions/arguments dictionnary Category/actions/arguments dictionnary
@ -78,11 +81,12 @@ action_dict = {
# User # # User #
############################# #############################
'user' : { 'user' : {
'help' : 'Manage users', 'category_help' : 'Manage users',
'actions' : { 'actions' : {
### user_list() ### user_list()
'list' : { 'list' : {
'help' : 'List users', 'action_help' : 'List users',
'ldap' : True,
'arguments' : { 'arguments' : {
'-a' : { '-a' : {
'full' : '--all', 'full' : '--all',
@ -93,7 +97,8 @@ action_dict = {
}, },
### user_add() ### user_add()
'add' : { 'add' : {
'help' : 'Create user', 'action_help' : 'Create user',
'ldap' : True,
'arguments' : { 'arguments' : {
'-u' : { '-u' : {
'full' : '--username', 'full' : '--username',
@ -113,29 +118,30 @@ action_dict = {
} }
}, },
'info' : { 'info' : {
'help' : 'Get user informations', 'ldap' : True,
'action_help' : 'Get user informations',
}, },
} }
}, },
'domain' : { 'domain' : {
'help' : 'Manage domains', 'category_help' : 'Manage domains',
'actions' : {} 'actions' : {}
}, },
'app' : { 'app' : {
'help' : 'Manage apps', 'category_help' : 'Manage apps',
'actions' : {} 'actions' : {}
}, },
'monitor' : { 'monitor' : {
'help' : 'Monitoring functions', 'category_help' : 'Monitoring functions',
'actions' : {} 'actions' : {}
}, },
'tools' : { 'tools' : {
'help' : 'Specific tools', 'category_help' : 'Specific tools',
'actions' : {} 'actions' : {}
} }
} }
def dict_to_args(action_dict): def parse_dict(action_dict):
""" """
Turn action dictionnary to parser, subparsers and arguments Turn action dictionnary to parser, subparsers and arguments
@ -146,6 +152,9 @@ def dict_to_args(action_dict):
Namespace of args Namespace of args
""" """
# Connection required
ldap = False
# Intialize parsers # Intialize parsers
parsers = subparsers_category = subparsers_action = {} parsers = subparsers_category = subparsers_action = {}
parsers['general'] = argparse.ArgumentParser() parsers['general'] = argparse.ArgumentParser()
@ -163,13 +172,18 @@ def dict_to_args(action_dict):
del action_dict['general_arguments'] del action_dict['general_arguments']
# Split categories into subparsers # Split categories into subparsers
for category, cat_params in action_dict.items(): for category, category_params in action_dict.items():
subparsers_category[category] = subparsers.add_parser(category, help=cat_params['help']) if 'category_help' not in category_params: category_params['category_help'] = ''
subparsers_category[category] = subparsers.add_parser(category, help=category_params['category_help'])
subparsers_action[category] = subparsers_category[category].add_subparsers() subparsers_action[category] = subparsers_category[category].add_subparsers()
# Split actions # Split actions
for action, action_params in cat_params['actions'].items(): if 'actions' in category_params:
parsers[category + '_' + action] = subparsers_action[category].add_parser(action, help=action_params['help']) for action, action_params in category_params['actions'].items():
# Set the action's related function # Service settings
if 'ldap' in action_params: ldap = action_params['ldap']
if 'action_help' not in action_params: action_params['action_help'] = ''
parsers[category + '_' + action] = subparsers_action[category].add_parser(action, help=action_params['action_help'])
# Set the action\'s related function
parsers[category + '_' + action].set_defaults( parsers[category + '_' + action].set_defaults(
func=str_to_func('yunohost_' + category + '.' func=str_to_func('yunohost_' + category + '.'
+ category + '_' + action)) + category + '_' + action))
@ -184,7 +198,7 @@ def dict_to_args(action_dict):
parsers[category + '_' + action].add_argument(arg_name, **arg_params) parsers[category + '_' + action].add_argument(arg_name, **arg_params)
args = parsers['general'].parse_args() args = parsers['general'].parse_args()
return args return { 'ldap' : ldap, 'args' : args }
def main(): def main():
""" """
@ -197,9 +211,15 @@ def main():
int -- 0 or error code int -- 0 or error code
""" """
parse = parse_dict(action_dict)
if parse['ldap']:
yldap = YunoHostLDAP()
try: try:
args = dict_to_args(action_dict) if parse['ldap']:
result = args.func(vars(args)) result = parse['args'].func(vars(parse['args']), yldap)
else:
result = parse['args'].func(vars(parse['args']))
except YunoHostError, error: except YunoHostError, error:
if not __debug__ : if not __debug__ :
traceback.print_exc() traceback.print_exc()
@ -213,6 +233,10 @@ def main():
pretty_print_dict(result) pretty_print_dict(result)
else: else:
print(json.dumps(result)) print(json.dumps(result))
finally:
if parse['ldap']:
yldap.disconnect()
return 0 return 0
if __name__ == '__main__': if __name__ == '__main__':