base.py~ 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. import ldap
  35. import django
  36. from django.db.backends import BaseDatabaseFeatures, BaseDatabaseOperations, BaseDatabaseWrapper
  37. from django.db.backends.creation import BaseDatabaseCreation
  38. class DatabaseCreation(BaseDatabaseCreation):
  39. def create_test_db(self, verbosity=1, autoclobber=False):
  40. """
  41. Creates a test database, prompting the user for confirmation if the
  42. database already exists. Returns the name of the test database created.
  43. """
  44. pass
  45. def destroy_test_db(self, old_database_name, verbosity=1):
  46. """
  47. Destroy a test database, prompting the user for confirmation if the
  48. database already exists. Returns the name of the test database created.
  49. """
  50. pass
  51. class DatabaseCursor(object):
  52. def __init__(self, ldap_connection):
  53. self.connection = ldap_connection
  54. class DatabaseFeatures(BaseDatabaseFeatures):
  55. def __init__(self, connection):
  56. self.connection = connection
  57. class DatabaseOperations(BaseDatabaseOperations):
  58. compiler_module = "ldapdb.backends.ldap.compiler"
  59. def quote_name(self, name):
  60. return name
  61. class DatabaseWrapper(BaseDatabaseWrapper):
  62. def __init__(self, *args, **kwargs):
  63. super(DatabaseWrapper, self).__init__(*args, **kwargs)
  64. self.charset = "utf-8"
  65. self.creation = DatabaseCreation(self)
  66. self.features = DatabaseFeatures(self)
  67. if django.VERSION > (1, 4):
  68. self.ops = DatabaseOperations(self)
  69. else:
  70. self.ops = DatabaseOperations()
  71. self.settings_dict['SUPPORTS_TRANSACTIONS'] = False
  72. def close(self):
  73. pass
  74. def _commit(self):
  75. pass
  76. def _cursor(self):
  77. if self.connection is None:
  78. self.connection = ldap.initialize(self.settings_dict['NAME'])
  79. self.connection.simple_bind_s(
  80. self.settings_dict['USER'],
  81. self.settings_dict['PASSWORD'])
  82. return DatabaseCursor(self.connection)
  83. def _rollback(self):
  84. pass
  85. def add_s(self, dn, modlist):
  86. cursor = self._cursor()
  87. return cursor.connection.add_s(dn.encode(self.charset), modlist)
  88. def delete_s(self, dn):
  89. cursor = self._cursor()
  90. return cursor.connection.delete_s(dn.encode(self.charset))
  91. def modify_s(self, dn, modlist):
  92. cursor = self._cursor()
  93. return cursor.connection.modify_s(dn.encode(self.charset), modlist)
  94. def rename_s(self, dn, newrdn):
  95. cursor = self._cursor()
  96. return cursor.connection.rename_s(dn.encode(self.charset), newrdn.encode(self.charset))
  97. def search_s(self, base, scope, filterstr='(objectClass=*)',attrlist=None):
  98. cursor = self._cursor()
  99. results = cursor.connection.search_s(base, scope, filterstr.encode(self.charset), attrlist)
  100. output = []
  101. for dn, attrs in results:
  102. output.append((dn.decode(self.charset), attrs))
  103. print filterstr
  104. return output