1 # -*- coding: utf-8 -*-
4 # Copyright (c) 2009-2010, Bolloré telecom
7 # See AUTHORS file for a full list of contributors.
9 # Redistribution and use in source and binary forms, with or without modification,
10 # are permitted provided that the following conditions are met:
12 # 1. Redistributions of source code must retain the above copyright notice,
13 # this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in the
17 # documentation and/or other materials provided with the distribution.
19 # 3. Neither the name of Bolloré telecom nor the names of its contributors
20 # may be used to endorse or promote products derived from this software
21 # without specific prior written permission.
23 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
27 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 from django.conf import settings
38 from django.db.backends import BaseDatabaseFeatures, BaseDatabaseOperations
40 def escape_ldap_filter(value):
41 value = unicode(value)
42 return value.replace('\\', '\\5c') \
43 .replace('*', '\\2a') \
44 .replace('(', '\\28') \
45 .replace(')', '\\29') \
46 .replace('\0', '\\00')
48 class DatabaseCursor(object):
49 def __init__(self, ldap_connection):
50 self.connection = ldap_connection
52 class DatabaseFeatures(BaseDatabaseFeatures):
55 class DatabaseOperations(BaseDatabaseOperations):
56 def quote_name(self, name):
59 class LdapConnection(object):
61 self.connection = None
62 self.charset = "utf-8"
63 self.features = DatabaseFeatures()
64 self.ops = DatabaseOperations()
67 if self.connection is None:
68 self.connection = ldap.initialize(settings.LDAPDB_SERVER_URI)
69 self.connection.simple_bind_s(
70 settings.LDAPDB_BIND_DN,
71 settings.LDAPDB_BIND_PASSWORD)
72 return DatabaseCursor(self.connection)
74 def add_s(self, dn, modlist):
75 cursor = self._cursor()
76 return cursor.connection.add_s(dn.encode(self.charset), modlist)
78 def delete_s(self, dn):
79 cursor = self._cursor()
80 return cursor.connection.delete_s(dn.encode(self.charset))
82 def modify_s(self, dn, modlist):
83 cursor = self._cursor()
84 return cursor.connection.modify_s(dn.encode(self.charset), modlist)
86 def rename_s(self, dn, newrdn):
87 cursor = self._cursor()
88 return cursor.connection.rename_s(dn.encode(self.charset), newrdn.encode(self.charset))
90 def search_s(self, base, scope, filterstr, attrlist):
91 cursor = self._cursor()
92 results = cursor.connection.search_s(base, scope, filterstr.encode(self.charset), attrlist)
94 for dn, attrs in results:
95 output.append((dn.decode(self.charset), attrs))
98 # FIXME: is this the right place to initialize the LDAP connection?
99 connection = LdapConnection()