From 9373ba0350af3f7346deeb7e4599bcd76cf89076 Mon Sep 17 00:00:00 2001 From: Filipe Ximenes Date: Thu, 23 Mar 2017 10:03:57 -0300 Subject: [PATCH] makes is_authenticated checking compatible with Django 11 --- rolepermissions/decorators.py | 5 +++-- rolepermissions/utils.py | 12 +++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/rolepermissions/decorators.py b/rolepermissions/decorators.py index 998cc51..3780c18 100644 --- a/rolepermissions/decorators.py +++ b/rolepermissions/decorators.py @@ -7,6 +7,7 @@ from django.core.exceptions import PermissionDenied from rolepermissions.checkers import has_role, has_permission +from rolepermissions.utils import user_is_authenticated def has_role_decorator(role): @@ -14,7 +15,7 @@ def request_decorator(dispatch): @wraps(dispatch) def wrapper(request, *args, **kwargs): user = request.user - if user.is_authenticated(): + if user_is_authenticated(user): if has_role(user, role): return dispatch(request, *args, **kwargs) if hasattr(settings, 'ROLEPERMISSIONS_REDIRECT_TO_LOGIN'): @@ -29,7 +30,7 @@ def request_decorator(dispatch): @wraps(dispatch) def wrapper(request, *args, **kwargs): user = request.user - if user.is_authenticated(): + if user_is_authenticated(user): if has_permission(user, permission_name): return dispatch(request, *args, **kwargs) if hasattr(settings, 'ROLEPERMISSIONS_REDIRECT_TO_LOGIN'): diff --git a/rolepermissions/utils.py b/rolepermissions/utils.py index 61c7a52..dde3bd4 100644 --- a/rolepermissions/utils.py +++ b/rolepermissions/utils.py @@ -1,10 +1,20 @@ from __future__ import unicode_literals import re +import collections + + +def user_is_authenticated(user): + if isinstance(user.is_authenticated, collections.Callable): + authenticated = user.is_authenticated() + else: + authenticated = user.is_authenticated + + return authenticated def camelToSnake(s): - """ + """ https://gist.github.com/jaytaylor/3660565 Is it ironic that this function is written in camel case, yet it converts to snake case? hmm..