permission.handlers.base: 33 total statements, 63.3% covered

Generated: Thu 2012-03-01 14:22 CST

Source file: /home/alisue/Dropbox/Codes/django-permission/permission/handlers/base.py

Stats: 19 executed, 11 missed, 3 excluded, 79 ignored

  1. # vim: set fileencoding=utf-8 :
  2. """
  3. Permission handler base
  4. AUTHOR:
  5. lambdalisue[Ali su ae] (lambdalisue@hashnote.net)
  6. License:
  7. The MIT License (MIT)
  8. Copyright (c) 2012 Alisue allright reserved.
  9. Permission is hereby granted, free of charge, to any person obtaining a copy
  10. of this software and associated documentation files (the "Software"), to
  11. deal in the Software without restriction, including without limitation the
  12. rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  13. sell copies of the Software, and to permit persons to whom the Software is
  14. furnished to do so, subject to the following conditions:
  15. The above copyright notice and this permission notice shall be included in
  16. all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  23. IN THE SOFTWARE.
  24. """
  25. from __future__ import with_statement
  26. from django.contrib.auth.models import Permission
  27. __all__ = ('PermissionHandler',)
  28. class PermissionHandler(object):
  29. """Permission handler base class.
  30. You must create subclasses of this to create your custom permission handler.
  31. And you MUST DEFINE ``has_perm(user_obj, perm, obj=None)`` method to check
  32. the permission of passed ``user_obj``
  33. ``get_permissions()`` method is used to determine which permission strings are
  34. should be handled with this handler. In default, this method return the value
  35. of ``permissions`` attribute if defined. Otherwise it return the same value as
  36. ``get_model_permissions()`` method which described below.
  37. ``get_model_permissions()`` method return permission strings which is related
  38. to the ``model`` registered with this handler instance. ``get_app_permissions()``
  39. method return permission strings which is related to the app of the ``model``
  40. registered with this handler instance.
  41. ``get_permission_codename(perm)`` method return codename of ``perm``.
  42. """
  43. def __init__(self, model):
  44. self.model = model
  45. def get_app_permissions(self):
  46. """get permissions associated with app of this model"""
  47. if not hasattr(self, '_app_perm_cache'):
  48. app_label = self.model._meta.app_label
  49. qs = Permission.objects.filter(content_type__app_label=app_label)
  50. app_perms = set([u"%s.%s" % (app_label, p.codename) for p in qs.iterator()])
  51. self._app_perm_cache = app_perms
  52. return set(self._app_perm_cache)
  53. def get_model_permissions(self):
  54. """get permissions associated with this model"""
  55. if not hasattr(self, '_model_perm_cache'):
  56. app_label = self.model._meta.app_label
  57. model = self.model._meta.object_name.lower()
  58. qs = Permission.objects.filter(
  59. content_type__app_label=app_label,
  60. content_type__model=model
  61. )
  62. model_perms = set([u"%s.%s" % (app_label, p.codename) for p in qs.iterator()])
  63. self._model_perm_cache = model_perms
  64. return set(self._model_perm_cache)
  65. def get_permissions(self):
  66. """get permissions which this handler will handle"""
  67. if not hasattr(self, 'permissions'):
  68. # build permission list associated with the model
  69. self.permissions = self.get_model_permissions()
  70. return set(self.permissions)
  71. def get_permission_codename(self, perm):
  72. """get permission codename from permission string"""
  73. from permission.utils import get_permission_codename
  74. return get_permission_codename(perm)
  75. def has_perm(self, user_obj, perm, obj=None):
  76. """whether the ``user_obj`` has permission ``perm`` of ``obj``
  77. This method will be called in ``has_perm()`` method of authentication
  78. backend so you CAN NOT call ``user_obj.has_perm()`` method within
  79. this method.
  80. Subclasses must override this method to define them own checking process.
  81. """
  82. raise NotImplementedError(
  83. '"%s" does not have ``has_perm`` method. Subclasses must '
  84. 'define ``has_perm(user_obj, perm, obj=None)`` method.'
  85. )
  86. def has_module_perm(self, user_obj, app_label):
  87. return False