verifiers.py 1.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
from django.core.exceptions import PermissionDenied


class Verifier(object):
    def __init__(self, **kwargs):
        for key, value in kwargs.items():
            setattr(self, key, value)

    def __call__(self, **kwargs):
        if not self.valid(**kwargs):
            raise PermissionDenied(self.message)


class GroupMembershipVerifier(Verifier):
    message = "You are not in the correct groups to log in to this site."

    def valid(self, groups, **kwargs):
        for g in groups:
19
            if g["id"] == self.group:
20 21 22 23 24
                return True

        return False


25 26 27 28 29 30 31
class AnyGroupMembershipVerifier(Verifier):
    message = "You are not in the correct groups to log in to this site."

    def valid(self, groups, **kwargs):
        return any([g["id"] in self.groups for g in groups])


32 33 34
class TESTMembershipVerifier(GroupMembershipVerifier):
    message = "You are not in TEST so you are not allowed to log in."
    group = 6
35 36 37 38 39 40 41 42


class TESTAlliesMembershipVerifier(AnyGroupMembershipVerifier):
    message = (
        "You are not in TEST or an ally of TEST so you are not allowed to "
        "log in."
    )
    groups = [6, 26]