summaryrefslogtreecommitdiffstats
path: root/users/decorators.py
blob: 77d633a94ea45caff7ddf281c5a27bca0aced276 (plain)
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
from functools import wraps

from django.contrib.auth.views import redirect_to_login
from django.http import HttpResponseRedirect

from users.models import Identity


def identity_required(function):
    """
    Decorator for views that ensures an active identity is selected.
    """

    @wraps(function)
    def inner(request, *args, **kwargs):
        # They do have to be logged in
        if not request.user.is_authenticated:
            return redirect_to_login(next=request.get_full_path())
        # Try to retrieve their active identity
        identity_id = request.session.get("identity_id")
        if not identity_id:
            identity = None
        else:
            try:
                identity = Identity.objects.get(id=identity_id)
            except Identity.DoesNotExist:
                identity = None
        # If there's no active one, try to auto-select one
        if identity is None:
            possible_identities = list(request.user.identities.all())
            if len(possible_identities) != 1:
                # OK, send them to the identity selection page to select/create one
                return HttpResponseRedirect("/identity/select/")
            identity = possible_identities[0]
        request.identity = identity
        request.session["identity_id"] = identity.pk
        return function(request, *args, **kwargs)

    return inner