From e117f1c135a457385ff9dd7fc813afd539684c07 Mon Sep 17 00:00:00 2001 From: Marco Bonetti Date: Thu, 4 Jun 2015 11:44:42 +0200 Subject: [PATCH] Better handling for Custom User Models. Fixes Issue #131 --- rosetta/access.py | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/rosetta/access.py b/rosetta/access.py index b633b01..b138076 100644 --- a/rosetta/access.py +++ b/rosetta/access.py @@ -1,5 +1,6 @@ from django.conf import settings from rosetta.conf import settings as rosetta_settings +from django.core.exceptions import ImproperlyConfigured try: import importlib @@ -28,20 +29,31 @@ def get_access_control_function(): def is_superuser_staff_or_in_translators_group(user): if not getattr(settings, 'ROSETTA_REQUIRES_AUTH', True): return True - if not user.is_authenticated(): - return False - elif user.is_superuser and user.is_staff: - return True - else: - return user.groups.filter(name='translators').exists() + try: + if not user.is_authenticated(): + return False + elif user.is_superuser and user.is_staff: + return True + else: + return user.groups.filter(name='translators').exists() + except AttributeError: + if not hasattr(user, 'is_authenticated') or not hasattr(user, 'is_superuser') or not hasattr(user, 'groups'): + raise ImproperlyConfigured('If you are using custom User Models you must implement a custom authentication method for Rosetta. See ROSETTA_ACCESS_CONTROL_FUNCTION here: https://django-rosetta.readthedocs.org/en/latest/settings.html') + raise def can_translate_language(user, langid): - if not rosetta_settings.ROSETTA_LANGUAGE_GROUPS: - return can_translate(user) - elif not user.is_authenticated(): - return False - elif user.is_superuser and user.is_staff: - return True - else: - return user.groups.filter(name='translators-%s' % langid).exists() + try: + if not rosetta_settings.ROSETTA_LANGUAGE_GROUPS: + return can_translate(user) + elif not user.is_authenticated(): + return False + elif user.is_superuser and user.is_staff: + return True + else: + return user.groups.filter(name='translators-%s' % langid).exists() + + except AttributeError: + if not hasattr(user, 'is_authenticated') or not hasattr(user, 'is_superuser') or not hasattr(user, 'groups'): + raise ImproperlyConfigured('If you are using custom User Models you must implement a custom authentication method for Rosetta. See ROSETTA_ACCESS_CONTROL_FUNCTION here: https://django-rosetta.readthedocs.org/en/latest/settings.html') + raise