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
15
yunohost.py
15
yunohost.py
|
@ -188,19 +188,29 @@ 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:
|
||||
|
@ -214,7 +224,6 @@ class YunoHostLDAP(object):
|
|||
self.conn.simple_bind_s('cn=admin,' + self.base, self.pwd)
|
||||
except ldap.INVALID_CREDENTIALS:
|
||||
raise YunoHostError(13, _('Invalid credentials'))
|
||||
return self
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
self.disconnect()
|
||||
|
|
Loading…
Reference in a new issue