router.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- coding: utf-8 -*-
  2. #
  3. # django-ldapdb
  4. # Copyright (c) 2009-2011, Bolloré telecom
  5. # Copyright (c) 2013, Jeremy Lainé
  6. # All rights reserved.
  7. #
  8. # See AUTHORS file for a full list of contributors.
  9. #
  10. # Redistribution and use in source and binary forms, with or without
  11. # modification, are permitted provided that the following conditions are met:
  12. #
  13. # 1. Redistributions of source code must retain the above copyright notice,
  14. # this list of conditions and the following disclaimer.
  15. #
  16. # 2. Redistributions in binary form must reproduce the above copyright
  17. # notice, this list of conditions and the following disclaimer in the
  18. # documentation and/or other materials provided with the distribution.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. # POSSIBILITY OF SUCH DAMAGE.
  31. #
  32. def is_ldap_model(model):
  33. # FIXME: there is probably a better check than testing 'base_dn'
  34. return hasattr(model, 'base_dn')
  35. class Router(object):
  36. """
  37. A router to point database operations on LDAP models to the LDAP
  38. database.
  39. NOTE: if you have more than one LDAP database, you will need to
  40. write your own router.
  41. """
  42. def __init__(self):
  43. "Find the name of the LDAP database"
  44. from django.conf import settings
  45. self.ldap_alias = None
  46. for alias, settings_dict in settings.DATABASES.items():
  47. if settings_dict['ENGINE'] == 'ldapdb.backends.ldap':
  48. self.ldap_alias = alias
  49. break
  50. def allow_syncdb(self, db, model):
  51. "Do not create tables for LDAP models"
  52. if is_ldap_model(model):
  53. return db == self.ldap_alias
  54. return None
  55. def db_for_read(self, model, **hints):
  56. "Point all operations on LDAP models to the LDAP database"
  57. if is_ldap_model(model):
  58. return self.ldap_alias
  59. return None
  60. def db_for_write(self, model, **hints):
  61. "Point all operations on LDAP models to the LDAP database"
  62. if is_ldap_model(model):
  63. return self.ldap_alias
  64. return None