Skip to content
Snippets Groups Projects
Commit cf945719 authored by Carlos Vega's avatar Carlos Vega
Browse files

added support for class-based views

parent 7aa15f11
No related branches found
No related tags found
1 merge request!192Feature/add way to change password and PERMISSIONS
...@@ -8,6 +8,7 @@ from django.http import HttpResponseForbidden ...@@ -8,6 +8,7 @@ from django.http import HttpResponseForbidden
from django.contrib.auth.models import Permission from django.contrib.auth.models import Permission
import functools import functools
from collections import defaultdict from collections import defaultdict
from django.views.generic.base import ContextMixin
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -84,19 +85,24 @@ class PermissionDecorator: ...@@ -84,19 +85,24 @@ class PermissionDecorator:
''' '''
This method is also called when the function is decorated This method is also called when the function is decorated
''' '''
def func_wrapper(request, **kwargs): def func_wrapper(thing, *args, **kwargs):
''' '''
This method is called when the decorated function is called This method is called when the decorated function is called
''' '''
if isinstance(thing, ContextMixin):
request = thing.request
else:
request = thing
if request.user.is_superuser: if request.user.is_superuser:
return func(request, **kwargs) return func(thing, *args, **kwargs)
else: else:
worker = Worker.get_by_user(request.user) worker = Worker.get_by_user(request.user)
roles = WorkerStudyRole.objects.filter(worker=worker, study_id=GLOBAL_STUDY_ID) roles = WorkerStudyRole.objects.filter(worker=worker, study_id=GLOBAL_STUDY_ID)
if roles.count() > 0: if roles.count() > 0:
permissions = roles[0].permissions.filter(codename=self.perm_codename) permissions = roles[0].permissions.filter(codename=self.perm_codename)
if len(permissions) > 0: if len(permissions) > 0:
return func(request, **kwargs) return func(thing, *args, **kwargs)
messages.error(request, 'You are not authorized to view this page. Request permissions to the system administrator.') messages.error(request, 'You are not authorized to view this page. Request permissions to the system administrator.')
#avoid loops if the HTTP_REFERER header is set to the visited URL #avoid loops if the HTTP_REFERER header is set to the visited URL
http_referer = request.META.get('HTTP_REFERER', 'web.views.index') http_referer = request.META.get('HTTP_REFERER', 'web.views.index')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment