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
88561a4e
Commit
88561a4e
authored
Feb 11, 2012
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix up DjangoModelPermissions.
parent
0e97074f
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
33 deletions
+35
-33
djangorestframework/permissions.py
+35
-33
No files found.
djangorestframework/permissions.py
View file @
88561a4e
...
@@ -89,45 +89,47 @@ class IsUserOrIsAnonReadOnly(BasePermission):
...
@@ -89,45 +89,47 @@ class IsUserOrIsAnonReadOnly(BasePermission):
raise
_403_FORBIDDEN_RESPONSE
raise
_403_FORBIDDEN_RESPONSE
class
DjangoModelPermisson
(
BasePermission
):
class
DjangoModelPermiss
i
on
(
BasePermission
):
"""
"""
The request is authenticated against the Django user's permissions on the
The request is authenticated against the Django user's permissions on the
`Resource`'s `Model`, if the resource is a `ModelResource`.
`Resource`'s `Model`.
"""
This permission should only be used on views with a `ModelResource`.
def
check_permission
(
self
,
user
):
"""
# GET-style methods are always allowed.
# Map methods into required permission codes.
if
self
.
view
.
request
.
method
in
(
'GET'
,
'OPTIONS'
,
'HEAD'
,):
# Override this if you need to also provide 'read' permissions,
return
# or other custom behaviour.
perms_map
=
{
klass
=
self
.
view
.
resource
.
model
'GET'
:
[],
'OPTIONS'
:
[],
# If it doesn't look like a model, we can't check permissions.
'HEAD'
:
[],
if
not
klass
or
not
getattr
(
klass
,
'_meta'
,
None
):
'POST'
:
[
'
%(app_label)
s.add_
%(model_name)
s'
],
return
'PUT'
:
[
'
%(app_label)
s.change_
%(model_name)
s'
],
'PATCH'
:
[
'
%(app_label)
s.change_
%(model_name)
s'
],
# User must be logged in to check permissions.
'DELETE'
:
[
'
%(app_label)
s.delete_
%(model_name)
s'
],
if
not
hasattr
(
self
.
view
.
request
,
'user'
)
or
not
self
.
view
.
request
.
user
.
is_authenticated
():
raise
_403_FORBIDDEN_RESPONSE
permission_map
=
{
'POST'
:
[
'
%
s.add_
%
s'
],
'PUT'
:
[
'
%
s.change_
%
s'
],
'DELETE'
:
[
'
%
s.delete_
%
s'
],
'PATCH'
:
[
'
%
s.add_
%
s'
,
'
%
s.change_
%
s'
,
'
%
s.delete_
%
s'
],
}
}
permission_codes
=
[]
# If we don't recognize the HTTP method, we don't know what
def
get_required_permissions
(
self
,
method
,
model_cls
):
# permissions to check. Deny.
"""
if
self
.
view
.
request
.
method
not
in
permission_map
:
Given a model and an HTTP method, return the list of permission
raise
_403_FORBIDDEN_RESPONSE
codes that the user is required to have.
"""
kwargs
=
{
'app_label'
:
model_cls
.
_meta
.
app_label
,
'model_name'
:
model_cls
.
__name__
.
lower
()
}
try
:
return
[
perm
%
kwargs
for
perm
in
self
.
perms_map
[
method
]]
except
KeyError
:
ErrorResponse
(
status
.
HTTP_405_METHOD_NOT_ALLOWED
)
for
perm
in
permission_map
[
self
.
view
.
request
.
method
]:
def
check_permission
(
self
,
user
):
permission_codes
.
append
(
perm
%
(
klass
.
_meta
.
app_label
,
klass
.
_meta
.
module_name
))
method
=
self
.
view
.
method
model_cls
=
self
.
view
.
resource
.
model
perms
=
self
.
get_required_permissions
(
method
,
model_cls
)
if
not
self
.
view
.
request
.
user
.
has_perms
(
permission_code
s
):
if
not
user
.
has_perms
(
perm
s
):
raise
_403_FORBIDDEN_RESPONSE
raise
_403_FORBIDDEN_RESPONSE
...
...
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