mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
broken
This commit is contained in:
parent
0e17445ff0
commit
d54ebd86eb
4 changed files with 295 additions and 268 deletions
|
@ -58,7 +58,6 @@ user:
|
|||
-f:
|
||||
full: --filter
|
||||
help: LDAP filter used to search
|
||||
pattern: 'yayaya'
|
||||
-l:
|
||||
full: --limit
|
||||
help: Maximum number of user fetched
|
||||
|
@ -75,15 +74,24 @@ user:
|
|||
-u:
|
||||
full: --username
|
||||
help: Must be unique
|
||||
ask: "Username"
|
||||
#pattern: '^[a-z0-9_]+$'
|
||||
pattern: 'aza'
|
||||
-f:
|
||||
full: --firstname
|
||||
ask: "Firstname"
|
||||
-l:
|
||||
full: --lastname
|
||||
ask: "Lastname"
|
||||
-m:
|
||||
full: --mail
|
||||
help: Main mail address must be unique
|
||||
ask: "Mail address"
|
||||
pattern: '^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}$'
|
||||
-p:
|
||||
full: --password
|
||||
ask: "User password"
|
||||
password: yes
|
||||
|
||||
### user_delete()
|
||||
delete:
|
||||
|
|
74
parse_args
74
parse_args
|
@ -24,6 +24,7 @@ import os
|
|||
import sys
|
||||
import argparse
|
||||
import gettext
|
||||
import getpass
|
||||
try:
|
||||
import yaml
|
||||
except ImportError:
|
||||
|
@ -38,7 +39,7 @@ if not __debug__:
|
|||
gettext.install('YunoHost')
|
||||
|
||||
try:
|
||||
from yunohost import YunoHostError, YunoHostLDAP, str_to_func, colorize, pretty_print_dict, display_error, connect_services, disconnect_services, validate
|
||||
from yunohost import YunoHostError, YunoHostLDAP, str_to_func, colorize, pretty_print_dict, display_error, validate
|
||||
except ImportError:
|
||||
sys.stderr.write('Error: Yunohost CLI Require YunoHost lib\n')
|
||||
sys.exit(1)
|
||||
|
@ -59,6 +60,8 @@ def parse_dict(action_map):
|
|||
parsers = subparsers_category = subparsers_action = {}
|
||||
parsers['general'] = argparse.ArgumentParser()
|
||||
subparsers = parsers['general'].add_subparsers()
|
||||
new_args = []
|
||||
patterns = {}
|
||||
|
||||
# Add general arguments
|
||||
for arg_name, arg_params in action_map['general_arguments'].items():
|
||||
|
@ -88,28 +91,59 @@ def parse_dict(action_map):
|
|||
# Add arguments
|
||||
if 'arguments' in action_params:
|
||||
for arg_name, arg_params in action_params['arguments'].items():
|
||||
if 'password' in arg_params:
|
||||
if arg_params['password']: is_password = True
|
||||
del arg_params['password']
|
||||
else: is_password = False
|
||||
|
||||
if 'full' in arg_params:
|
||||
arg_fullname = arg_params['full']
|
||||
arg_names = [arg_name, arg_params['full']]
|
||||
del arg_params['full']
|
||||
else: arg_names = [arg_name]
|
||||
|
||||
if 'ask' in arg_params:
|
||||
require_input = True
|
||||
if (category != sys.argv[1]) or (action != sys.argv[2]):
|
||||
require_input = False
|
||||
for name in arg_names:
|
||||
if name in sys.argv: require_input = False
|
||||
|
||||
if require_input:
|
||||
if is_password:
|
||||
if os.isatty(1):
|
||||
pwd1 = getpass.getpass(colorize(arg_params['ask'] + ': ', 'cyan'))
|
||||
pwd2 = getpass.getpass(colorize('Retype ' + arg_params['ask'][0].lower() + arg_params['ask'][1:] + ': ', 'cyan'))
|
||||
if pwd1 != pwd2:
|
||||
raise YunoHostError(22, _("Passwords don't match"))
|
||||
sys.exit(1)
|
||||
else:
|
||||
arg_fullname = False
|
||||
raise YunoHostError(22, _("Missing arguments") + ': ' + arg_name)
|
||||
new_args.extend([arg_name, pwd1])
|
||||
else:
|
||||
if os.isatty(1):
|
||||
arg_value = raw_input(colorize(arg_params['ask'] + ': ', 'cyan'))
|
||||
else:
|
||||
raise YunoHostError(22, _("Missing arguments") + ': ' + arg_name)
|
||||
new_args.extend([arg_name, arg_value])
|
||||
del arg_params['ask']
|
||||
|
||||
if 'pattern' in arg_params:
|
||||
pattern = arg_params['pattern']
|
||||
if (category == sys.argv[1]) and (action == sys.argv[2]):
|
||||
if 'dest' in arg_params: name = arg_params['dest']
|
||||
elif arg_fullname: name = arg_fullname
|
||||
else: name = arg_name
|
||||
patterns[name] = arg_params['pattern']
|
||||
del arg_params['pattern']
|
||||
def pmatch(string):
|
||||
validate({ string : pattern })
|
||||
if arg_fullname:
|
||||
parsers[category + '_' + action].add_argument(arg_name, arg_fullname, type=pmatch, **arg_params)
|
||||
else:
|
||||
parsers[category + '_' + action].add_argument(arg_name, type=pmatch, **arg_params)
|
||||
else:
|
||||
if arg_fullname:
|
||||
parsers[category + '_' + action].add_argument(arg_name, arg_fullname, **arg_params)
|
||||
else:
|
||||
parsers[category + '_' + action].add_argument(arg_name, **arg_params)
|
||||
|
||||
return parsers['general'].parse_args()
|
||||
parsers[category + '_' + action].add_argument(*arg_names, **arg_params)
|
||||
|
||||
args = parsers['general'].parse_args(sys.argv.extend(new_args))
|
||||
args_dict = vars(args)
|
||||
print patterns
|
||||
for key, value in patterns.items():
|
||||
validate({ args_dict[key] : value })
|
||||
|
||||
return args
|
||||
|
||||
|
||||
def main():
|
||||
|
@ -126,12 +160,8 @@ def main():
|
|||
with open('action_map.yml') as f:
|
||||
action_map = yaml.load(f)
|
||||
|
||||
args = parse_dict(action_map)
|
||||
connections = connect_services(action_map)
|
||||
try:
|
||||
if connections:
|
||||
result = args.func(vars(args), connections)
|
||||
else:
|
||||
args = parse_dict(action_map)
|
||||
result = args.func(vars(args))
|
||||
except TypeError, error:
|
||||
if not __debug__ :
|
||||
|
@ -148,8 +178,6 @@ def main():
|
|||
pretty_print_dict(result)
|
||||
else:
|
||||
print(json.dumps(result))
|
||||
finally:
|
||||
disconnect_services(connections)
|
||||
|
||||
return 0
|
||||
|
||||
|
|
13
yunohost.py
13
yunohost.py
|
@ -99,6 +99,7 @@ def validate(regex_dict):
|
|||
Boolean | YunoHostError
|
||||
|
||||
"""
|
||||
print regex_dict
|
||||
for attr, pattern in regex_dict.items():
|
||||
if re.match(pattern, attr):
|
||||
continue
|
||||
|
@ -242,27 +243,35 @@ class YunoHostError(Exception):
|
|||
self.desc = code
|
||||
|
||||
|
||||
class YunoHostLDAP:
|
||||
class YunoHostLDAP(object):
|
||||
""" Specific LDAP functions for YunoHost """
|
||||
conn = None
|
||||
|
||||
def __init__(self, password=False):
|
||||
def __enter__(self, password=False):
|
||||
"""
|
||||
Connect to LDAP base
|
||||
|
||||
Initialize to localhost, base yunohost.org, prompt for password
|
||||
|
||||
"""
|
||||
if self.conn is None:
|
||||
self.conn = ldap.initialize('ldap://localhost:389')
|
||||
self.base = 'dc=yunohost,dc=org'
|
||||
if password:
|
||||
self.pwd = password
|
||||
else:
|
||||
try:
|
||||
self.pwd = getpass.getpass(colorize(_('Admin Password: '), 'yellow'))
|
||||
except KeyboardInterrupt, EOFError:
|
||||
raise YunoHostError(125, _("Interrupted"))
|
||||
try:
|
||||
self.conn.simple_bind_s('cn=admin,' + self.base, self.pwd)
|
||||
except ldap.INVALID_CREDENTIALS:
|
||||
raise YunoHostError(13, _('Invalid credentials'))
|
||||
return self
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
self.disconnect()
|
||||
|
||||
def disconnect(self):
|
||||
"""
|
||||
|
|
|
@ -7,20 +7,19 @@ import crypt
|
|||
import random
|
||||
import string
|
||||
import getpass
|
||||
from yunohost import YunoHostError, win_msg, colorize, validate, get_required_args
|
||||
from yunohost import YunoHostError, YunoHostLDAP, win_msg, colorize, validate, get_required_args
|
||||
|
||||
def user_list(args, connections):
|
||||
def user_list(args):
|
||||
"""
|
||||
List YunoHost users from LDAP
|
||||
|
||||
Keyword argument:
|
||||
args -- Dictionnary of values (can be empty)
|
||||
connections -- LDAP connection
|
||||
|
||||
Returns:
|
||||
Dict
|
||||
"""
|
||||
yldap = connections['ldap']
|
||||
with YunoHostLDAP() as yldap:
|
||||
user_attrs = ['uid', 'mail', 'cn', 'mailalias']
|
||||
attrs = []
|
||||
result_dict = {}
|
||||
|
@ -64,36 +63,22 @@ def user_list(args, connections):
|
|||
return result_dict
|
||||
|
||||
|
||||
def user_create(args, connections):
|
||||
def user_create(args):
|
||||
"""
|
||||
Add user to LDAP
|
||||
|
||||
Keyword argument:
|
||||
args -- Dictionnary of values (can be empty)
|
||||
connections -- LDAP connections
|
||||
|
||||
Returns:
|
||||
Dict
|
||||
"""
|
||||
yldap = connections['ldap']
|
||||
args = get_required_args(args, {
|
||||
'username': _('Username'),
|
||||
'mail': _('Mail address'),
|
||||
'firstname': _('Firstname'),
|
||||
'lastname': _('Lastname'),
|
||||
'password': _('Password')
|
||||
}, True)
|
||||
|
||||
print args
|
||||
with YunoHostLDAP() as yldap:
|
||||
# Validate password length
|
||||
if len(args['password']) < 4:
|
||||
raise YunoHostError(22, _("Password is too short"))
|
||||
|
||||
# Validate other values TODO: validate all values
|
||||
validate({
|
||||
args['username'] : r'^[a-z0-9_]+$',
|
||||
args['mail'] : r'^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}$',
|
||||
})
|
||||
|
||||
yldap.validate_uniqueness({
|
||||
'uid' : args['username'],
|
||||
'mail' : args['mail'],
|
||||
|
@ -145,18 +130,17 @@ def user_create(args, connections):
|
|||
raise YunoHostError(169, _("An error occured during user creation"))
|
||||
|
||||
|
||||
def user_delete(args, connections):
|
||||
def user_delete(args):
|
||||
"""
|
||||
Remove user from LDAP
|
||||
|
||||
Keyword argument:
|
||||
args -- Dictionnary of values (can be empty)
|
||||
connections -- LDAP connection
|
||||
|
||||
Returns:
|
||||
Dict
|
||||
"""
|
||||
yldap = connections['ldap']
|
||||
with YunoHostLDAP() as yldap:
|
||||
result = { 'Users' : [] }
|
||||
|
||||
args = get_required_args(args, { 'users' : _('User to delete') })
|
||||
|
@ -177,18 +161,17 @@ def user_delete(args, connections):
|
|||
return result
|
||||
|
||||
|
||||
def user_update(args, connections):
|
||||
def user_update(args):
|
||||
"""
|
||||
Update user informations
|
||||
|
||||
Keyword argument:
|
||||
args -- Dictionnary of values
|
||||
connections -- LDAP connection
|
||||
|
||||
Returns:
|
||||
Dict
|
||||
"""
|
||||
yldap = connections['ldap']
|
||||
with YunoHostLDAP() as yldap:
|
||||
validate({ args['user'] : r'^[a-z0-9_]+$' })
|
||||
attrs_to_fetch = ['givenName', 'sn', 'mail', 'mailAlias']
|
||||
new_attr_dict = {}
|
||||
|
@ -281,18 +264,17 @@ def user_update(args, connections):
|
|||
|
||||
|
||||
|
||||
def user_info(args, connections):
|
||||
def user_info(args):
|
||||
"""
|
||||
Fetch user informations from LDAP
|
||||
|
||||
Keyword argument:
|
||||
args -- Dictionnary of values (can be empty)
|
||||
connections -- LDAP connection
|
||||
|
||||
Returns:
|
||||
Dict
|
||||
"""
|
||||
yldap = connections['ldap']
|
||||
with YunoHostLDAP() as yldap:
|
||||
user_attrs = ['cn', 'mail', 'uid', 'mailAlias']
|
||||
|
||||
if args['mail']:
|
||||
|
|
Loading…
Add table
Reference in a new issue