__init__.py 2.57 KB
Newer Older
1
2
3
4
5

from django.http import HttpResponseForbidden
from django.core.exceptions import PermissionDenied
from django.views.generic.base import View

6
7
from core.models import Group

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# TODO: see models.py's TODO!
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)
        obj = self.object
        user = self.request.user
        if obj is None:
            return res
        if user.is_superuser or user.groups.filter(name=obj.owner_group.name).exists():
            return res
        return HttpResponseForbidden("403, Forbidden")

class CanEditMixin(CanEditPropMixin):
    """
    This view makes exactly the same this as its direct parent, but checks the group on the edit_group field of the
    object
    """
    def dispatch(self, request, *arg, **kwargs):
        res = super(CanEditMixin, self).dispatch(request, *arg, **kwargs)
        if res.status_code != 403:
            return res
        obj = self.object
        user = self.request.user
        if obj is None:
            return res
        for g in obj.edit_group.all():
            if user.groups.filter(name=g.name).exists():
                return super(CanEditPropMixin, self).dispatch(request, *arg, **kwargs)
Skia's avatar
Skia committed
42
43
        if isinstance(obj, User) and obj == user:
                return super(CanEditPropMixin, self).dispatch(request, *arg, **kwargs)
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
        return HttpResponseForbidden("403, Forbidden")

class CanViewMixin(CanEditMixin):
    """
    This view still makes exactly the same this as its direct parent, but checks the group on the view_group field of
    the object
    """
    def dispatch(self, request, *arg, **kwargs):
        res = super(CanViewMixin, self).dispatch(request, *arg, **kwargs)
        if res.status_code != 403:
            return res
        obj = self.object
        user = self.request.user
        if obj is None:
            return res
        for g in obj.view_group.all():
            if user.groups.filter(name=g.name).exists():
                return super(CanEditPropMixin, self).dispatch(request, *arg, **kwargs)
        raise PermissionDenied
        return HttpResponseForbidden("403, Forbidden")

Skia's avatar
Skia committed
65
66
67
from .user import *
from .page import *
from .site import *
68
from .group import *
69