server/src/metaeducation/middleware.py
author ymh <ymh.work@gmail.com>
Fri, 11 Mar 2016 00:03:15 +0100
changeset 20 af79d3dd81f7
parent 15 8004d8fc9b38
child 27 c3c6f2f85680
permissions -rw-r--r--
Added tag 00.00.02 for changeset dc3f8366729d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
     1
from django.shortcuts import redirect
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
     2
from django.conf import settings
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
     3
from urllib.parse import urlencode
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
     4
from re import compile
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
     5
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
     6
EXEMPT_URLS = [compile(settings.LOGIN_URL.lstrip('/'))]
15
8004d8fc9b38 Corrected settings to support deployment in a subdirectory + protected front_list and front_delete so only staff users can access it + added version display in front_list
durandn
parents: 4
diff changeset
     7
if hasattr(settings, 'OAUTH_EXEMPT_URLS'):
8004d8fc9b38 Corrected settings to support deployment in a subdirectory + protected front_list and front_delete so only staff users can access it + added version display in front_list
durandn
parents: 4
diff changeset
     8
    EXEMPT_URLS += [compile(expr) for expr in settings.OAUTH_EXEMPT_URLS]
1
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
     9
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    10
class MtdcLoginRequiredWithContextMiddleware:
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    11
    """
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    12
    Middleware intended to emulate login_required decorator so we can forward the context query arg
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    13
    """
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    14
    def process_request(self, request):
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    15
        if not request.user.is_authenticated():
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    16
            path = request.path_info.lstrip('/')
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    17
            if not any(m.match(path) for m in EXEMPT_URLS):
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    18
                if request.GET.get("context", ""):
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    19
                    context = request.GET["context"]
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    20
                    response = redirect(settings.LOGIN_URL)
15
8004d8fc9b38 Corrected settings to support deployment in a subdirectory + protected front_list and front_delete so only staff users can access it + added version display in front_list
durandn
parents: 4
diff changeset
    21
                    print(path)
8004d8fc9b38 Corrected settings to support deployment in a subdirectory + protected front_list and front_delete so only staff users can access it + added version display in front_list
durandn
parents: 4
diff changeset
    22
                    response["LOCATION"] += "?"+urlencode({"context": context, "next": settings.URL_SUBDIRECTORY+"/"+path})
1
5f50937893ac Commit work on metaeducation
durandn
parents:
diff changeset
    23
                    return response