delay LDAP connection establishment
[matthijs/upstream/django-ldapdb.git] / ldapdb / __init__.py
1 # -*- coding: utf-8 -*-
2
3 # django-ldapdb
4 # Copyright (C) 2009-2010 BollorĂ© telecom
5 # See AUTHORS file for a full list of contributors.
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 import ldap
22
23 from django.conf import settings
24 from django.db.backends import BaseDatabaseFeatures, BaseDatabaseOperations
25
26 def convert(field, value, func):
27     if not value or field == 'jpegPhoto':
28         return value
29     elif isinstance(value, int):
30         return str(value)
31     elif isinstance(value, list):
32         return [ func(x) for x in value ]
33     else:
34         return func(value)
35
36 def escape_ldap_filter(value):
37     value = str(value)
38     return value.replace('\\', '\\5c') \
39                 .replace('*', '\\2a') \
40                 .replace('(', '\\28') \
41                 .replace(')', '\\29') \
42                 .replace('\0', '\\00')
43
44 class DatabaseCursor(object):
45     def __init__(self, ldap_connection):
46         self.connection = ldap_connection
47
48 class DatabaseFeatures(BaseDatabaseFeatures):
49     pass
50
51 class DatabaseOperations(BaseDatabaseOperations):
52     def quote_name(self, name):
53         return name
54
55 class LdapConnection(object):
56     def __init__(self):
57         self.connection = None
58         self.charset = "utf-8"
59         self.features = DatabaseFeatures()
60         self.ops = DatabaseOperations()
61
62     def _cursor(self):
63         if self.connection is None:
64             self.connection = ldap.initialize(settings.LDAPDB_SERVER_URI)
65             self.connection.simple_bind_s(
66                 settings.LDAPDB_BIND_DN,
67                 settings.LDAPDB_BIND_PASSWORD)
68         return DatabaseCursor(self.connection)
69
70     def add_s(self, dn, modlist):
71         mods = []
72         for field, value in modlist:
73             converted = convert(field, value, lambda x: x.encode(self.charset))
74             if isinstance(converted, list):
75                 mods.append((field, converted))
76             else:
77                 mods.append((field, [converted]))
78         cursor = self._cursor()
79         return cursor.connection.add_s(dn.encode(self.charset), mods)
80
81     def delete_s(self, dn):
82         cursor = self._cursor()
83         return cursor.connection.delete_s(dn.encode(self.charset))
84
85     def modify_s(self, dn, modlist):
86         mods = []
87         for op, field, value in modlist:
88             mods.append((op, field, convert(field, value, lambda x: x.encode(self.charset))))
89         cursor = self._cursor()
90         return cursor.connection.modify_s(dn.encode(self.charset), mods)
91
92     def rename_s(self, dn, newrdn):
93         cursor = self._cursor()
94         return cursor.connection.rename_s(dn.encode(self.charset), newrdn.encode(self.charset))
95
96     def search_s(self, base, scope, filterstr, attrlist):
97         cursor = self._cursor()
98         results = cursor.connection.search_s(base, scope, filterstr.encode(self.charset), attrlist)
99         output = []
100         for dn, attrs in results:
101             for field in attrs:
102                 if field == "member" or field == "memberUid":
103                     attrs[field] = convert(field, attrs[field], lambda x: x.decode(self.charset))
104                 else:
105                     attrs[field] = convert(field, attrs[field][0], lambda x: x.decode(self.charset))
106             output.append((dn.decode(self.charset), attrs))
107         return output
108
109 # FIXME: is this the right place to initialize the LDAP connection?
110 connection = LdapConnection()
111