Skip to content

Commit

Permalink
Merge pull request CenterForOpenScience#10536 from cslzchen/feature/c…
Browse files Browse the repository at this point in the history
…edar-bug-fix

[Cedar] Bug-fix: Fix Permission
  • Loading branch information
cslzchen authored Feb 6, 2024
2 parents 589ac54 + 07bf4b3 commit abdcb64
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 7 deletions.
6 changes: 3 additions & 3 deletions api/cedar_metadata_records/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class CedarMetadataRecordPermission(permissions.BasePermission):
def has_object_permission(self, request, view, obj):

assert isinstance(obj, CedarMetadataRecord), 'obj must be a CedarMetadataRecord'

auth = get_user_auth(request)

permission_source = obj.guid.referent
Expand All @@ -24,6 +23,7 @@ def has_object_permission(self, request, view, obj):
return False

if request.method in permissions.SAFE_METHODS:
is_public = permission_source.is_public and obj.is_published
return is_public or permission_source.can_view(auth)
if not obj.is_published:
return permission_source.can_edit(auth)
return permission_source.is_public or permission_source.can_view(auth)
return permission_source.can_edit(auth)
10 changes: 9 additions & 1 deletion api/cedar_metadata_records/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

from django.db import IntegrityError
from rest_framework import serializers as ser
from rest_framework.exceptions import PermissionDenied, NotFound

from api.base.exceptions import InvalidModelValueError, JSONAPIException
from api.base.serializers import JSONAPISerializer, LinksField, RelationshipField
from api.base.utils import absolute_reverse
from api.base.utils import absolute_reverse, get_user_auth
from api.cedar_metadata_records.utils import get_guids_related_view, get_guids_related_view_kwargs
from api.cedar_metadata_records.utils import can_create_record

from osf.exceptions import ValidationError
from osf.models import CedarMetadataRecord, CedarMetadataTemplate, Guid
Expand Down Expand Up @@ -112,6 +114,12 @@ def create(self, validated_data):
template = validated_data.pop('template')
metadata = validated_data.pop('metadata')
is_published = validated_data.pop('is_published')

auth = get_user_auth(self.context['request'])
if not can_create_record(auth, guid):
raise PermissionDenied
if not template.is_active():
raise NotFound
record = CedarMetadataRecord(guid=guid, template=template, metadata=metadata, is_published=is_published)
try:
record.save()
Expand Down
24 changes: 24 additions & 0 deletions api/cedar_metadata_records/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,27 @@ def get_guids_related_view_kwargs(obj):
return {'file_id': '<guid._id>'}
else:
raise NotImplementedError()

def can_view_record(user_auth, record):

permission_source = record.guid.referent

if isinstance(permission_source, BaseFileNode):
permission_source = permission_source.target
elif not isinstance(permission_source, (Node, Registration)):
return False

if not record.is_published:
return permission_source.can_edit(user_auth)
return permission_source.is_public or permission_source.can_view(user_auth)

def can_create_record(user_auth, guid):

permission_source = guid.referent

if isinstance(permission_source, BaseFileNode):
permission_source = permission_source.target
elif not isinstance(permission_source, (Node, Registration)):
return False

return permission_source.can_edit(user_auth)
15 changes: 12 additions & 3 deletions api/cedar_metadata_records/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
)
from api.base.versioning import PrivateVersioning
from api.base.views import JSONAPIBaseView
from api.base.utils import get_user_auth
from api.cedar_metadata_records.permissions import CedarMetadataRecordPermission
from api.cedar_metadata_records.serializers import (
CedarMetadataRecordsListSerializer,
CedarMetadataRecordsListCreateSerializer,
CedarMetadataRecordsDetailSerializer,
)
from api.cedar_metadata_records.utils import can_view_record
from framework.auth.oauth_scopes import CoreScopes

from osf.models import CedarMetadataRecord
Expand Down Expand Up @@ -54,7 +56,10 @@ def get_serializer_class(self):
return CedarMetadataRecordsListSerializer

def get_default_queryset(self):
return CedarMetadataRecord.objects.filter(is_published=True)
published_records = CedarMetadataRecord.objects.filter(is_published=True)
user_auth = get_user_auth(self.request)
record_ids = [record.id for record in published_records if can_view_record(user_auth, record)]
return CedarMetadataRecord.objects.filter(pk__in=record_ids)

def get_queryset(self):
return self.get_queryset_from_request()
Expand All @@ -79,9 +84,11 @@ class CedarMetadataRecordDetail(JSONAPIBaseView, RetrieveUpdateDestroyAPIView):

def get_object(self):
try:
return CedarMetadataRecord.objects.get(_id=self.kwargs['record_id'])
record = CedarMetadataRecord.objects.get(_id=self.kwargs['record_id'])
except CedarMetadataRecord.DoesNotExist:
raise NotFound
self.check_object_permissions(self.request, record)
return record

class CedarMetadataRecordMetadataDownload(JSONAPIBaseView, RetrieveAPIView):

Expand All @@ -102,9 +109,11 @@ class CedarMetadataRecordMetadataDownload(JSONAPIBaseView, RetrieveAPIView):

def get_object(self):
try:
return CedarMetadataRecord.objects.get(_id=self.kwargs['record_id'])
record = CedarMetadataRecord.objects.get(_id=self.kwargs['record_id'])
except CedarMetadataRecord.DoesNotExist:
raise NotFound
self.check_object_permissions(self.request, record)
return record

def get_serializer_class(self):
return None
Expand Down
3 changes: 3 additions & 0 deletions osf/models/cedar_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def __unicode__(self):
def get_semantic_iri(self):
return self.cedar_id

def is_active(self):
return self.active


class CedarMetadataRecord(ObjectIDMixin, BaseModel):

Expand Down

0 comments on commit abdcb64

Please sign in to comment.