This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
authentic2-auth-saml2/authentic2_auth_saml2/decorators.py

21 lines
671 B
Python

from django.conf import settings
from django.utils.http import urlencode
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.http import HttpResponseRedirect
from functools import wraps
def anonymous_only(func):
'''Logout before entering this view'''
@wraps(func)
def f(request, *args, **kwargs):
if request.user.is_authenticated():
current_url = request.build_absolute_uri()
query = urlencode({REDIRECT_FIELD_NAME: current_url})
logout_url = '{0}?{1}'.format(settings.LOGOUT_URL, query)
return HttpResponseRedirect(logout_url)
return func(request, *args, **kwargs)
return f