X-Git-Url: https://git.stderr.nl/gitweb?p=matthijs%2Fupstream%2Fdjango-ldapdb.git;a=blobdiff_plain;f=ldapdb%2F__init__.py;h=4d6f8cf8a8a98eb20d4ab3e9cb910b13e68e54ff;hp=c6f0974476129a1e1d910d536ddc446f0027ef62;hb=82a5a4df7e7899646501206fb99925c79394d1c6;hpb=baba34a193fe7d304d86003b19f36ed638c7ec80 diff --git a/ldapdb/__init__.py b/ldapdb/__init__.py index c6f0974..4d6f8cf 100644 --- a/ldapdb/__init__.py +++ b/ldapdb/__init__.py @@ -32,69 +32,29 @@ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # -import ldap - +from django import db from django.conf import settings -from django.db.backends import BaseDatabaseFeatures, BaseDatabaseOperations + +from ldapdb.router import Router def escape_ldap_filter(value): - value = str(value) + value = unicode(value) return value.replace('\\', '\\5c') \ .replace('*', '\\2a') \ .replace('(', '\\28') \ .replace(')', '\\29') \ .replace('\0', '\\00') -class DatabaseCursor(object): - def __init__(self, ldap_connection): - self.connection = ldap_connection - -class DatabaseFeatures(BaseDatabaseFeatures): - pass - -class DatabaseOperations(BaseDatabaseOperations): - def quote_name(self, name): - return name - -class LdapConnection(object): - def __init__(self): - self.connection = None - self.charset = "utf-8" - self.features = DatabaseFeatures() - self.ops = DatabaseOperations() - - def _cursor(self): - if self.connection is None: - self.connection = ldap.initialize(settings.LDAPDB_SERVER_URI) - self.connection.simple_bind_s( - settings.LDAPDB_BIND_DN, - settings.LDAPDB_BIND_PASSWORD) - return DatabaseCursor(self.connection) - - def add_s(self, dn, modlist): - cursor = self._cursor() - return cursor.connection.add_s(dn.encode(self.charset), modlist) - - def delete_s(self, dn): - cursor = self._cursor() - return cursor.connection.delete_s(dn.encode(self.charset)) - - def modify_s(self, dn, modlist): - cursor = self._cursor() - return cursor.connection.modify_s(dn.encode(self.charset), modlist) - - def rename_s(self, dn, newrdn): - cursor = self._cursor() - return cursor.connection.rename_s(dn.encode(self.charset), newrdn.encode(self.charset)) - - def search_s(self, base, scope, filterstr, attrlist): - cursor = self._cursor() - results = cursor.connection.search_s(base, scope, filterstr.encode(self.charset), attrlist) - output = [] - for dn, attrs in results: - output.append((dn.decode(self.charset), attrs)) - return output - -# FIXME: is this the right place to initialize the LDAP connection? -connection = LdapConnection() - +# Legacy single database support +if hasattr(settings, 'LDAPDB_SERVER_URI'): + # Add the LDAP backend + settings.DATABASES['ldap'] = { + 'ENGINE': 'ldapdb.backends.ldap', + 'NAME': settings.LDAPDB_SERVER_URI, + 'USER': settings.LDAPDB_BIND_DN, + 'PASSWORD': settings.LDAPDB_BIND_PASSWORD, + 'SUPPORTS_TRANSACTIONS': False} + connection = db.connections['ldap'] + + # Add the LDAP router + db.router.routers.append(Router())