Projet

Général

Profil

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

Valentin Deniaud, 04 avril 2019 17:07

Télécharger (1,6 ko)

Voir les différences:

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
13 13
from django.core.exceptions import ValidationError
14 14
from django.utils import six
15 15

  
16
from django_rbac.exceptions import InsufficientAuthLevel
16 17
from . import utils, app_settings, middleware
17 18
from .utils import to_list, to_iter
18 19

  
......
313 314
        response.write(json_str)
314 315
        return response
315 316
    return f
317

  
318

  
319
def check_auth_level(func):
320
    from django.contrib.auth.views import redirect_to_login
321
    @wraps(func)
322
    def wrapped(request, *args, **kwargs):
323
        try:
324
            request.user.auth_level = request.session.get('auth_level', 1)
325
            return func(request, *args, **kwargs)
326
        except InsufficientAuthLevel as e:
327
            required_auth_level = e.required_level
328
            next_field_value = request.get_full_path()
329
            login_url = utils.get_manager_login_url() + '?auth_level=%s' % required_auth_level
330
            return redirect_to_login(next_field_value, login_url)
331
    return wrapped
316
-