Gitlab CSE Unil

permissions.py 5.3 KB
Newer Older
1
2
3
4
5
6
7
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# coding=utf-8
from __future__ import unicode_literals
from django.core.exceptions import PermissionDenied
from django.shortcuts import render
import json

SHARING_MODE_NONE = 0
SHARING_MODE_MANUAL = 1
SHARING_MODE_REGKEY = 4
SHARING_MODE_AAIRULES = 8
SHARING_MODE_MOODLELTI = 16


class PermissionClass(object):
    """
    Base class for Permission Classes.
    Cannot be used directly, has to be extended.
    Child classes need to implement ``check_registration``
    and ``check_revocation``methods
    """
    has_interactive_registration = False

    @classmethod
    def get_interactive_registration_response(cls, request, anobj):
        raise NotImplementedError()

    @classmethod
    def _register_user(cls, user, anobj):
        if not anobj.locked:
            anobj.members.add(user)

    @classmethod
    def _revoke_user(cls, user, anobj):
        if not anobj.locked:
            anobj.members.remove(user)

    @classmethod
    def check_registration(cls, request, anobj):
        raise NotImplementedError()

    @classmethod
    def check_revocation(cls, request, anobj):
        raise NotImplementedError()

    @classmethod
    def check_permission(cls, request, anobj):
        user = request.user
        # Anonymous users have no permissions
        if user.is_anonymous():
            raise PermissionDenied()

        # Owner can always access
        if anobj.owner == user:
            return True

        members = anobj.members.all()
        if user in members:
            if not anobj.locked:
                # Check revocation only if anobj is not locked
                cls.check_revocation(request, anobj)
        else:
            if anobj.locked:
                # If anobj is locked never try to register
                raise PermissionDenied()
            cls.check_registration(request, anobj)

        return True

    @classmethod
    def has_permission(cls, request, anobj):
        try:
            return cls.check_permission(request, anobj)
        except PermissionDenied:
            return False


class IsMember(PermissionClass):
    """
    The simplest permission mode:
        (#no auto registration -> check_registration always fails#)

        no auto revocation -> check_revocation always pass
    """

    @classmethod
    def check_registration(cls, request, anobj):
        """
        Check if the username is listed in the sharing options.
        This may occur if the username has been added as member but the
        corresponding user object was not yet created.
        """
        # obviously, we need some sharing options to perform the check
        if not anobj.sharing_opts:
            raise PermissionDenied()

        opts_members = anobj.sharing_opts.get('members', [])
        if request.user.username in opts_members:
            cls._register_user(request.user, anobj)
            # The user is now in the anobj.members list, so we have to remove it
            #   from the sharing_opts.members list
            opts_members.remove(request.user.username)
            anobj.sharing_opts['members'] = opts_members
            anobj.save()
        else:
            raise PermissionDenied()

    @classmethod
    def check_revocation(cls, request, anobj):
        return anobj


class RegistrationKey(PermissionClass):
    """
    Register user if she knows the key defined in
    sharing options of the anobj
    """
    @classmethod
    def check_registration(cls, request, anobj):
        reg_key = request.POST.get('k')
        # We need at least a key and some sharing options
        if reg_key is None or not anobj.sharing_opts:
            raise PermissionDenied()

        if anobj.sharing_opts.get('key') == reg_key:
            cls._register_user(request.user, anobj)
        else:
            raise PermissionDenied()

    @classmethod
    def check_revocation(cls, request, anobj):
        """
        noop, there is no revocation possible here
        """
        return anobj

    has_interactive_registration = True

    @classmethod
    def get_interactive_registration_response(cls, request, anobj):
        errors = []
        if request.POST.get('k') is not None:
            errors.append({'code': "invalid_key"})
        if anobj.locked:
            errors.append({'code': "locked_anobj"})

        context = {
            'anobj': anobj,
            'errors': errors
        }
        return render(request, "registration/adim/registration_key.html", context)


class AAIRules(PermissionClass):
    pass


class MoodleLTI(PermissionClass):
    pass


PERMISSION_CLASSES = {
    SHARING_MODE_NONE: None,
    SHARING_MODE_MANUAL: IsMember,
    SHARING_MODE_REGKEY: RegistrationKey,
    SHARING_MODE_AAIRULES: AAIRules,
    SHARING_MODE_MOODLELTI: MoodleLTI
}


def get_permission_class(sharing_mode):
    return PERMISSION_CLASSES.get(sharing_mode)


def check_anobj_permission(request, anobj):
    p = get_permission_class(anobj.sharing_mode)
    if p is not None:
        p.check_permission(request, anobj)
    elif request.user != anobj.owner:
        raise PermissionDenied()


def has_anobj_access(request, anobj):
    """
    Verify that request.user has access to the anobj.
    Doesn't apply registration nor revocation,
    no need to know what kind of sharing_mode we have.

    :param request:
    :param anobj:
    :return: boolean
    """
    return request.user == anobj.owner or request.user in anobj.members.all()