Skip to content
This repository was archived by the owner on Jan 26, 2021. It is now read-only.

Commit 880a3c8

Browse files
authored
Merge pull request #408 from necessary129/vol-acc-fix
Stop volunteers from accessing each other's urls.
2 parents 6751c3b + d750aa8 commit 880a3c8

File tree

6 files changed

+68
-35
lines changed

6 files changed

+68
-35
lines changed

vms/event/views.py

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from django.utils.decorators import method_decorator
1616
from django.shortcuts import render_to_response
1717
from django.http import Http404
18+
from volunteer.utils import vol_id_check
1819

1920
class AdministratorLoginRequiredMixin(object):
2021

@@ -121,6 +122,7 @@ def get_queryset(self):
121122

122123

123124
@login_required
125+
@vol_id_check
124126
def list_sign_up(request, volunteer_id):
125127
if request.method == 'POST':
126128
form = EventDateForm(request.POST)

vms/shift/tests/test_viewVolunteerShift.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def test_access_another_existing_volunteer_view(self):
6767
def test_access_another_nonexisting_volunteer_view(self):
6868
upcoming_shift_page = self.upcoming_shift_page
6969
upcoming_shift_page.get_page(self.live_server_url, upcoming_shift_page.view_shift_page + '65459')
70-
found = re.search('Not Found', self.driver.page_source)
70+
found = re.search('You don\'t have the necessary rights to access this page', self.driver.page_source)
7171
self.assertNotEqual(found, None)
7272

7373
def test_view_without_any_assigned_shift(self):

vms/shift/views.py

+12-29
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from django.views.generic import ListView
1919
from django.utils.decorators import method_decorator
2020
from django.core.urlresolvers import reverse_lazy
21-
21+
from volunteer.utils import vol_id_check
2222

2323
class AdministratorLoginRequiredMixin(object):
2424

@@ -573,6 +573,10 @@ def sign_up(request, shift_id, volunteer_id):
573573
class ViewHoursView(LoginRequiredMixin, FormView, TemplateView):
574574
template_name = 'shift/hours_list.html'
575575

576+
@method_decorator(vol_id_check)
577+
def dispatch(self, *args, **kwargs):
578+
return super(ViewHoursView, self).dispatch(*args, **kwargs)
579+
576580
def get_context_data(self, **kwargs):
577581
context = super(ViewHoursView, self).get_context_data(**kwargs)
578582
volunteer_id = self.kwargs['volunteer_id']
@@ -582,36 +586,15 @@ def get_context_data(self, **kwargs):
582586

583587

584588
@login_required
589+
@vol_id_check
585590
def view_volunteer_shifts(request, volunteer_id):
586-
user = request.user
587-
vol = None
588-
589-
try:
590-
vol = user.volunteer
591-
except ObjectDoesNotExist:
592-
pass
591+
shift_list = get_unlogged_shifts_by_volunteer_id(volunteer_id)
592+
return render(
593+
request,
594+
'shift/volunteer_shifts.html',
595+
{'shift_list': shift_list, 'volunteer_id': volunteer_id, }
596+
)
593597

594-
# check that a volunteer is logged in
595-
if vol:
596-
if volunteer_id:
597-
volunteer = get_volunteer_by_id(volunteer_id)
598-
if volunteer:
599-
user = request.user
600-
if int(user.volunteer.id) == int(volunteer_id):
601-
shift_list = get_unlogged_shifts_by_volunteer_id(volunteer_id)
602-
return render(
603-
request,
604-
'shift/volunteer_shifts.html',
605-
{'shift_list': shift_list, 'volunteer_id': volunteer_id, }
606-
)
607-
else:
608-
return HttpResponse(status=403)
609-
else:
610-
raise Http404
611-
else:
612-
raise Http404
613-
else:
614-
return HttpResponse(status=403)
615598

616599

617600
class VolunteerSearchView(AdministratorLoginRequiredMixin, FormView):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{% extends "vms/base.html" %}
2+
3+
{% load i18n %}
4+
5+
{% block content %}
6+
<div class="spacer"></div>
7+
8+
{% csrf_token %}
9+
<div class="panel panel-danger">
10+
<div class="panel-heading">
11+
<h3 class="panel-title">{% trans "No Access" %}</h3>
12+
</div>
13+
<div class="panel-body">
14+
<br>
15+
{% trans "You don't have the necessary rights to access this page." %}
16+
<br>
17+
<br>
18+
<input type="button" class="btn btn-default" value="{% blocktrans %}Return to Previous Page{% endblocktrans %}" onClick="javascript:history.go(-1);">
19+
</div>
20+
</div>
21+
22+
{% endblock %}

vms/volunteer/utils.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from functools import wraps
2+
from django.shortcuts import render
3+
from django.http import Http404
4+
from volunteer.services import get_volunteer_by_id
5+
6+
def vol_id_check(func):
7+
@wraps(func)
8+
def wrapped_view(request, volunteer_id):
9+
vol = getattr(request.user, 'volunteer', hasattr(request.user, 'administrator'))
10+
if not vol:
11+
return render(request, 'vms/no_volunteer_access.html', status=403)
12+
elif vol != True:
13+
volunteer = get_volunteer_by_id(volunteer_id)
14+
if not volunteer:
15+
return render(request, 'vms/no_volunteer_access.html', status=403)
16+
if not int(volunteer.id) == vol.id:
17+
return render(request, 'vms/no_volunteer_access.html', status=403)
18+
return func(request, volunteer_id=volunteer_id)
19+
return wrapped_view

vms/volunteer/views.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
from volunteer.validation import validate_file
2323
from django.views.generic import View
2424
from django.core.urlresolvers import reverse_lazy
25-
25+
from django.utils.decorators import method_decorator
26+
from volunteer.utils import vol_id_check
2627

2728
@login_required
2829
def download_resume(request, volunteer_id):
@@ -109,13 +110,15 @@ def form_valid(self, form):
109110
class ProfileView(LoginRequiredMixin, DetailView):
110111
template_name = 'volunteer/profile.html'
111112

113+
@method_decorator(vol_id_check)
114+
def dispatch(self, *args, **kwargs):
115+
return super(ProfileView, self).dispatch(*args, **kwargs)
116+
112117
def get_object(self, queryset=None):
113118
volunteer_id = self.kwargs['volunteer_id']
114119
obj = Volunteer.objects.get(id=self.kwargs['volunteer_id'])
115-
if obj:
116-
return obj
117-
else:
118-
return HttpResponse(status=403)
120+
return obj
121+
119122

120123
'''
121124
The view generate Report.
@@ -124,6 +127,10 @@ def get_object(self, queryset=None):
124127

125128
class GenerateReportView(LoginRequiredMixin, View):
126129

130+
@method_decorator(vol_id_check)
131+
def dispatch(self, *args, **kwargs):
132+
return super(GenerateReportView, self).dispatch(*args, **kwargs)
133+
127134
def get(self, request, *args, **kwargs):
128135
view = ShowFormView.as_view()
129136
return view(request, *args,**kwargs)

0 commit comments

Comments
 (0)