7fec75dc41c733a6cebdddff51656ce4563c2700
[matthijs/upstream/django-ldapdb.git] / ldapdb / __init__.py
1 # -*- coding: utf-8 -*-
2
3 # django-ldapdb
4 # Copyright (c) 2009-2010, BollorĂ© telecom
5 # All rights reserved.
6
7 # See AUTHORS file for a full list of contributors.
8
9 # Redistribution and use in source and binary forms, with or without modification,
10 # are permitted provided that the following conditions are met:
11
12 #     1. Redistributions of source code must retain the above copyright notice, 
13 #        this list of conditions and the following disclaimer.
14 #     
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.
18
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.
22
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.
33 #
34
35 import ldap
36
37 from django.conf import settings
38 from django.db.backends import BaseDatabaseFeatures, BaseDatabaseOperations
39
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')
47
48 class DatabaseCursor(object):
49     def __init__(self, ldap_connection):
50         self.connection = ldap_connection
51
52 class DatabaseFeatures(BaseDatabaseFeatures):
53     def __init__(self, connection):
54         self.connection = connection
55
56 class DatabaseOperations(BaseDatabaseOperations):
57     def quote_name(self, name):
58         return name
59
60 class DatabaseWrapper(object):
61     def __init__(self, settings_dict={}, alias='ldap'):
62         self.settings_dict = settings_dict
63         self.connection = None
64         self.charset = "utf-8"
65         self.features = DatabaseFeatures(self)
66         self.ops = DatabaseOperations()
67
68     def close(self):
69         pass
70
71     def _cursor(self):
72         if self.connection is None:
73             self.connection = ldap.initialize(self.settings_dict['NAME'])
74             self.connection.simple_bind_s(
75                 self.settings_dict['USER'],
76                 self.settings_dict['PASSWORD'])
77         return DatabaseCursor(self.connection)
78
79     def add_s(self, dn, modlist):
80         cursor = self._cursor()
81         return cursor.connection.add_s(dn.encode(self.charset), modlist)
82
83     def delete_s(self, dn):
84         cursor = self._cursor()
85         return cursor.connection.delete_s(dn.encode(self.charset))
86
87     def modify_s(self, dn, modlist):
88         cursor = self._cursor()
89         return cursor.connection.modify_s(dn.encode(self.charset), modlist)
90
91     def rename_s(self, dn, newrdn):
92         cursor = self._cursor()
93         return cursor.connection.rename_s(dn.encode(self.charset), newrdn.encode(self.charset))
94
95     def search_s(self, base, scope, filterstr, attrlist):
96         cursor = self._cursor()
97         results = cursor.connection.search_s(base, scope, filterstr.encode(self.charset), attrlist)
98         output = []
99         for dn, attrs in results:
100             output.append((dn.decode(self.charset), attrs))
101         return output
102
103 # FIXME: is this the right place to initialize the LDAP connection?
104 connection = DatabaseWrapper({
105     'NAME': settings.LDAPDB_SERVER_URI,
106     'USER': settings.LDAPDB_BIND_DN,
107     'PASSWORD': settings.LDAPDB_BIND_PASSWORD})
108