|
1 |
| -from rest_framework.permissions import IsAuthenticated |
| 1 | +from rest_framework.permissions import BasePermission |
2 | 2 |
|
3 | 3 |
|
4 |
| -class PublicDetailPrivateListing(IsAuthenticated): |
| 4 | +class PublicDetailPrivateListing(BasePermission): |
5 | 5 |
|
6 | 6 | """
|
7 | 7 | Permission class for our custom use case.
|
8 | 8 |
|
9 | 9 | * Always give permission for a ``detail`` request
|
10 | 10 | * Only give permission for ``listing`` request if user is admin of the project
|
| 11 | + """ |
| 12 | + |
| 13 | + def has_permission(self, request, view): |
| 14 | + if view.detail: |
| 15 | + return True |
| 16 | + |
| 17 | + project = view._get_parent_project() |
| 18 | + if view.has_admin_permission(request.user, project): |
| 19 | + return True |
| 20 | + |
| 21 | + |
| 22 | +class ListCreateProject(BasePermission): |
| 23 | + |
| 24 | + """ |
| 25 | + Permission class to grant projects listing and project creation. |
| 26 | +
|
11 | 27 | * Allow access to ``/projects`` (user's projects listing)
|
12 | 28 | """
|
13 | 29 |
|
14 | 30 | def has_permission(self, request, view):
|
15 |
| - is_authenticated = super().has_permission(request, view) |
16 |
| - if is_authenticated: |
17 |
| - if view.basename == 'projects' and any([ |
18 |
| - view.action == 'list', |
19 |
| - view.action == 'create', # used to create Form in BrowsableAPIRenderer |
20 |
| - view.action is None, # needed for BrowsableAPIRenderer |
21 |
| - ]): |
22 |
| - # hitting ``/projects/``, allowing |
23 |
| - return True |
24 |
| - |
25 |
| - if view.detail: |
26 |
| - return True |
27 |
| - |
28 |
| - project = view._get_parent_project() |
29 |
| - if view.has_admin_permission(request.user, project): |
30 |
| - return True |
31 |
| - |
32 |
| - return False |
| 31 | + if view.basename == 'projects' and any([ |
| 32 | + view.action == 'list', |
| 33 | + view.action == 'create', # used to create Form in BrowsableAPIRenderer |
| 34 | + view.action is None, # needed for BrowsableAPIRenderer |
| 35 | + ]): |
| 36 | + # hitting ``/projects/``, allowing |
| 37 | + return True |
0 commit comments