-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
authorization.py
216 lines (159 loc) · 6.83 KB
/
authorization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from __future__ import unicode_literals
from tastypie.exceptions import TastypieError, Unauthorized
from tastypie.compat import get_module_name
class Authorization(object):
"""
A base class that provides no permissions checking.
"""
def __get__(self, instance, owner):
"""
Makes ``Authorization`` a descriptor of ``ResourceOptions`` and creates
a reference to the ``ResourceOptions`` object that may be used by
methods of ``Authorization``.
"""
self.resource_meta = instance
return self
def apply_limits(self, request, object_list):
"""
Deprecated.
FIXME: REMOVE BEFORE 1.0
"""
raise TastypieError("Authorization classes no longer support `apply_limits`. Please update to using `read_list`.")
def read_list(self, object_list, bundle):
"""
Returns a list of all the objects a user is allowed to read.
Should return an empty list if none are allowed.
Returns the entire list by default.
"""
return object_list
def read_detail(self, object_list, bundle):
"""
Returns either ``True`` if the user is allowed to read the object in
question or throw ``Unauthorized`` if they are not.
Returns ``True`` by default.
"""
return True
def create_list(self, object_list, bundle):
"""
Unimplemented, as Tastypie never creates entire new lists, but
present for consistency & possible extension.
"""
raise NotImplementedError("Tastypie has no way to determine if all objects should be allowed to be created.")
def create_detail(self, object_list, bundle):
"""
Returns either ``True`` if the user is allowed to create the object in
question or throw ``Unauthorized`` if they are not.
Returns ``True`` by default.
"""
return True
def update_list(self, object_list, bundle):
"""
Returns a list of all the objects a user is allowed to update.
Should return an empty list if none are allowed.
Returns the entire list by default.
"""
return object_list
def update_detail(self, object_list, bundle):
"""
Returns either ``True`` if the user is allowed to update the object in
question or throw ``Unauthorized`` if they are not.
Returns ``True`` by default.
"""
return True
def delete_list(self, object_list, bundle):
"""
Returns a list of all the objects a user is allowed to delete.
Should return an empty list if none are allowed.
Returns the entire list by default.
"""
return object_list
def delete_detail(self, object_list, bundle):
"""
Returns either ``True`` if the user is allowed to delete the object in
question or throw ``Unauthorized`` if they are not.
Returns ``True`` by default.
"""
return True
class ReadOnlyAuthorization(Authorization):
"""
Default Authentication class for ``Resource`` objects.
Only allows ``GET`` requests.
"""
def read_list(self, object_list, bundle):
return object_list
def read_detail(self, object_list, bundle):
return True
def create_list(self, object_list, bundle):
return []
def create_detail(self, object_list, bundle):
raise Unauthorized("You are not allowed to access that resource.")
def update_list(self, object_list, bundle):
return []
def update_detail(self, object_list, bundle):
raise Unauthorized("You are not allowed to access that resource.")
def delete_list(self, object_list, bundle):
return []
def delete_detail(self, object_list, bundle):
raise Unauthorized("You are not allowed to access that resource.")
class DjangoAuthorization(Authorization):
"""
Uses permission checking from ``django.contrib.auth`` to map
``POST / PUT / DELETE / PATCH`` to their equivalent Django auth
permissions.
Both the list & detail variants simply check the model they're based
on, as that's all the more granular Django's permission setup gets.
"""
# By default, following `ModelAdmin` "convention", `app.change_model` is used
# `django.contrib.auth.models.Permission` as perm code for viewing and updating.
# https://docs.djangoproject.com/es/1.9/topics/auth/default/#permissions-and-authorization
READ_PERM_CODE = 'change'
def base_checks(self, request, model_klass):
# If it doesn't look like a model, we can't check permissions.
if not model_klass or not getattr(model_klass, '_meta', None):
return False
# User must be logged in to check permissions.
if not hasattr(request, 'user'):
return False
return model_klass
def check_user_perm(self, user, permission, obj_or_list):
return user.has_perm(permission)
def perm_list_checks(self, request, code, obj_list):
klass = self.base_checks(request, obj_list.model)
if klass is False:
return []
permission = '%s.%s_%s' % (
klass._meta.app_label,
code,
get_module_name(klass._meta)
)
if self.check_user_perm(request.user, permission, obj_list):
return obj_list
return obj_list.none()
def perm_obj_checks(self, request, code, obj):
klass = self.base_checks(request, obj.__class__)
if klass is False:
raise Unauthorized("You are not allowed to access that resource.")
permission = '%s.%s_%s' % (
klass._meta.app_label,
code,
get_module_name(klass._meta)
)
if self.check_user_perm(request.user, permission, obj):
return True
raise Unauthorized("You are not allowed to access that resource.")
def read_list(self, object_list, bundle):
return self.perm_list_checks(bundle.request, self.READ_PERM_CODE, object_list)
def read_detail(self, object_list, bundle):
return self.perm_obj_checks(bundle.request, self.READ_PERM_CODE, bundle.obj)
def create_list(self, object_list, bundle):
return self.perm_list_checks(bundle.request, 'add', object_list)
def create_detail(self, object_list, bundle):
return self.perm_obj_checks(bundle.request, 'add', bundle.obj)
def update_list(self, object_list, bundle):
return self.perm_list_checks(bundle.request, 'change', object_list)
def update_detail(self, object_list, bundle):
return self.perm_obj_checks(bundle.request, 'change', bundle.obj)
def delete_list(self, object_list, bundle):
return self.perm_list_checks(bundle.request, 'delete', object_list)
def delete_detail(self, object_list, bundle):
return self.perm_obj_checks(bundle.request, 'delete', bundle.obj)