2015-11-27 14:39:42 +00:00
|
|
|
|
2015-12-07 16:23:52 +00:00
|
|
|
from django.shortcuts import render
|
2016-05-02 09:33:38 +00:00
|
|
|
from django.http import HttpResponseForbidden, HttpResponseNotFound
|
2015-12-08 08:46:48 +00:00
|
|
|
from django.core.exceptions import PermissionDenied, ObjectDoesNotExist
|
2015-11-27 14:39:42 +00:00
|
|
|
from django.views.generic.base import View
|
|
|
|
|
2015-12-07 15:08:24 +00:00
|
|
|
from core.models import Group
|
|
|
|
|
2015-12-07 16:23:52 +00:00
|
|
|
def forbidden(request):
|
2016-05-02 09:33:38 +00:00
|
|
|
return HttpResponseForbidden(render(request, "core/403.jinja"))
|
2015-12-07 16:23:52 +00:00
|
|
|
|
|
|
|
def not_found(request):
|
2016-05-02 09:33:38 +00:00
|
|
|
return HttpResponseNotFound(render(request, "core/404.jinja"))
|
2015-12-07 16:23:52 +00:00
|
|
|
|
2016-02-05 15:59:42 +00:00
|
|
|
def can_edit_prop(obj, user):
|
|
|
|
if obj is None or user.is_owner(obj):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def can_edit(obj, user):
|
|
|
|
if obj is None or user.can_edit(obj):
|
|
|
|
return True
|
|
|
|
return can_edit_prop(obj, user)
|
|
|
|
|
|
|
|
def can_view(obj, user):
|
|
|
|
if obj is None or user.can_view(obj):
|
|
|
|
return True
|
|
|
|
return can_edit(obj, user)
|
2015-12-07 16:23:52 +00:00
|
|
|
|
2015-11-27 14:39:42 +00:00
|
|
|
class CanEditPropMixin(View):
|
|
|
|
"""
|
|
|
|
This view is made to protect any child view that would be showing some properties of an object that are restricted
|
|
|
|
to only the owner group of the given object.
|
|
|
|
In other word, you can make a view with this view as parent, and it would be retricted to the users that are in the
|
|
|
|
object's owner_group
|
|
|
|
"""
|
|
|
|
def dispatch(self, request, *arg, **kwargs):
|
|
|
|
res = super(CanEditPropMixin, self).dispatch(request, *arg, **kwargs)
|
2016-02-05 15:59:42 +00:00
|
|
|
if hasattr(self, 'object'):
|
|
|
|
obj = self.object
|
|
|
|
elif hasattr(self, 'object_list'):
|
|
|
|
obj = self.object_list[0] if self.object_list else None
|
|
|
|
if can_edit_prop(obj, self.request.user):
|
2015-11-27 14:39:42 +00:00
|
|
|
return res
|
2015-12-08 10:10:29 +00:00
|
|
|
try: # Always unlock when 403
|
|
|
|
self.object.unset_lock()
|
|
|
|
except: pass
|
2015-12-07 16:23:52 +00:00
|
|
|
raise PermissionDenied
|
2015-11-27 14:39:42 +00:00
|
|
|
|
2015-12-08 10:10:29 +00:00
|
|
|
class CanEditMixin(View):
|
2015-11-27 14:39:42 +00:00
|
|
|
"""
|
2016-02-05 15:59:42 +00:00
|
|
|
This view makes exactly the same this as its direct parent, but checks the group on the edit_groups field of the
|
2015-11-27 14:39:42 +00:00
|
|
|
object
|
|
|
|
"""
|
|
|
|
def dispatch(self, request, *arg, **kwargs):
|
2015-12-08 10:10:29 +00:00
|
|
|
res = super(CanEditMixin, self).dispatch(request, *arg, **kwargs)
|
2016-02-05 15:59:42 +00:00
|
|
|
if hasattr(self, 'object'):
|
|
|
|
obj = self.object
|
|
|
|
elif hasattr(self, 'object_list'):
|
|
|
|
obj = self.object_list[0] if self.object_list else None
|
|
|
|
if can_edit(obj, self.request.user):
|
2015-12-08 08:46:48 +00:00
|
|
|
return res
|
2015-12-08 10:10:29 +00:00
|
|
|
try: # Always unlock when 403
|
|
|
|
self.object.unset_lock()
|
|
|
|
except: pass
|
2015-12-07 16:23:52 +00:00
|
|
|
raise PermissionDenied
|
2015-11-27 14:39:42 +00:00
|
|
|
|
2015-12-08 10:10:29 +00:00
|
|
|
class CanViewMixin(View):
|
2015-11-27 14:39:42 +00:00
|
|
|
"""
|
2016-02-05 15:59:42 +00:00
|
|
|
This view still makes exactly the same this as its direct parent, but checks the group on the view_groups field of
|
2015-11-27 14:39:42 +00:00
|
|
|
the object
|
|
|
|
"""
|
|
|
|
def dispatch(self, request, *arg, **kwargs):
|
2015-12-08 10:10:29 +00:00
|
|
|
res = super(CanViewMixin, self).dispatch(request, *arg, **kwargs)
|
2016-02-05 15:59:42 +00:00
|
|
|
if hasattr(self, 'object'):
|
|
|
|
obj = self.object
|
|
|
|
elif hasattr(self, 'object_list'):
|
|
|
|
obj = self.object_list[0] if self.object_list else None
|
|
|
|
if can_view(obj, self.request.user):
|
2015-11-27 14:39:42 +00:00
|
|
|
return res
|
2015-12-08 10:10:29 +00:00
|
|
|
try: # Always unlock when 403
|
|
|
|
self.object.unset_lock()
|
|
|
|
except: pass
|
2015-11-27 14:39:42 +00:00
|
|
|
raise PermissionDenied
|
|
|
|
|
2015-11-24 15:09:46 +00:00
|
|
|
from .user import *
|
|
|
|
from .page import *
|
|
|
|
from .site import *
|
2015-11-26 16:40:31 +00:00
|
|
|
from .group import *
|
2016-01-08 16:11:38 +00:00
|
|
|
from .api import *
|
2015-11-27 14:39:42 +00:00
|
|
|
|