|
1
|
|
|
# coding=utf-8 |
|
2
|
|
|
import collections |
|
3
|
|
|
from permission.utils.permissions import get_app_perms |
|
4
|
|
|
from permission.utils.permissions import get_model_perms |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
class PermissionHandler(object): |
|
8
|
|
|
""" |
|
9
|
|
|
Abstract permission handler class |
|
10
|
|
|
""" |
|
11
|
|
|
_includes = None |
|
12
|
|
|
_excludes = None |
|
13
|
|
|
|
|
14
|
|
|
@property |
|
15
|
|
|
def includes(self): |
|
16
|
|
|
return self._includes |
|
17
|
|
|
|
|
18
|
|
|
@includes.setter |
|
19
|
|
|
def includes(self, value): |
|
20
|
|
|
# clear cache |
|
21
|
|
|
if hasattr(self, '_perms_cache'): |
|
22
|
|
|
del self._perms_cache |
|
23
|
|
|
self._includes = value |
|
24
|
|
|
|
|
25
|
|
|
@property |
|
26
|
|
|
def excludes(self): |
|
27
|
|
|
return self._excludes |
|
28
|
|
|
|
|
29
|
|
|
@excludes.setter |
|
30
|
|
|
def excludes(self, value): |
|
31
|
|
|
# clear cache |
|
32
|
|
|
if hasattr(self, '_perms_cache'): |
|
33
|
|
|
del self._perms_cache |
|
34
|
|
|
self._excludes = value |
|
35
|
|
|
|
|
36
|
|
|
def __init__(self, model_or_app_label): |
|
37
|
|
|
""" |
|
38
|
|
|
Constructor |
|
39
|
|
|
|
|
40
|
|
|
Parameters |
|
41
|
|
|
---------- |
|
42
|
|
|
model_or_app_label : django model class or string |
|
43
|
|
|
A django model class or application label string. |
|
44
|
|
|
Use django model class for model level permission and application |
|
45
|
|
|
label for application level permission. |
|
46
|
|
|
""" |
|
47
|
|
|
if isinstance(model_or_app_label, str): |
|
48
|
|
|
self.app_label = model_or_app_label |
|
49
|
|
|
self.model = None |
|
50
|
|
|
if self.includes is None: |
|
51
|
|
|
self.includes = self._get_app_perms |
|
52
|
|
|
else: |
|
53
|
|
|
self.app_label = model_or_app_label._meta.app_label |
|
54
|
|
|
self.model = model_or_app_label |
|
55
|
|
|
self.model._permission_handler = self |
|
56
|
|
|
if self.includes is None: |
|
57
|
|
|
self.includes = self._get_model_perms |
|
58
|
|
|
|
|
59
|
|
|
def _get_app_perms(self, *args): |
|
60
|
|
|
""" |
|
61
|
|
|
Get permissions related to the application specified in constructor |
|
62
|
|
|
|
|
63
|
|
|
Returns |
|
64
|
|
|
------- |
|
65
|
|
|
set |
|
66
|
|
|
A set instance of `app_label.codename` formatted permission strings |
|
67
|
|
|
""" |
|
68
|
|
|
if not hasattr(self, '_app_perms_cache'): |
|
69
|
|
|
self._app_perms_cache = get_app_perms(self.app_label) |
|
70
|
|
|
return self._app_perms_cache |
|
71
|
|
|
|
|
72
|
|
|
def _get_model_perms(self, *args): |
|
73
|
|
|
""" |
|
74
|
|
|
Get permissions related to the model specified in constructor |
|
75
|
|
|
|
|
76
|
|
|
Returns |
|
77
|
|
|
------- |
|
78
|
|
|
set |
|
79
|
|
|
A set instance of `app_label.codename` formatted permission strings |
|
80
|
|
|
""" |
|
81
|
|
|
if not hasattr(self, '_model_perms_cache'): |
|
82
|
|
|
if self.model is None: |
|
83
|
|
|
self._model_perms_cache = set() |
|
84
|
|
|
else: |
|
85
|
|
|
self._model_perms_cache = get_model_perms(self.model) |
|
86
|
|
|
return self._model_perms_cache |
|
87
|
|
|
|
|
88
|
|
|
def get_supported_permissions(self): |
|
89
|
|
|
""" |
|
90
|
|
|
Get permissions which this handler can treat. |
|
91
|
|
|
Specified with :attr:`includes` and :attr:`excludes` of this instance. |
|
92
|
|
|
|
|
93
|
|
|
Returns |
|
94
|
|
|
------- |
|
95
|
|
|
set |
|
96
|
|
|
A set instance of `app_label.codename` formatted permission strings |
|
97
|
|
|
""" |
|
98
|
|
|
if not hasattr(self, '_perms_cache'): |
|
99
|
|
|
if (self.includes and |
|
100
|
|
|
isinstance(self.includes, collections.Callable)): |
|
101
|
|
|
includes = self.includes(self) |
|
102
|
|
|
else: |
|
103
|
|
|
includes = self.includes or [] |
|
104
|
|
|
if (self.excludes and |
|
105
|
|
|
isinstance(self.excludes, collections.Callable)): |
|
106
|
|
|
excludes = self.excludes(self) |
|
107
|
|
|
else: |
|
108
|
|
|
excludes = self.excludes or [] |
|
109
|
|
|
includes = set(includes) |
|
110
|
|
|
excludes = set(excludes) |
|
111
|
|
|
includes = includes.difference(excludes) |
|
112
|
|
|
self._perms_cache = includes |
|
113
|
|
|
return self._perms_cache |
|
114
|
|
|
|
|
115
|
|
|
def get_supported_app_labels(self): |
|
116
|
|
|
""" |
|
117
|
|
|
Get app labels which this handler can treat. |
|
118
|
|
|
Specified with :attr:`includes` and :attr:`excludes` of this instance. |
|
119
|
|
|
|
|
120
|
|
|
Returns |
|
121
|
|
|
------- |
|
122
|
|
|
set |
|
123
|
|
|
A set instance of app_label |
|
124
|
|
|
""" |
|
125
|
|
|
get_app_label = lambda x: x.split(".", 1)[0] |
|
126
|
|
|
if not hasattr(self, '_app_labels_cache'): |
|
127
|
|
|
perms = self.get_supported_permissions() |
|
128
|
|
|
self._app_labels_cache = set([get_app_label(x) for x in perms]) |
|
129
|
|
|
return self._app_labels_cache |
|
130
|
|
|
|
|
131
|
|
|
def has_perm(self, user_obj, perm, obj=None): |
|
132
|
|
|
""" |
|
133
|
|
|
Check if user have permission (of object) |
|
134
|
|
|
|
|
135
|
|
|
Parameters |
|
136
|
|
|
---------- |
|
137
|
|
|
user_obj : django user model instance |
|
138
|
|
|
A django user model instance which be checked |
|
139
|
|
|
perm : string |
|
140
|
|
|
`app_label.codename` formatted permission string |
|
141
|
|
|
obj : None or django model instance |
|
142
|
|
|
None or django model instance for object permission |
|
143
|
|
|
|
|
144
|
|
|
Returns |
|
145
|
|
|
------- |
|
146
|
|
|
boolean |
|
147
|
|
|
Whether the specified user have specified permission (of specified |
|
148
|
|
|
object). |
|
149
|
|
|
|
|
150
|
|
|
.. note:: |
|
151
|
|
|
Sub class must override this method. |
|
152
|
|
|
""" |
|
153
|
|
|
raise NotImplementedError(( |
|
154
|
|
|
"'%s' does not override `has_perm(user_obj, perm, obj=None)` " |
|
155
|
|
|
"method. Sub class must override this method." |
|
156
|
|
|
) % self.__class__) |
|
157
|
|
|
|
|
158
|
|
|
def has_module_perms(self, user_obj, app_label): |
|
159
|
|
|
""" |
|
160
|
|
|
Check if user have permission of specified app |
|
161
|
|
|
|
|
162
|
|
|
Parameters |
|
163
|
|
|
---------- |
|
164
|
|
|
user_obj : django user model instance |
|
165
|
|
|
A django user model instance which be checked |
|
166
|
|
|
app_label : string |
|
167
|
|
|
Django application name |
|
168
|
|
|
|
|
169
|
|
|
Returns |
|
170
|
|
|
------- |
|
171
|
|
|
boolean |
|
172
|
|
|
Whether the specified user have any permissions of specified app |
|
173
|
|
|
|
|
174
|
|
|
""" |
|
175
|
|
|
cache_name = "_has_module_perms_%s_%s_cache" % (app_label, user_obj.pk) |
|
176
|
|
|
if hasattr(self, cache_name): |
|
177
|
|
|
return getattr(self, cache_name) |
|
178
|
|
|
if self.app_label != app_label: |
|
179
|
|
|
setattr(self, cache_name, False) |
|
180
|
|
|
else: |
|
181
|
|
|
for permission in self.get_supported_permissions(): |
|
182
|
|
|
if user_obj.has_perm(permission): |
|
183
|
|
|
setattr(self, cache_name, True) |
|
184
|
|
|
return True |
|
185
|
|
|
setattr(self, cache_name, False) |
|
186
|
|
|
return False |
|
187
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
class LogicalPermissionHandler(PermissionHandler): |
|
190
|
|
|
""" |
|
191
|
|
|
Permission handler class which use permission logics to determine the |
|
192
|
|
|
permission |
|
193
|
|
|
""" |
|
194
|
|
|
|
|
195
|
|
|
def __init__(self, model): |
|
196
|
|
|
""" |
|
197
|
|
|
Constructor |
|
198
|
|
|
|
|
199
|
|
|
Parameters |
|
200
|
|
|
---------- |
|
201
|
|
|
model : django model class |
|
202
|
|
|
A django model class. |
|
203
|
|
|
|
|
204
|
|
|
.. note:: |
|
205
|
|
|
LogicalPermissionHandler cannot treat application level permission |
|
206
|
|
|
""" |
|
207
|
|
|
# logical permission handler cannot treat application level permission |
|
208
|
|
|
if isinstance(model, str): |
|
209
|
|
|
raise AttributeError(( |
|
210
|
|
|
"'%s' cannot treat application level permission." |
|
211
|
|
|
) % self.__class__) |
|
212
|
|
|
super(LogicalPermissionHandler, self).__init__(model) |
|
213
|
|
|
|
|
214
|
|
|
def has_perm(self, user_obj, perm, obj=None): |
|
215
|
|
|
""" |
|
216
|
|
|
Check if user have permission (of object) based on |
|
217
|
|
|
specified models's ``_permission_logics`` attribute. |
|
218
|
|
|
|
|
219
|
|
|
The result will be stored in user_obj as a cache to reduce method call. |
|
220
|
|
|
|
|
221
|
|
|
Parameters |
|
222
|
|
|
---------- |
|
223
|
|
|
user_obj : django user model instance |
|
224
|
|
|
A django user model instance which be checked |
|
225
|
|
|
perm : string |
|
226
|
|
|
`app_label.codename` formatted permission string |
|
227
|
|
|
obj : None or django model instance |
|
228
|
|
|
None or django model instance for object permission |
|
229
|
|
|
|
|
230
|
|
|
Returns |
|
231
|
|
|
------- |
|
232
|
|
|
boolean |
|
233
|
|
|
Whether the specified user have specified permission (of specified |
|
234
|
|
|
object). |
|
235
|
|
|
""" |
|
236
|
|
|
if perm not in self.get_supported_permissions(): |
|
237
|
|
|
return False |
|
238
|
|
|
# use cache to reduce method call |
|
239
|
|
|
CACHE_NAME = '_logical_perms_cache' |
|
240
|
|
|
if not hasattr(user_obj, CACHE_NAME): |
|
241
|
|
|
setattr(user_obj, CACHE_NAME, {}) |
|
242
|
|
|
cache = getattr(user_obj, CACHE_NAME) |
|
243
|
|
|
cachekey = "%s %s" % (perm, hash(obj)) |
|
244
|
|
|
if cachekey not in cache: |
|
245
|
|
|
cache[cachekey] = self._has_perm(user_obj, perm, obj) |
|
246
|
|
|
return cache[cachekey] |
|
247
|
|
|
|
|
248
|
|
|
def _has_perm(self, user_obj, perm, obj=None): |
|
249
|
|
|
if perm not in self.get_supported_permissions(): |
|
250
|
|
|
return False |
|
251
|
|
|
for permission_logic in getattr(self.model, |
|
252
|
|
|
'_permission_logics', set()): |
|
253
|
|
|
if permission_logic.has_perm(user_obj, perm, obj): |
|
254
|
|
|
return True |
|
255
|
|
|
return False |
|
256
|
|
|
|