-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1137 from open-formulieren/feature/1000-form-api-…
…permissions [#1000] Implemented and applied custom permission for FormAPI viewsets
- Loading branch information
Showing
13 changed files
with
306 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,47 @@ | ||
from rest_framework.permissions import SAFE_METHODS, BasePermission | ||
|
||
|
||
class IsStaffOrReadOnlyNoList(BasePermission): | ||
class FormAPIPermissions(BasePermission): | ||
""" | ||
The request is a staff user, or is a read-only request (but not for a 'list' action). | ||
Custom permissions for the FormViewSet and related | ||
from Github issue #1000: | ||
1) Write access (put/patch/post/delete) is ONLY given to staff users that have forms.change_form permission | ||
2) Read access (get) to everything give to staff users that have form.change_form permission | ||
3) Read access (get) to forms.form_LIST given to non-staff users that have forms.view_form permission | ||
4) Read access (get) to forms.form_DETAIL given to anonymous users | ||
x) Anybody can detail/retrieve | ||
""" | ||
|
||
def has_permission(self, request, view): | ||
if request.user and request.user.is_staff: | ||
user = request.user | ||
|
||
# x) anybody can read detail | ||
if request.method in SAFE_METHODS and view.action in ("detail", "retrieve"): | ||
return True | ||
return request.method in SAFE_METHODS and view.action != "list" | ||
|
||
# 4) anon users can only read detail (with above) | ||
elif not user or not user.is_authenticated: | ||
return False | ||
|
||
elif request.method in SAFE_METHODS: | ||
# 2) staff with change_form can read everything | ||
if user.is_staff and user.has_perm("forms.change_form"): | ||
return True | ||
# 3) non-staff with view_form can only list | ||
elif ( | ||
not user.is_staff | ||
and view.action == "list" | ||
and user.has_perm("forms.view_form") | ||
): | ||
return True | ||
else: | ||
return False | ||
else: | ||
# 1) only staff with change_form can do unsafe operations | ||
if user.is_staff and user.has_perm("forms.change_form"): | ||
return True | ||
else: | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.