Skip to content

Commit

Permalink
Move submission access check into a helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninjaclasher authored and Xyene committed Apr 14, 2020
1 parent f0db0dd commit b8aac3b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
4 changes: 2 additions & 2 deletions judge/jinja2/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ def submission_layout(submission, profile_id, user, completed_problem_ids, edita
problem_id = submission.problem_id
can_view = False

if problem_id in editable_problem_ids:
if user.has_perm('judge.view_all_submission'):
can_view = True

if profile_id == submission.user_id:
can_view = True

if user.has_perm('judge.change_submission'):
if problem_id in editable_problem_ids:
can_view = True

if submission.problem_id in completed_problem_ids:
Expand Down
16 changes: 16 additions & 0 deletions judge/models/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,22 @@ def abort(self):

abort.alters_data = True

def can_see_detail(self, user):
profile = user.profile
if not user.is_authenticated:
return False
if user.has_perm('judge.view_all_submission'):
return True
if self.user_id == profile.id:
return True
if self.problem.is_editor(profile):
return True
if (self.problem.is_public or self.problem.testers.filter(id=profile.id).exists()) and \
self.problem.submission_set.filter(user_id=profile.id, result='AC',
points=self.problem.points).exists():
return True
return False

def update_contest(self):
try:
contest = self.contest
Expand Down
16 changes: 3 additions & 13 deletions judge/views/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,9 @@ class SubmissionMixin(object):
class SubmissionDetailBase(LoginRequiredMixin, TitleMixin, SubmissionMixin, DetailView):
def get_object(self, queryset=None):
submission = super(SubmissionDetailBase, self).get_object(queryset)
profile = self.request.profile
problem = submission.problem
if self.request.user.has_perm('judge.view_all_submission'):
return submission
if submission.user_id == profile.id:
return submission
if problem.is_editor(profile):
return submission
if problem.is_public or problem.testers.filter(id=profile.id).exists():
if Submission.objects.filter(user_id=profile.id, result='AC', problem_id=problem.id,
points=problem.points).exists():
return submission
raise PermissionDenied()
if not submission.can_see_detail(self.request.user):
raise PermissionDenied()
return submission

def get_title(self):
submission = self.object
Expand Down

0 comments on commit b8aac3b

Please sign in to comment.