mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
singleton
This commit is contained in:
parent
f63cc8d5d0
commit
54c2dbb64b
1 changed files with 24 additions and 15 deletions
39
yunohost.py
39
yunohost.py
|
@ -188,33 +188,42 @@ class YunoHostError(Exception):
|
|||
else:
|
||||
self.desc = code
|
||||
|
||||
def singleton(cls):
|
||||
instances = {}
|
||||
def get_instance():
|
||||
if cls not in instances:
|
||||
instances[cls] = cls()
|
||||
return instances[cls]
|
||||
return get_instance
|
||||
|
||||
@singleton
|
||||
class YunoHostLDAP(object):
|
||||
""" Specific LDAP functions for YunoHost """
|
||||
conn = None
|
||||
|
||||
def __enter__(self, password=False):
|
||||
self.__init__(password)
|
||||
return self
|
||||
|
||||
def __init__(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"))
|
||||
self.conn = ldap.initialize('ldap://localhost:389')
|
||||
self.base = 'dc=yunohost,dc=org'
|
||||
if password:
|
||||
self.pwd = password
|
||||
else:
|
||||
try:
|
||||
self.conn.simple_bind_s('cn=admin,' + self.base, self.pwd)
|
||||
except ldap.INVALID_CREDENTIALS:
|
||||
raise YunoHostError(13, _('Invalid credentials'))
|
||||
return self
|
||||
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'))
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
self.disconnect()
|
||||
|
|
Loading…
Reference in a new issue