-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(mp): Throws a 403 exception when a user try to quote a wrong post.
1. Creates a decorator.py file in the mp module. 2. Adds the method is_participant to apply checks on view. 3. Adds this permission on the view PrivatePostAnswer. 4. Adds a test case.
- Loading branch information
1 parent
e0d295f
commit e4c7cd2
Showing
3 changed files
with
44 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# coding: utf-8 | ||
|
||
from django.core.exceptions import PermissionDenied | ||
from django.shortcuts import get_object_or_404 | ||
from zds.mp.models import PrivateTopic, PrivatePost | ||
|
||
|
||
def is_participant(func): | ||
""" | ||
Checks if the current user is a participant of the private topic specified in the URL | ||
and if the post specified in the GET parameter `cite` is on the private topic. | ||
:param func: the decorated function | ||
:return: `True` if the current user can read and write, `False` otherwise. | ||
""" | ||
def _is_participant(request, *args, **kwargs): | ||
private_topic = get_object_or_404(PrivateTopic, pk=kwargs.get('pk')) | ||
if not request.user == private_topic.author and request.user not in list(private_topic.participants.all()): | ||
raise PermissionDenied | ||
if 'cite' in request.GET: | ||
if PrivatePost.objects.filter(privatetopic=private_topic).filter(pk=request.GET.get('cite')).count() != 1: | ||
raise PermissionDenied | ||
return func(request, *args, **kwargs) | ||
return _is_participant |
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