make rollback a no-op
[matthijs/upstream/django-ldapdb.git] / ldapdb / backends / ldap / base.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.db.backends import BaseDatabaseFeatures, BaseDatabaseOperations, BaseDatabaseWrapper
38
39 class DatabaseCursor(object):
40     def __init__(self, ldap_connection):
41         self.connection = ldap_connection
42
43 class DatabaseFeatures(BaseDatabaseFeatures):
44     def __init__(self, connection):
45         self.connection = connection
46
47 class DatabaseOperations(BaseDatabaseOperations):
48     def quote_name(self, name):
49         return name
50
51 class DatabaseWrapper(BaseDatabaseWrapper):
52     def __init__(self, *args, **kwargs):
53         super(DatabaseWrapper, self).__init__(*args, **kwargs)
54
55         self.charset = "utf-8"
56         self.features = DatabaseFeatures(self)
57         self.ops = DatabaseOperations()
58
59     def close(self):
60         pass
61
62     def _cursor(self):
63         if self.connection is None:
64             self.connection = ldap.initialize(self.settings_dict['NAME'])
65             self.connection.simple_bind_s(
66                 self.settings_dict['USER'],
67                 self.settings_dict['PASSWORD'])
68         return DatabaseCursor(self.connection)
69
70     def _rollback(self):
71         pass
72
73     def add_s(self, dn, modlist):
74         cursor = self._cursor()
75         return cursor.connection.add_s(dn.encode(self.charset), modlist)
76
77     def delete_s(self, dn):
78         cursor = self._cursor()
79         return cursor.connection.delete_s(dn.encode(self.charset))
80
81     def modify_s(self, dn, modlist):
82         cursor = self._cursor()
83         return cursor.connection.modify_s(dn.encode(self.charset), modlist)
84
85     def rename_s(self, dn, newrdn):
86         cursor = self._cursor()
87         return cursor.connection.rename_s(dn.encode(self.charset), newrdn.encode(self.charset))
88
89     def search_s(self, base, scope, filterstr, attrlist):
90         cursor = self._cursor()
91         results = cursor.connection.search_s(base, scope, filterstr.encode(self.charset), attrlist)
92         output = []
93         for dn, attrs in results:
94             output.append((dn.decode(self.charset), attrs))
95         return output
96