Skip to content

Commit 37d6cdf

Browse files
committed
Add permissions checks to comment model methods
Handle PermissionsErrors in comment serializer methods.
1 parent 8db0c06 commit 37d6cdf

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

api/comments/serializers.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from rest_framework import serializers as ser
22
from framework.auth.core import Auth
3+
from framework.exceptions import PermissionsError
34
from website.project.model import Comment, Node
45
from rest_framework.exceptions import ValidationError, PermissionDenied
56
from api.base.exceptions import InvalidModelValueError, Conflict
@@ -78,11 +79,20 @@ def update(self, comment, validated_data):
7879
if validated_data:
7980
if 'get_content' in validated_data:
8081
content = validated_data.pop('get_content')
81-
comment.edit(content, auth=auth, save=True)
82+
try:
83+
comment.edit(content, auth=auth, save=True)
84+
except PermissionsError:
85+
raise PermissionDenied('Not authorized to edit this comment.')
8286
if validated_data.get('is_deleted', None) is True:
83-
comment.delete(auth, save=True)
87+
try:
88+
comment.delete(auth, save=True)
89+
except PermissionsError:
90+
raise PermissionDenied('Not authorized to delete this comment.')
8491
elif comment.is_deleted:
85-
comment.undelete(auth, save=True)
92+
try:
93+
comment.undelete(auth, save=True)
94+
except PermissionsError:
95+
raise PermissionDenied('Not authorized to undelete this comment.')
8696
return comment
8797

8898
def get_target_type(self, obj):
@@ -136,10 +146,11 @@ def create(self, validated_data):
136146
detail='Invalid comment target \'{}\'.'.format(target_id)
137147
)
138148
validated_data['target'] = target
139-
if node and node.can_comment(auth):
149+
validated_data['content'] = validated_data.pop('get_content')
150+
try:
140151
comment = Comment.create(auth=auth, **validated_data)
141-
else:
142-
raise PermissionDenied("Not authorized to comment on this project.")
152+
except PermissionsError:
153+
raise PermissionDenied('Not authorized to comment on this project.')
143154
return comment
144155

145156

website/project/model.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ def find_unread(cls, user, node):
234234
@classmethod
235235
def create(cls, auth, **kwargs):
236236
comment = cls(**kwargs)
237+
if not comment.node.can_comment(auth):
238+
raise PermissionsError('{0!r} does not have permission to comment on this node'.format(auth.user))
237239
comment.save()
238240

239241
comment.node.add_log(
@@ -254,6 +256,8 @@ def create(cls, auth, **kwargs):
254256
return comment
255257

256258
def edit(self, content, auth, save=False):
259+
if not self.node.can_comment(auth) or self.user._id != auth.user._id:
260+
raise PermissionsError('{0!r} does not have permission to edit this comment'.format(auth.user))
257261
self.content = content
258262
self.modified = True
259263
self.node.add_log(
@@ -271,6 +275,8 @@ def edit(self, content, auth, save=False):
271275
self.save()
272276

273277
def delete(self, auth, save=False):
278+
if not self.node.can_comment(auth) or self.user._id != auth.user._id:
279+
raise PermissionsError('{0!r} does not have permission to comment on this node'.format(auth.user))
274280
self.is_deleted = True
275281
self.node.add_log(
276282
NodeLog.COMMENT_REMOVED,
@@ -287,6 +293,8 @@ def delete(self, auth, save=False):
287293
self.save()
288294

289295
def undelete(self, auth, save=False):
296+
if not self.node.can_comment(auth) or self.user._id != auth.user._id:
297+
raise PermissionsError('{0!r} does not have permission to comment on this node'.format(auth.user))
290298
self.is_deleted = False
291299
self.node.add_log(
292300
NodeLog.COMMENT_ADDED,

0 commit comments

Comments
 (0)