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 -*-
import sys
import ldap
import getpass
conn = ldap.initialize('ldap://localhost:389')
conn.simple_bind_s("cn=admin,dc=yunohost,dc=org","secret")
class YunoHostLDAP:
""" 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.modlist as modlist
import yunohost_ldap as yldap
import yunohost_ldap
# Initialize LDAP
yldap = yunohost_ldap.YunoHostLDAP()
def user_list(args):
result = yldap.conn.search_s('ou=users,dc=gavoty,dc=org',ldap.SCOPE_SUBTREE,'(cn=*)',['cn','mail'])
for dn,entry in result:
print entry['mail'][0]
result = yldap.search('ou=users,dc=gavoty,dc=org', attrs=['mail', 'dn', 'cn'])
print(result)

View file

@ -1,7 +1,8 @@
#!/usr/bin/env python
# -*- 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
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
along with this program; if not, see http://www.gnu.org/licenses
"""
"""
__author__ = 'Kload'
__version__ = '2.0 beta1'
import sys
import argparse
import gettext
sys.path.append('lib') # Local temporary hack
gettext.install('YunoHost')
def str_to_func(astr):
"""Call a function from a string name
"""
Call a function from a string name
Keyword arguments:
astr -- Name of function to call
@ -44,14 +48,15 @@ def str_to_func(astr):
try:
func = getattr(mod, function)
except NameError:
print 'Error: Function ' + category + '_' + action + '() is not defined'
print(_('Error: Function is not defined'))
sys.exit(1)
else:
return func
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:
action_dict -- Multi-level dictionnary of categories/actions list
@ -60,7 +65,6 @@ def dict_to_parsers(action_dict):
Dictionnrary of parsers
"""
# Intialize parsers
parsers = subparsers_category = subparsers_action = dict()
parsers['general'] = argparse.ArgumentParser()
@ -73,15 +77,15 @@ def dict_to_parsers(action_dict):
for action, helper in info['actions'].items():
parsers[category + '_' + action] = subparsers_action[category].add_parser(action, help=helper)
parsers[category + '_' + action].set_defaults(
func=str_to_func('yunohost_' + category
+ '.' + category
+ '_' + action))
func=str_to_func('yunohost_' + category + '.'
+ category + '_' + action))
return parsers
def parse_args(parsers):
"""Add and parse arguments
"""
Add and parse arguments
Keyword arguments:
parsers -- parsers and subparsers as a dict
@ -98,7 +102,6 @@ def parse_args(parsers):
#argparse.ArgumentParser.add_argument
"""
# General
parsers['general'].add_argument(
'-v',
@ -123,31 +126,30 @@ def parse_args(parsers):
def main():
"""Main instructions
"""
""" Main instructions """
action_dict = {
'user' : {
'help' : 'manage users',
'actions' : {
'list' : 'list users'
},
'help' : 'manage users'
}
},
'domain' : {
'actions' : {},
'help' : 'manage domains'
'help' : 'manage domains',
'actions' : {}
},
'app' : {
'actions' : {},
'help' : 'manage apps'
'help' : 'manage apps',
'actions' : {}
},
'monitor' : {
'actions' : {},
'help' : 'monitoring functions'
'help' : 'monitoring functions',
'actions' : {}
},
'tools' : {
'actions' : {},
'help' : 'specific tools'
'help' : 'specific tools',
'actions' : {}
}
}