permission.backends.role_backend: 61 total statements, 61.4% covered

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

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

Stats: 35 executed, 22 missed, 4 excluded, 50 ignored

  1. # vim: set fileencoding=utf-8 :
  2. """
  3. Authentication backends for role system
  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 permission.models import Role
  27. __all__ = ('RoleBackend',)
  28. class RoleBackend(object):
  29. """Authentication backend for role system"""
  30. supports_object_permissions = False
  31. supports_anonymous_user = False
  32. supports_inactive_user = True
  33. def authenticate(self, username, password):
  34. """This backend is only for checking permission"""
  35. return None
  36. def get_all_roles(self, user_obj):
  37. return Role.objects.filter_by_user(user_obj)
  38. def get_all_permissions(self, user_obj, obj=None):
  39. if obj is not None:
  40. # do not return any permission for obj
  41. return set()
  42. return Role.objects.get_all_permissions_of_user(user_obj)
  43. def has_role(self, user_obj, role):
  44. if user_obj.is_anonymous() or not user_obj.is_active:
  45. return False
  46. return self.get_all_roles(user_obj).filter(codename=role).exists()
  47. def has_perm(self, user_obj, perm, obj=None):
  48. if user_obj.is_anonymous() or not user_obj.is_active:
  49. return False
  50. permissions = self.get_all_permissions(user_obj, obj)
  51. permissions = set(["%s.%s" % (p.content_type.app_label, p.codename) for p in permissions])
  52. if perm in permissions:
  53. return True
  54. # do not touch this permission
  55. return False
  56. def has_module_perms(self, user_obj, app_label):
  57. if not user_obj.is_active:
  58. return False
  59. permissions = self.get_all_permissions(user_obj)
  60. permissions = set(["%s.%s" % (p.content_type.app_label, p.codename) for p in permissions])
  61. for perm in permissions:
  62. if perm[:perm.index('.')] == app_label:
  63. return True
  64. return False
  65. #
  66. # Extend User class
  67. #
  68. from django.contrib import auth
  69. from django.contrib.auth.models import User
  70. def _user_has_role(user, role, obj):
  71. anon = user.is_anonymous()
  72. active = user.is_active
  73. for backend in auth.get_backends():
  74. if anon or active or backend.supports_inactive_user:
  75. if hasattr(backend, 'has_role'):
  76. if obj is not None:
  77. if backend.has_role(user, role, obj):
  78. return True
  79. else:
  80. if backend.has_role(user, role):
  81. return True
  82. return False
  83. def _user_get_all_roles(user):
  84. anon = user.is_anonymous()
  85. active = user.is_active
  86. for backend in auth.get_backends():
  87. if anon or active or backend.supports_inactive_user:
  88. if hasattr(backend, 'get_all_roles'):
  89. return backend.get_all_roles(user)
  90. return None
  91. User.has_role = _user_has_role
  92. User.roles = property(_user_get_all_roles)