start moving charset conversion to fields
[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     # FIXME : we should not reference the attribute name!
28     if not value or field == 'jpegPhoto':
29         return value
30     elif isinstance(value, int):
31         return str(value)
32     elif isinstance(value, list):
33         return [ func(x) for x in value ]
34     else:
35         return func(value)
36
37 def escape_ldap_filter(value):
38     value = str(value)
39     return value.replace('\\', '\\5c') \
40                 .replace('*', '\\2a') \
41                 .replace('(', '\\28') \
42                 .replace(')', '\\29') \
43                 .replace('\0', '\\00')
44
45 class DatabaseCursor(object):
46     def __init__(self, ldap_connection):
47         self.connection = ldap_connection
48
49 class DatabaseFeatures(BaseDatabaseFeatures):
50     pass
51
52 class DatabaseOperations(BaseDatabaseOperations):
53     def quote_name(self, name):
54         return name
55
56 class LdapConnection(object):
57     def __init__(self):
58         self.connection = None
59         self.charset = "utf-8"
60         self.features = DatabaseFeatures()
61         self.ops = DatabaseOperations()
62
63     def _cursor(self):
64         if self.connection is None:
65             self.connection = ldap.initialize(settings.LDAPDB_SERVER_URI)
66             self.connection.simple_bind_s(
67                 settings.LDAPDB_BIND_DN,
68                 settings.LDAPDB_BIND_PASSWORD)
69         return DatabaseCursor(self.connection)
70
71     def add_s(self, dn, modlist):
72         cursor = self._cursor()
73         return cursor.connection.add_s(dn.encode(self.charset), modlist)
74
75     def delete_s(self, dn):
76         cursor = self._cursor()
77         return cursor.connection.delete_s(dn.encode(self.charset))
78
79     def modify_s(self, dn, modlist):
80         cursor = self._cursor()
81         return cursor.connection.modify_s(dn.encode(self.charset), modlist)
82
83     def rename_s(self, dn, newrdn):
84         cursor = self._cursor()
85         return cursor.connection.rename_s(dn.encode(self.charset), newrdn.encode(self.charset))
86
87     def search_s(self, base, scope, filterstr, attrlist):
88         cursor = self._cursor()
89         results = cursor.connection.search_s(base, scope, filterstr.encode(self.charset), attrlist)
90         output = []
91         for dn, attrs in results:
92             for field in attrs:
93                 attrs[field] = convert(field, attrs[field], lambda x: x.decode(self.charset))
94             output.append((dn.decode(self.charset), attrs))
95         return output
96
97 # FIXME: is this the right place to initialize the LDAP connection?
98 connection = LdapConnection()
99