Skip to content

Commit d750aa8

Browse files
committed
Stop volunteers from accessing each other's urls.
Related to #326 also fix the test.
1 parent 3804832 commit d750aa8

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

2021
class AdministratorLoginRequiredMixin(object):
@@ -122,6 +123,7 @@ def get_queryset(self):
122123

123124

124125
@login_required
126+
@vol_id_check
125127
def list_sign_up(request, volunteer_id):
126128
if request.method == 'POST':
127129
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

@@ -568,6 +568,10 @@ def sign_up(request, shift_id, volunteer_id):
568568
class ViewHoursView(LoginRequiredMixin, FormView, TemplateView):
569569
template_name = 'shift/hours_list.html'
570570

571+
@method_decorator(vol_id_check)
572+
def dispatch(self, *args, **kwargs):
573+
return super(ViewHoursView, self).dispatch(*args, **kwargs)
574+
571575
def get_context_data(self, **kwargs):
572576
context = super(ViewHoursView, self).get_context_data(**kwargs)
573577
volunteer_id = self.kwargs['volunteer_id']
@@ -577,36 +581,15 @@ def get_context_data(self, **kwargs):
577581

578582

579583
@login_required
584+
@vol_id_check
580585
def view_volunteer_shifts(request, volunteer_id):
581-
user = request.user
582-
vol = None
583-
584-
try:
585-
vol = user.volunteer
586-
except ObjectDoesNotExist:
587-
pass
586+
shift_list = get_unlogged_shifts_by_volunteer_id(volunteer_id)
587+
return render(
588+
request,
589+
'shift/volunteer_shifts.html',
590+
{'shift_list': shift_list, 'volunteer_id': volunteer_id, }
591+
)
588592

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

611594

612595
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
@@ -21,7 +21,8 @@
2121
from volunteer.validation import validate_file
2222
from django.views.generic import View
2323
from django.core.urlresolvers import reverse_lazy
24-
24+
from django.utils.decorators import method_decorator
25+
from volunteer.utils import vol_id_check
2526

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

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

119122
'''
120123
The view generate Report.
@@ -123,6 +126,10 @@ def get_object(self, queryset=None):
123126

124127
class GenerateReportView(LoginRequiredMixin, View):
125128

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

0 commit comments

Comments
 (0)