diff --git a/preferences/views.py b/preferences/views.py index 471207e0..90afa2e8 100644 --- a/preferences/views.py +++ b/preferences/views.py @@ -43,7 +43,7 @@ from reversion import revisions as reversion from importlib import import_module from re2o.settings_local import OPTIONNAL_APPS_RE2O from re2o.views import form -from re2o.acl import can_create, can_edit, can_delete_set, can_view_all, can_delete +from re2o.acl import can_create, can_edit, can_delete_set, can_view_all, can_delete, acl_error_message from .forms import MailContactForm, DelMailContactForm from .forms import ( @@ -130,10 +130,9 @@ def edit_options(request, section): return redirect(reverse('preferences:display-options')) options_instance, _created = model.objects.get_or_create() - can, msg = options_instance.can_edit(request.user) + can, msg, permissions = options_instance.can_edit(request.user) if not can: - messages.error(request, msg or _("You don't have the right to edit" - " this option.")) + messages.error(request, acl_error_message(msg, permissions)) return redirect(reverse('index')) options = form_instance( request.POST or None, diff --git a/re2o/acl.py b/re2o/acl.py index 23f51210..e445a28c 100644 --- a/re2o/acl.py +++ b/re2o/acl.py @@ -39,12 +39,24 @@ from django.utils.translation import ugettext as _ from re2o.utils import get_group_having_permission -def group_list(permissions): - """Create a string listing every groups having one of the given - `permissions`.""" - return ", ".join([ +def acl_error_message(msg, permissions): + """Create an error message for msg and permissions.""" + groups = ", ".join([ g.name for g in get_group_having_permission(*permissions) ]) + message = msg or _("You don't have the right to edit" + " this option.") + if groups: + return message + _( + " You need to be a member of one of those" + " groups : %s" + ) % groups + else: + return message + " No group have the %s permission(s) !" % " or ".join([ + ",".join(permissions[:-1]), + permissions[-1]] + if len(permissions) > 2 else permissions + ) def acl_base_decorator(method_name, *targets, on_instance=True): @@ -163,23 +175,8 @@ ModelC) for target, fields in group_targets(): for can, msg, permissions in process_target(target, fields): if not can: - groups = group_list(permissions) - if groups: - error_messages.append( - msg + _( - " You need to be a member of one of those" - " groups : %s" - ) % groups - ) - else: - error_messages.append( - msg + " No group have the %s permission(s) !" \ - % " or ".join([ - ",".join(permissions[:-1]), - permissions[-1]] - if len(permissions) > 2 else permissions - ) - ) + error_messages.append(acl_error_message(msg, permissions)) + if error_messages: for msg in error_messages: messages.error( diff --git a/re2o/field_permissions.py b/re2o/field_permissions.py index 5febeb63..4e3be7f3 100644 --- a/re2o/field_permissions.py +++ b/re2o/field_permissions.py @@ -69,7 +69,7 @@ class FieldPermissionModelMixin: # Try to find a user setting that qualifies them for permission. for perm in checks: if callable(perm): - result, _reason = perm(user_request=user) + result, _reason, _permissions = perm(user_request=user) if result is not None: return result else: