permission.backends.permission_backend: 22 total statements, 75.0% covered

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

Source file: /home/alisue/Dropbox/Codes/django-permission/permission/backends/permission_backend.py

Stats: 15 executed, 5 missed, 2 excluded, 65 ignored

  1. # vim: set fileencoding=utf-8 :
  2. """
  3. Authentication backends for checking permissions
  4. Method:
  5. get_permission_handler
  6. get permission handler of the model from registry
  7. Class:
  8. PermissionBackend
  9. Authentication backend for checking permissions
  10. AUTHOR:
  11. lambdalisue[Ali su ae] (lambdalisue@hashnote.net)
  12. License:
  13. The MIT License (MIT)
  14. Copyright (c) 2012 Alisue allright reserved.
  15. Permission is hereby granted, free of charge, to any person obtaining a copy
  16. of this software and associated documentation files (the "Software"), to
  17. deal in the Software without restriction, including without limitation the
  18. rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  19. sell copies of the Software, and to permit persons to whom the Software is
  20. furnished to do so, subject to the following conditions:
  21. The above copyright notice and this permission notice shall be included in
  22. all copies or substantial portions of the Software.
  23. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  28. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  29. IN THE SOFTWARE.
  30. """
  31. from __future__ import with_statement
  32. from permission.handlers import registry
  33. __all__ = ('PermissionBackend',)
  34. class PermissionBackend(object):
  35. """Authentication backend for cheking permissions
  36. This backend is used to check permissions. The permissions
  37. are handled with ``PermissionHandler`` which have to be registered in
  38. ``fluidpermission.handlers.registry`` before use.
  39. ``has_perm(user_obj, perm, obj=None)`` method of detected model's
  40. ``PermissionHandler`` will be used to cheking process.
  41. If no model was detected or no handler was registered, this backend
  42. does not touch that permission and return ``None`` to pass the permission
  43. checking process to downstream backends.
  44. """
  45. supports_object_permissions = True
  46. supports_anonymous_user = True
  47. supports_inactive_user = True
  48. def authenticate(self, username, password):
  49. """This backend is only for checking permission"""
  50. return None
  51. def has_perm(self, user_obj, perm, obj=None):
  52. """check permission"""
  53. # get permission handlers fot this perm
  54. handlers = registry.get_handlers(perm)
  55. for handler in handlers:
  56. if handler.has_perm(user_obj, perm, obj=obj):
  57. return True
  58. # do not touch this permission
  59. return False
  60. def has_module_perms(self, user_obj, app_label):
  61. # get permission handlers fot this perm
  62. handlers = registry.get_module_handlers(app_label)
  63. for handler in handlers:
  64. if handler.has_module_perms(user_obj, app_label):
  65. return True
  66. return False