8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-22 19:33:11 +00:00

Reusable acl error messages.

This commit is contained in:
Hugo Levy-Falk 2019-09-06 16:05:45 +02:00
parent 04abc64cc9
commit 9161e0f7e3
3 changed files with 22 additions and 26 deletions

View file

@ -43,7 +43,7 @@ from reversion import revisions as reversion
from importlib import import_module from importlib import import_module
from re2o.settings_local import OPTIONNAL_APPS_RE2O from re2o.settings_local import OPTIONNAL_APPS_RE2O
from re2o.views import form 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 MailContactForm, DelMailContactForm
from .forms import ( from .forms import (
@ -130,10 +130,9 @@ def edit_options(request, section):
return redirect(reverse('preferences:display-options')) return redirect(reverse('preferences:display-options'))
options_instance, _created = model.objects.get_or_create() 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: if not can:
messages.error(request, msg or _("You don't have the right to edit" messages.error(request, acl_error_message(msg, permissions))
" this option."))
return redirect(reverse('index')) return redirect(reverse('index'))
options = form_instance( options = form_instance(
request.POST or None, request.POST or None,

View file

@ -39,12 +39,24 @@ from django.utils.translation import ugettext as _
from re2o.utils import get_group_having_permission from re2o.utils import get_group_having_permission
def group_list(permissions): def acl_error_message(msg, permissions):
"""Create a string listing every groups having one of the given """Create an error message for msg and permissions."""
`permissions`.""" groups = ", ".join([
return ", ".join([
g.name for g in get_group_having_permission(*permissions) 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): def acl_base_decorator(method_name, *targets, on_instance=True):
@ -163,23 +175,8 @@ ModelC)
for target, fields in group_targets(): for target, fields in group_targets():
for can, msg, permissions in process_target(target, fields): for can, msg, permissions in process_target(target, fields):
if not can: if not can:
groups = group_list(permissions) error_messages.append(acl_error_message(msg, 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
)
)
if error_messages: if error_messages:
for msg in error_messages: for msg in error_messages:
messages.error( messages.error(

View file

@ -69,7 +69,7 @@ class FieldPermissionModelMixin:
# Try to find a user setting that qualifies them for permission. # Try to find a user setting that qualifies them for permission.
for perm in checks: for perm in checks:
if callable(perm): if callable(perm):
result, _reason = perm(user_request=user) result, _reason, _permissions = perm(user_request=user)
if result is not None: if result is not None:
return result return result
else: else: