This commit is contained in:
Kloadut 2012-10-07 16:20:20 +02:00
parent 8b781dc9f8
commit daa28fdf4b
3 changed files with 78 additions and 29 deletions

View file

@ -1,6 +1,51 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import sys
import ldap import ldap
import getpass
conn = ldap.initialize('ldap://localhost:389') class YunoHostLDAP:
conn.simple_bind_s("cn=admin,dc=yunohost,dc=org","secret") """ Specific LDAP functions for YunoHost """
def __init__(self):
""" Connect to LDAP base """
self.conn = ldap.initialize('ldap://localhost:389')
self.base = 'dc=yunohost,dc=org'
self.pwd = getpass.getpass()
try:
self.conn.simple_bind_s('cn=admin,' + self.base, self.pwd)
except ldap.INVALID_CREDENTIALS:
print(_('Error: Wrong credentials'))
sys.exit(1)
def disconnect(self):
""" Unbind from LDAP """
try:
self.conn.unbind_s()
except:
print(_('Error: A problem occured on LDAP unbind'))
return False
else:
return True
def search(self, base, filter='(objectClass=*)', attrs=['dn']):
""" Search in LDAP base """
try:
result = self.conn.search_s(base, ldap.SCOPE_ONELEVEL, filter, attrs)
except Exception:
print(_('Error: An error occured on LDAP search'))
return False
if result:
result_list = []
for dn, entry in result:
if 'dn' in attrs:
entry['dn'] = [dn]
result_list.append(entry)
return result_list
else:
print(_('No result found'))
return False

View file

@ -2,9 +2,11 @@
import ldap import ldap
import ldap.modlist as modlist import ldap.modlist as modlist
import yunohost_ldap as yldap import yunohost_ldap
# Initialize LDAP
yldap = yunohost_ldap.YunoHostLDAP()
def user_list(args): def user_list(args):
result = yldap.conn.search_s('ou=users,dc=gavoty,dc=org',ldap.SCOPE_SUBTREE,'(cn=*)',['cn','mail']) result = yldap.search('ou=users,dc=gavoty,dc=org', attrs=['mail', 'dn', 'cn'])
for dn,entry in result: print(result)
print entry['mail'][0]

View file

@ -1,7 +1,8 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
__credits__ = """Copyright (C) 2012 YunoHost __credits__ = """
Copyright (C) 2012 YunoHost
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published it under the terms of the GNU Affero General Public License as published
@ -15,17 +16,20 @@ __credits__ = """Copyright (C) 2012 YunoHost
You should have received a copy of the GNU Affero General Public License You should have received a copy of the GNU Affero General Public License
along with this program; if not, see http://www.gnu.org/licenses along with this program; if not, see http://www.gnu.org/licenses
""" """
__author__ = 'Kload' __author__ = 'Kload'
__version__ = '2.0 beta1' __version__ = '2.0 beta1'
import sys import sys
import argparse import argparse
import gettext
sys.path.append('lib') # Local temporary hack sys.path.append('lib') # Local temporary hack
gettext.install('YunoHost')
def str_to_func(astr): def str_to_func(astr):
"""Call a function from a string name """
Call a function from a string name
Keyword arguments: Keyword arguments:
astr -- Name of function to call astr -- Name of function to call
@ -44,14 +48,15 @@ def str_to_func(astr):
try: try:
func = getattr(mod, function) func = getattr(mod, function)
except NameError: except NameError:
print 'Error: Function ' + category + '_' + action + '() is not defined' print(_('Error: Function is not defined'))
sys.exit(1) sys.exit(1)
else: else:
return func return func
def dict_to_parsers(action_dict): def dict_to_parsers(action_dict):
"""Turn action dictionnary to parser and subparsers (2 level) """
Turn action dictionnary to parser and subparsers (2 level)
Keyword arguments: Keyword arguments:
action_dict -- Multi-level dictionnary of categories/actions list action_dict -- Multi-level dictionnary of categories/actions list
@ -60,7 +65,6 @@ def dict_to_parsers(action_dict):
Dictionnrary of parsers Dictionnrary of parsers
""" """
# Intialize parsers # Intialize parsers
parsers = subparsers_category = subparsers_action = dict() parsers = subparsers_category = subparsers_action = dict()
parsers['general'] = argparse.ArgumentParser() parsers['general'] = argparse.ArgumentParser()
@ -73,15 +77,15 @@ def dict_to_parsers(action_dict):
for action, helper in info['actions'].items(): for action, helper in info['actions'].items():
parsers[category + '_' + action] = subparsers_action[category].add_parser(action, help=helper) parsers[category + '_' + action] = subparsers_action[category].add_parser(action, help=helper)
parsers[category + '_' + action].set_defaults( parsers[category + '_' + action].set_defaults(
func=str_to_func('yunohost_' + category func=str_to_func('yunohost_' + category + '.'
+ '.' + category + category + '_' + action))
+ '_' + action))
return parsers return parsers
def parse_args(parsers): def parse_args(parsers):
"""Add and parse arguments """
Add and parse arguments
Keyword arguments: Keyword arguments:
parsers -- parsers and subparsers as a dict parsers -- parsers and subparsers as a dict
@ -98,7 +102,6 @@ def parse_args(parsers):
#argparse.ArgumentParser.add_argument #argparse.ArgumentParser.add_argument
""" """
# General # General
parsers['general'].add_argument( parsers['general'].add_argument(
'-v', '-v',
@ -123,31 +126,30 @@ def parse_args(parsers):
def main(): def main():
"""Main instructions """ Main instructions """
"""
action_dict = { action_dict = {
'user' : { 'user' : {
'help' : 'manage users',
'actions' : { 'actions' : {
'list' : 'list users' 'list' : 'list users'
}, }
'help' : 'manage users'
}, },
'domain' : { 'domain' : {
'actions' : {}, 'help' : 'manage domains',
'help' : 'manage domains' 'actions' : {}
}, },
'app' : { 'app' : {
'actions' : {}, 'help' : 'manage apps',
'help' : 'manage apps' 'actions' : {}
}, },
'monitor' : { 'monitor' : {
'actions' : {}, 'help' : 'monitoring functions',
'help' : 'monitoring functions' 'actions' : {}
}, },
'tools' : { 'tools' : {
'actions' : {}, 'help' : 'specific tools',
'help' : 'specific tools' 'actions' : {}
} }
} }