return iter(sorted(accounts))
- @property
+ @lazy_property
def ldap(self):
- if not hasattr(self, "_ldap"):
- # Connect to LDAP server
- ldap_uri = self.settings.get("ldap_uri")
- self._ldap = ldap.initialize(ldap_uri)
+ # Connect to LDAP server
+ ldap_uri = self.settings.get("ldap_uri")
+ conn = ldap.initialize(ldap_uri)
- # Bind with username and password
- bind_dn = self.settings.get("ldap_bind_dn")
- if bind_dn:
- bind_pw = self.settings.get("ldap_bind_pw", "")
- self._ldap.simple_bind(bind_dn, bind_pw)
+ # Bind with username and password
+ bind_dn = self.settings.get("ldap_bind_dn")
+ if bind_dn:
+ bind_pw = self.settings.get("ldap_bind_pw", "")
+ conn.simple_bind(bind_dn, bind_pw)
- return self._ldap
+ return conn
def _query(self, query, attrlist=None, limit=0):
logging.debug("Performing LDAP query: %s" % query)
query, attrlist=attrlist, sizelimit=limit)
except:
# Close current connection
- del self._ldap
+ self.ldap.close()
+ del self.ldap
raise