X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=ldapdb%2F__init__.py;h=4b0a6d54379abaf9dbb55d1037426fe0a2148d32;hb=b1cc6993cafbf24e8a394d8b9bea1048efa8adc9;hp=c622eb31491f65d618a2f3b6e9bd596ebb147b41;hpb=0be5396f6fbb66545d009e95b332b098803260b5;p=matthijs%2Fupstream%2Fdjango-ldapdb.git diff --git a/ldapdb/__init__.py b/ldapdb/__init__.py index c622eb3..4b0a6d5 100644 --- a/ldapdb/__init__.py +++ b/ldapdb/__init__.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # django-ldapdb -# Copyright (C) 2009 Bolloré telecom +# Copyright (C) 2009-2010 Bolloré telecom # See AUTHORS file for a full list of contributors. # # This program is free software: you can redistribute it and/or modify @@ -21,6 +21,7 @@ import ldap from django.conf import settings +from django.db.backends import BaseDatabaseFeatures, BaseDatabaseOperations def convert(field, value, func): if not value or field == 'jpegPhoto': @@ -31,12 +32,40 @@ def convert(field, value, func): return [ func(x) for x in value ] else: return func(value) - + +def escape_ldap_filter(value): + value = str(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, server, bind_dn, bind_password): - self.connection = ldap.initialize(server) - self.connection.simple_bind_s(bind_dn, bind_password) + 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): mods = [] @@ -46,22 +75,27 @@ class LdapConnection(object): mods.append((field, converted)) else: mods.append((field, [converted])) - return self.connection.add_s(dn.encode(self.charset), mods) + cursor = self._cursor() + return cursor.connection.add_s(dn.encode(self.charset), mods) def delete_s(self, dn): - return self.connection.delete_s(dn) + cursor = self._cursor() + return cursor.connection.delete_s(dn.encode(self.charset)) def modify_s(self, dn, modlist): mods = [] for op, field, value in modlist: mods.append((op, field, convert(field, value, lambda x: x.encode(self.charset)))) - return self.connection.modify_s(dn.encode(self.charset), mods) + cursor = self._cursor() + return cursor.connection.modify_s(dn.encode(self.charset), mods) def rename_s(self, dn, newrdn): - return self.connection.rename_s(dn.encode(self.charset), newrdn.encode(self.charset)) + cursor = self._cursor() + return cursor.connection.rename_s(dn.encode(self.charset), newrdn.encode(self.charset)) def search_s(self, base, scope, filterstr, attrlist): - results = self.connection.search_s(base, scope, filterstr.encode(self.charset), attrlist) + cursor = self._cursor() + results = cursor.connection.search_s(base, scope, filterstr.encode(self.charset), attrlist) output = [] for dn, attrs in results: for field in attrs: @@ -73,7 +107,5 @@ class LdapConnection(object): return output # FIXME: is this the right place to initialize the LDAP connection? -connection = LdapConnection(settings.LDAPDB_SERVER_URI, - settings.LDAPDB_BIND_DN, - settings.LDAPDB_BIND_PASSWORD) +connection = LdapConnection()