1 # -*- coding: utf-8 -*-
4 # Copyright (C) 2009-2010 Bolloré telecom
5 # See AUTHORS file for a full list of contributors.
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.
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.
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/>.
23 from django.conf import settings
24 from django.db.backends import BaseDatabaseFeatures, BaseDatabaseOperations
26 def convert(field, value, func):
27 if not value or field == 'jpegPhoto':
29 elif isinstance(value, int):
31 elif isinstance(value, list):
32 return [ func(x) for x in value ]
36 def escape_ldap_filter(value):
38 return value.replace('\\', '\\5c') \
39 .replace('*', '\\2a') \
40 .replace('(', '\\28') \
41 .replace(')', '\\29') \
42 .replace('\0', '\\00')
44 class DatabaseFeatures(BaseDatabaseFeatures):
47 class DatabaseOperations(BaseDatabaseOperations):
48 def quote_name(self, name):
51 class LdapConnection(object):
52 def __init__(self, server, bind_dn, bind_password):
53 self.connection = ldap.initialize(server)
54 self.connection.simple_bind_s(bind_dn, bind_password)
55 self.charset = "utf-8"
56 self.features = DatabaseFeatures()
57 self.ops = DatabaseOperations()
59 def add_s(self, dn, modlist):
61 for field, value in modlist:
62 converted = convert(field, value, lambda x: x.encode(self.charset))
63 if isinstance(converted, list):
64 mods.append((field, converted))
66 mods.append((field, [converted]))
67 return self.connection.add_s(dn.encode(self.charset), mods)
69 def delete_s(self, dn):
70 return self.connection.delete_s(dn.encode(self.charset))
72 def modify_s(self, dn, modlist):
74 for op, field, value in modlist:
75 mods.append((op, field, convert(field, value, lambda x: x.encode(self.charset))))
76 return self.connection.modify_s(dn.encode(self.charset), mods)
78 def rename_s(self, dn, newrdn):
79 return self.connection.rename_s(dn.encode(self.charset), newrdn.encode(self.charset))
81 def search_s(self, base, scope, filterstr, attrlist):
82 results = self.connection.search_s(base, scope, filterstr.encode(self.charset), attrlist)
84 for dn, attrs in results:
86 if field == "member" or field == "memberUid":
87 attrs[field] = convert(field, attrs[field], lambda x: x.decode(self.charset))
89 attrs[field] = convert(field, attrs[field][0], lambda x: x.decode(self.charset))
90 output.append((dn.decode(self.charset), attrs))
93 # FIXME: is this the right place to initialize the LDAP connection?
94 connection = LdapConnection(settings.LDAPDB_SERVER_URI,
95 settings.LDAPDB_BIND_DN,
96 settings.LDAPDB_BIND_PASSWORD)