Project

General

Profile

0005-decorators-add-check_auth_level-for-automatic-increa.patch

Valentin Deniaud, 04 April 2019 05:07 PM

Download (1.6 KB)

View differences:

Subject: [PATCH 05/13] decorators: add check_auth_level for automatic increase

It should be used the same way as login_required, eventually at the same
time.
 src/authentic2/decorators.py | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
src/authentic2/decorators.py
from django.core.exceptions import ValidationError
from django.utils import six
from django_rbac.exceptions import InsufficientAuthLevel
from . import utils, app_settings, middleware
from .utils import to_list, to_iter
......
response.write(json_str)
return response
return f
def check_auth_level(func):
from django.contrib.auth.views import redirect_to_login
@wraps(func)
def wrapped(request, *args, **kwargs):
try:
request.user.auth_level = request.session.get('auth_level', 1)
return func(request, *args, **kwargs)
except InsufficientAuthLevel as e:
required_auth_level = e.required_level
next_field_value = request.get_full_path()
login_url = utils.get_manager_login_url() + '?auth_level=%s' % required_auth_level
return redirect_to_login(next_field_value, login_url)
return wrapped