-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: Consolidate access control logic with
django-guardian
- Loading branch information
Showing
20 changed files
with
103 additions
and
174 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
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
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,13 @@ | ||
from rest_framework.viewsets import ModelViewSet | ||
|
||
from uvdat.core.models import FileItem | ||
from uvdat.core.rest.filter import AccessControl | ||
from uvdat.core.rest.guardian import GuardianFilter, GuardianPermission | ||
from uvdat.core.rest.serializers import FileItemSerializer | ||
|
||
|
||
class FileItemViewSet(ModelViewSet): | ||
queryset = FileItem.objects.all() | ||
serializer_class = FileItemSerializer | ||
filter_backends = [AccessControl] | ||
permission_classes = [GuardianPermission] | ||
filter_backends = [GuardianFilter] | ||
lookup_field = 'id' |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from guardian.shortcuts import get_objects_for_user | ||
from rest_framework.filters import BaseFilterBackend | ||
from rest_framework.permissions import SAFE_METHODS, IsAuthenticated | ||
|
||
from uvdat.core import models | ||
|
||
|
||
class GuardianPermission(IsAuthenticated): | ||
def get_object_queryset(self, obj): | ||
if isinstance(obj, models.Project): | ||
return obj | ||
elif isinstance(models.Dataset): | ||
return obj.project_set | ||
elif ( | ||
isinstance(obj.models.Chart) | ||
or isinstance(obj, models.SimulationResult) | ||
or isinstance(obj, models.DerivedRegion) | ||
): | ||
return obj.project | ||
elif ( | ||
isinstance(obj, models.FileItem) | ||
or isinstance(obj, models.VectorMapLayer) | ||
or isinstance(obj, models.RasterMapLayer) | ||
or isinstance(obj, models.Network) | ||
or isinstance(obj, models.SourceRegion) | ||
): | ||
return obj.dataset.project_set | ||
elif isinstance(obj, models.NetworkEdge) or isinstance(obj, models.NetworkNode): | ||
return obj.network.dataset.project_set | ||
|
||
def has_object_permission(self, request, view, obj): | ||
if request.user.is_superuser: | ||
return True | ||
|
||
perms = ['follower'] | ||
if request.method not in SAFE_METHODS: | ||
perms.append('collaborator') | ||
if request.method == 'DELETE': | ||
perms.append('owner') | ||
return request.user.has_perm(perms, self.get_object_queryset(obj)) | ||
|
||
|
||
class GuardianFilter(BaseFilterBackend): | ||
def filter_queryset(self, request, queryset, view): | ||
if request.user.is_superuser: | ||
pass | ||
return get_objects_for_user( | ||
klass=queryset, | ||
user=request.user, | ||
perms=['follower', 'collaborator', 'owner'], | ||
) |
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.