Test coverage report: permission.backends.role_backend

# 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. ``
5. ``
6. `AUTHOR:`
7. `lambdalisue[Ali su ae] (lambdalisue@hashnote.net)`
8. ``
9. `License:`
10. `The MIT License (MIT)`
11. ``
12. `Copyright (c) 2012 Alisue allright reserved.`
13. ``
14. `Permission is hereby granted, free of charge, to any person obtaining a copy`
15. `of this software and associated documentation files (the "Software"), to`
16. `deal in the Software without restriction, including without limitation the`
17. `rights to use, copy, modify, merge, publish, distribute, sublicense, and/or`
18. `sell copies of the Software, and to permit persons to whom the Software is`
19. `furnished to do so, subject to the following conditions:`
20. ``
21. `The above copyright notice and this permission notice shall be included in`
22. `all copies or substantial portions of the Software.`
23. ``
24. `THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR`
25. `IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,`
26. `FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE`
27. `AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER`
28. `LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING`
29. `FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS`
30. `IN THE SOFTWARE.`
31. ``
32. `"""`
33. `from __future__ import with_statement`
34. ``
35. `from permission.models import Role`
36. ``
37. `__all__ = ('RoleBackend',)`
38. ``
39. ``
40. `class RoleBackend(object):`
41. `"""Authentication backend for role system"""`
42. `supports_object_permissions = False`
43. `supports_anonymous_user = False`
44. `supports_inactive_user = True`
45. ``
46. `def authenticate(self, username, password):`
47. `"""This backend is only for checking permission"""`
48. `return None`
49. ``
50. `def get_all_roles(self, user_obj):`
51. `return Role.objects.filter_by_user(user_obj)`
52. ``
53. `def get_all_permissions(self, user_obj, obj=None):`
54. `if obj is not None:`
55. `# do not return any permission for obj`
56. `return set()`
57. `return Role.objects.get_all_permissions_of_user(user_obj)`
58. ``
59. `def has_role(self, user_obj, role):`
60. `if user_obj.is_anonymous() or not user_obj.is_active:`
61. `return False`
62. `return self.get_all_roles(user_obj).filter(codename=role).exists()`
63. ``
64. `def has_perm(self, user_obj, perm, obj=None):`
65. `if user_obj.is_anonymous() or not user_obj.is_active:`
66. `return False`
67. `permissions = self.get_all_permissions(user_obj, obj)`
68. `permissions = set(["%s.%s" % (p.content_type.app_label, p.codename) for p in permissions])`
69. `if perm in permissions:`
70. `return True`
71. `# do not touch this permission`
72. `return False`
73. ``
74. `def has_module_perms(self, user_obj, app_label):`
75. `if not user_obj.is_active:`
76. `return False`
77. `permissions = self.get_all_permissions(user_obj)`
78. `permissions = set(["%s.%s" % (p.content_type.app_label, p.codename) for p in permissions])`
79. `for perm in permissions:`
80. `if perm[:perm.index('.')] == app_label:`
81. `return True`
82. `return False`
83. ``
84. `#`
85. `# Extend User class`
86. `#`
87. `from django.contrib import auth`
88. `from django.contrib.auth.models import User`
89. `def _user_has_role(user, role, obj):`
90. `anon = user.is_anonymous()`
91. `active = user.is_active`
92. `for backend in auth.get_backends():`
93. `if anon or active or backend.supports_inactive_user:`
94. `if hasattr(backend, 'has_role'):`
95. `if obj is not None:`
96. `if backend.has_role(user, role, obj):`
97. `return True`
98. `else:`
99. `if backend.has_role(user, role):`
100. `return True`
101. `return False`
102. `def _user_get_all_roles(user):`
103. `anon = user.is_anonymous()`
104. `active = user.is_active`
105. `for backend in auth.get_backends():`
106. `if anon or active or backend.supports_inactive_user:`
107. `if hasattr(backend, 'get_all_roles'):`
108. `return backend.get_all_roles(user)`
109. `return None`
110. `User.has_role = _user_has_role`
111. `User.roles = property(_user_get_all_roles)`