07db7bb587b2fca5da7b0cca6cca8384efd482a7
[matthijs/upstream/django-ldapdb.git] / ldapdb / __init__.py
1 # -*- coding: utf-8 -*-
2
3 # django-ldapdb
4 # Copyright (C) 2009 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
25 def convert(field, value, func):
26     if not value or field == 'jpegPhoto':
27         return value
28     elif isinstance(value, int):
29         return str(value)
30     elif isinstance(value, list):
31         return [ func(x) for x in value ]
32     else:
33         return func(value)
34
35 def escape_ldap_filter(value):
36     value = str(value)
37     return value.replace('\\', '\\5c') \
38                 .replace('*', '\\2a') \
39                 .replace('(', '\\28') \
40                 .replace(')', '\\29') \
41                 .replace('\0', '\\00')
42
43 class LdapConnection(object):
44     def __init__(self, server, bind_dn, bind_password):
45         self.connection = ldap.initialize(server)
46         self.connection.simple_bind_s(bind_dn, bind_password)
47         self.charset = "utf-8"
48
49     def add_s(self, dn, modlist):
50         mods = []
51         for field, value in modlist:
52             converted = convert(field, value, lambda x: x.encode(self.charset))
53             if isinstance(converted, list):
54                 mods.append((field, converted))
55             else:
56                 mods.append((field, [converted]))
57         return self.connection.add_s(dn.encode(self.charset), mods)
58
59     def delete_s(self, dn):
60         return self.connection.delete_s(dn.encode(self.charset))
61
62     def modify_s(self, dn, modlist):
63         mods = []
64         for op, field, value in modlist:
65             mods.append((op, field, convert(field, value, lambda x: x.encode(self.charset))))
66         return self.connection.modify_s(dn.encode(self.charset), mods)
67
68     def rename_s(self, dn, newrdn):
69         return self.connection.rename_s(dn.encode(self.charset), newrdn.encode(self.charset))
70
71     def search_s(self, base, scope, filterstr, attrlist):
72         results = self.connection.search_s(base, scope, filterstr.encode(self.charset), attrlist)
73         output = []
74         for dn, attrs in results:
75             for field in attrs:
76                 if field == "member" or field == "memberUid":
77                     attrs[field] = convert(field, attrs[field], lambda x: x.decode(self.charset))
78                 else:
79                     attrs[field] = convert(field, attrs[field][0], lambda x: x.decode(self.charset))
80             output.append((dn.decode(self.charset), attrs))
81         return output
82
83 # FIXME: is this the right place to initialize the LDAP connection?
84 connection = LdapConnection(settings.LDAPDB_SERVER_URI,
85     settings.LDAPDB_BIND_DN,
86     settings.LDAPDB_BIND_PASSWORD)
87