Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
29136ef2
Commit
29136ef2
authored
Feb 10, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enforce PUT-as-create permissions
parent
84a1896b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
0 deletions
+25
-0
rest_framework/mixins.py
+5
-0
rest_framework/tests/permissions.py
+20
-0
No files found.
rest_framework/mixins.py
View file @
29136ef2
...
@@ -9,6 +9,7 @@ from __future__ import unicode_literals
...
@@ -9,6 +9,7 @@ from __future__ import unicode_literals
from
django.http
import
Http404
from
django.http
import
Http404
from
rest_framework
import
status
from
rest_framework
import
status
from
rest_framework.response
import
Response
from
rest_framework.response
import
Response
from
rest_framework.request
import
clone_request
class
CreateModelMixin
(
object
):
class
CreateModelMixin
(
object
):
...
@@ -90,6 +91,10 @@ class UpdateModelMixin(object):
...
@@ -90,6 +91,10 @@ class UpdateModelMixin(object):
try
:
try
:
self
.
object
=
self
.
get_object
()
self
.
object
=
self
.
get_object
()
except
Http404
:
except
Http404
:
# If this is a PUT-as-create operation, we need to ensure that
# we have relevant permissions, as if this was a POST request.
if
not
self
.
has_permission
(
clone_request
(
request
,
'POST'
)):
self
.
permission_denied
(
self
.
request
)
created
=
True
created
=
True
success_status_code
=
status
.
HTTP_201_CREATED
success_status_code
=
status
.
HTTP_201_CREATED
else
:
else
:
...
...
rest_framework/tests/permissions.py
View file @
29136ef2
...
@@ -44,9 +44,14 @@ class ModelPermissionsIntegrationTests(TestCase):
...
@@ -44,9 +44,14 @@ class ModelPermissionsIntegrationTests(TestCase):
Permission
.
objects
.
get
(
codename
=
'change_basicmodel'
),
Permission
.
objects
.
get
(
codename
=
'change_basicmodel'
),
Permission
.
objects
.
get
(
codename
=
'delete_basicmodel'
)
Permission
.
objects
.
get
(
codename
=
'delete_basicmodel'
)
]
]
user
=
User
.
objects
.
create_user
(
'updateonly'
,
'updateonly@example.com'
,
'password'
)
user
.
user_permissions
=
[
Permission
.
objects
.
get
(
codename
=
'change_basicmodel'
),
]
self
.
permitted_credentials
=
basic_auth_header
(
'permitted'
,
'password'
)
self
.
permitted_credentials
=
basic_auth_header
(
'permitted'
,
'password'
)
self
.
disallowed_credentials
=
basic_auth_header
(
'disallowed'
,
'password'
)
self
.
disallowed_credentials
=
basic_auth_header
(
'disallowed'
,
'password'
)
self
.
updateonly_credentials
=
basic_auth_header
(
'updateonly'
,
'password'
)
BasicModel
(
text
=
'foo'
)
.
save
()
BasicModel
(
text
=
'foo'
)
.
save
()
...
@@ -87,3 +92,18 @@ class ModelPermissionsIntegrationTests(TestCase):
...
@@ -87,3 +92,18 @@ class ModelPermissionsIntegrationTests(TestCase):
request
=
factory
.
delete
(
'/1'
,
HTTP_AUTHORIZATION
=
self
.
disallowed_credentials
)
request
=
factory
.
delete
(
'/1'
,
HTTP_AUTHORIZATION
=
self
.
disallowed_credentials
)
response
=
instance_view
(
request
,
pk
=
1
)
response
=
instance_view
(
request
,
pk
=
1
)
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_403_FORBIDDEN
)
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_403_FORBIDDEN
)
def
test_has_put_as_create_permissions
(
self
):
# User only has update permissions - should be able to update an entity.
request
=
factory
.
put
(
'/1'
,
json
.
dumps
({
'text'
:
'foobar'
}),
content_type
=
'application/json'
,
HTTP_AUTHORIZATION
=
self
.
updateonly_credentials
)
response
=
instance_view
(
request
,
pk
=
'1'
)
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_200_OK
)
# But if PUTing to a new entity, permission should be denied.
request
=
factory
.
put
(
'/2'
,
json
.
dumps
({
'text'
:
'foobar'
}),
content_type
=
'application/json'
,
HTTP_AUTHORIZATION
=
self
.
updateonly_credentials
)
response
=
instance_view
(
request
,
pk
=
'2'
)
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_403_FORBIDDEN
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment