X-Git-Url: https://git.stderr.nl/gitweb?p=matthijs%2Fupstream%2Fdjango-ldapdb.git;a=blobdiff_plain;f=ldapdb%2F__init__.py;h=7dde301da3532915ec9e0a099a8b881026038f6c;hp=fc9e0ff8c2bcdaf8026166c92c85713816d8ffb2;hb=9c4c98c24a7c0300ba09218b12833426d9bbb88f;hpb=62de279ee18d0054b8eef779844365723250b96b diff --git a/ldapdb/__init__.py b/ldapdb/__init__.py index fc9e0ff..7dde301 100644 --- a/ldapdb/__init__.py +++ b/ldapdb/__init__.py @@ -32,10 +32,8 @@ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # -import ldap - from django.conf import settings -from django.db.backends import BaseDatabaseFeatures, BaseDatabaseOperations +from django.db import connections def escape_ldap_filter(value): value = unicode(value) @@ -45,57 +43,11 @@ def escape_ldap_filter(value): .replace(')', '\\29') \ .replace('\0', '\\00') -class DatabaseCursor(object): - def __init__(self, ldap_connection): - self.connection = ldap_connection - -class DatabaseFeatures(BaseDatabaseFeatures): - def __init__(self, connection): - self.connection = connection - -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) - 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() +# Add the LDAP backend to the configured databases +settings.DATABASES['ldap'] = { + 'ENGINE': 'ldapdb.backends.ldap', + 'NAME': settings.LDAPDB_SERVER_URI, + 'USER': settings.LDAPDB_BIND_DN, + 'PASSWORD': settings.LDAPDB_BIND_PASSWORD} +connection = connections['ldap']