diff --git a/lib/yunohost_ldap.py b/lib/yunohost_ldap.py index e4b16435..94e586e1 100644 --- a/lib/yunohost_ldap.py +++ b/lib/yunohost_ldap.py @@ -3,6 +3,7 @@ import sys import ldap import getpass +import yunohost_messages as msg class YunoHostLDAP: """ Specific LDAP functions for YunoHost """ @@ -12,11 +13,11 @@ class YunoHostLDAP: self.conn = ldap.initialize('ldap://localhost:389') self.base = 'dc=yunohost,dc=org' - self.pwd = getpass.getpass() + self.pwd = getpass.getpass(_('LDAP Admin Password: ')) try: self.conn.simple_bind_s('cn=admin,' + self.base, self.pwd) except ldap.INVALID_CREDENTIALS: - print(_('Error: Wrong credentials')) + print(msg.error + _('Wrong credentials')) sys.exit(1) def disconnect(self): @@ -30,9 +31,12 @@ class YunoHostLDAP: else: return True - def search(self, base, filter='(objectClass=*)', attrs=['dn']): + def search(self, base=None, filter='(objectClass=*)', attrs=['dn']): """ Search in LDAP base """ + if not base: + base = self.base + try: result = self.conn.search_s(base, ldap.SCOPE_ONELEVEL, filter, attrs) except Exception: diff --git a/lib/yunohost_user.py b/lib/yunohost_user.py index 91127ba3..5c12b0f7 100644 --- a/lib/yunohost_user.py +++ b/lib/yunohost_user.py @@ -1,12 +1,38 @@ # -*- coding: utf-8 -*- +import sys import ldap import ldap.modlist as modlist import yunohost_ldap +import yunohost_messages as msg +import getpass # Initialize LDAP yldap = yunohost_ldap.YunoHostLDAP() -def user_list(args): - result = yldap.search('ou=users,dc=gavoty,dc=org', attrs=['mail', 'dn', 'cn']) +def user_list(args): # TODO : fix + result = yldap.search() print(result) + + +def user_add(args): + required_args = ['username', 'mail', 'firstname', 'lastname'] + + try: + for arg in required_args: + if not args[arg]: + args[arg] = raw_input(arg.capitalize()+': ') + + if not args['password']: + args['password'] = getpass.getpass() + pwd2 = getpass.getpass('Retype password:') + if args['password'] != pwd2: + print(msg.error + _("Passwords doesn't match")) + sys.exit(1) + except KeyboardInterrupt, EOFError: + print("\n" + msg.interrupt + _("User not created")) + sys.exit(1) + + + print(args) + diff --git a/yunohost b/yunohost index 944d0378..fccc4f8c 100755 --- a/yunohost +++ b/yunohost @@ -25,7 +25,7 @@ import argparse import gettext sys.path.append('lib') # Local temporary hack gettext.install('YunoHost') - +import yunohost_messages as msg def str_to_func(astr): """ @@ -48,7 +48,7 @@ def str_to_func(astr): try: func = getattr(mod, function) except NameError: - print(_('Error: Function is not defined')) + print(msg.error + _('Function is not defined')) sys.exit(1) else: return func @@ -110,14 +110,25 @@ def parse_args(parsers): version='%(prog)s ' + __version__ ) + ##################### + # User # + ##################### - # User + # user list parsers['user_list'].add_argument( '-a', '--all', action='store' ) + # user add + parsers['user_add'].add_argument('-u', '--username') + parsers['user_add'].add_argument('-m', '--mail') + parsers['user_add'].add_argument('-f', '--firstname') + parsers['user_add'].add_argument('-l', '--lastname') + parsers['user_add'].add_argument('-p', '--password') + + # Call arguments parsing args = parsers['general'].parse_args() @@ -130,25 +141,26 @@ def main(): action_dict = { 'user' : { - 'help' : 'manage users', + 'help' : 'Manage users', 'actions' : { - 'list' : 'list users' + 'list' : 'List users', + 'add' : 'Add user' } }, 'domain' : { - 'help' : 'manage domains', + 'help' : 'Manage domains', 'actions' : {} }, 'app' : { - 'help' : 'manage apps', + 'help' : 'Manage apps', 'actions' : {} }, 'monitor' : { - 'help' : 'monitoring functions', + 'help' : 'Monitoring functions', 'actions' : {} }, 'tools' : { - 'help' : 'specific tools', + 'help' : 'Specific tools', 'actions' : {} } }