Modify no-ldap option

This commit is contained in:
Kload 2013-10-24 16:49:40 +00:00
parent 15f85746cd
commit 2c93b811c7
2 changed files with 35 additions and 25 deletions

View file

@ -64,7 +64,7 @@ def main():
admin_password_provided = False
json_print = False
no_ldap = False
write_ldap = True
for key, arg in enumerate(sys.argv):
if arg == '--admin-password':
@ -72,11 +72,9 @@ def main():
admin_password = sys.argv[key+1]
sys.argv.pop(key)
sys.argv.pop(key)
if arg == '--no-ldap':
no_ldap = True
write_ldap = False
sys.argv.pop(key)
if arg == '--json':
json_print = True
sys.argv.pop(key)
@ -104,7 +102,8 @@ def main():
if admin_password_provided:
with YunoHostLDAP(password=admin_password):
result = args.func(**args_dict)
elif os.isatty(1) and not no_ldap:
elif os.isatty(1) and write_ldap:
print('meh')
admin_password = getpass.getpass(colorize(_('Admin Password: '), 'yellow'))
with YunoHostLDAP(password=admin_password):
try:
@ -125,6 +124,7 @@ def main():
os.remove('/etc/yunohost/passwd')
os.remove('/var/run/yunohost.pid')
else:
with YunoHostLDAP(anonymous=True):
result = args.func(**args_dict)
#except TypeError, error:
#if not __debug__ :

View file

@ -251,6 +251,7 @@ class Singleton(object):
class YunoHostLDAP(Singleton):
""" Specific LDAP functions for YunoHost """
pwd = False
connected = False
conn = ldap.initialize('ldap://localhost:389')
base = 'dc=yunohost,dc=org'
level = 0
@ -258,15 +259,23 @@ class YunoHostLDAP(Singleton):
def __enter__(self):
return self
def __init__(self, password=False):
def __init__(self, password=False, anonymous=False):
"""
Connect to LDAP base
Initialize to localhost, base yunohost.org, prompt for password
"""
if password: self.pwd = password
elif self.pwd: pass
if anonymous:
self.conn.simple_bind_s()
self.connected = True
elif self.connected:
pass
else:
if password:
self.pwd = password
elif self.pwd:
pass
else:
need_password = True
while need_password:
@ -283,6 +292,7 @@ class YunoHostLDAP(Singleton):
self.level = self.level+1
try:
self.conn.simple_bind_s('cn=admin,' + self.base, self.pwd)
self.connected = True
except ldap.INVALID_CREDENTIALS:
raise YunoHostError(13, _('Invalid credentials'))