X-Git-Url: https://git.stderr.nl/gitweb?p=matthijs%2Fupstream%2Fdjango-ldapdb.git;a=blobdiff_plain;f=ldapdb%2F__init__.py;h=44d4974e9148d9c5ba9738be42531ef361c854ad;hp=7fec75dc41c733a6cebdddff51656ce4563c2700;hb=3e119b381a67261e2b70646117f29bd799c776c1;hpb=f226e94e9993be5bd9191f64a09fa58f687ed0c0 diff --git a/ldapdb/__init__.py b/ldapdb/__init__.py index 7fec75d..44d4974 100644 --- a/ldapdb/__init__.py +++ b/ldapdb/__init__.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # django-ldapdb -# Copyright (c) 2009-2010, Bolloré telecom +# Copyright (c) 2009-2011, Bolloré telecom # All rights reserved. # # See AUTHORS file for a full list of contributors. @@ -32,10 +32,7 @@ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # -import ldap - from django.conf import settings -from django.db.backends import BaseDatabaseFeatures, BaseDatabaseOperations def escape_ldap_filter(value): value = unicode(value) @@ -45,64 +42,17 @@ 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 DatabaseWrapper(object): - def __init__(self, settings_dict={}, alias='ldap'): - self.settings_dict = settings_dict - self.connection = None - self.charset = "utf-8" - self.features = DatabaseFeatures(self) - self.ops = DatabaseOperations() - - def close(self): - pass - - def _cursor(self): - if self.connection is None: - self.connection = ldap.initialize(self.settings_dict['NAME']) - self.connection.simple_bind_s( - self.settings_dict['USER'], - self.settings_dict['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 +# Legacy single database support +if hasattr(settings, 'LDAPDB_SERVER_URI'): + from django import db + from ldapdb.router import Router -# FIXME: is this the right place to initialize the LDAP connection? -connection = DatabaseWrapper({ - 'NAME': settings.LDAPDB_SERVER_URI, - 'USER': settings.LDAPDB_BIND_DN, - 'PASSWORD': settings.LDAPDB_BIND_PASSWORD}) + # 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} + # Add the LDAP router + db.router.routers.append(Router())