Skip to content

Commit

Permalink
SCKAN-341 fix: Update isOwner permission to check for related entity …
Browse files Browse the repository at this point in the history
…ownership only on creation
  • Loading branch information
afonsobspinto committed Nov 6, 2024
1 parent 64398c9 commit f2a77f9
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 22 deletions.
23 changes: 2 additions & 21 deletions backend/composer/api/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def has_permission(self, request, view):
if request.method in permissions.SAFE_METHODS:
return True

# Checks if creator is the owner of the related entity (if related entity exists)
if request.method == 'POST':
# If creating a new instance, ensure related entity ownership
if request.method == 'POST' and view.action == 'create':
return check_related_entity_ownership(request)

# For unsafe methods (PATCH, PUT, DELETE), allow only authenticated users
Expand Down Expand Up @@ -59,25 +59,6 @@ def has_object_permission(self, request, view, obj):
return obj.connectivity_statement.owner == request.user


class IsSentenceOrStatementOwnerOrSystemUserOrReadOnly(permissions.BasePermission):
"""
Custom permission to allow:
- System user to bypass all checks.
- Only the owner of a sentence or connectivity statement can create a note.
"""

def has_permission(self, request, view):
# Allow system user to bypass all checks
if request.user.username == 'system' and request.user.is_staff:
return True

# Allow read-only access (GET, HEAD, OPTIONS)
if request.method in permissions.SAFE_METHODS:
return True

# For POST (create), PUT, PATCH (update), or DELETE, check ownership
return check_related_entity_ownership(request)


def check_related_entity_ownership(request):
"""
Expand Down
1 change: 0 additions & 1 deletion backend/composer/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
BaseConnectivityStatementSerializer,
)
from .permissions import (
IsSentenceOrStatementOwnerOrSystemUserOrReadOnly,
IsStaffUserIfExportedStateInConnectivityStatement,
IsOwnerOrAssignOwnerOrCreateOrReadOnly,
IsOwnerOfConnectivityStatementOrReadOnly,
Expand Down

0 comments on commit f2a77f9

Please sign in to comment.