From 63948821d34840e78f73e0b59e5d0f081a7e3839 Mon Sep 17 00:00:00 2001 From: LEVY-FALK Hugo Date: Thu, 28 Dec 2017 16:00:37 +0100 Subject: [PATCH] Templatetag can_view_app --- re2o/templatetags/acl.py | 65 ++++++++++++++++++++++++++++++++++++++++ templates/base.html | 23 +++++++++----- 2 files changed, 81 insertions(+), 7 deletions(-) diff --git a/re2o/templatetags/acl.py b/re2o/templatetags/acl.py index 2db6d682..0363475c 100644 --- a/re2o/templatetags/acl.py +++ b/re2o/templatetags/acl.py @@ -74,6 +74,8 @@ an instance of a model (either Model.can_xxx or instance.can_xxx) from django import template from django.template.base import Node, NodeList +from re2o.utils import APP_VIEWING_RIGHT + import cotisations.models as cotisations import machines.models as machines import preferences.models as preferences @@ -178,6 +180,23 @@ def get_callback(tag_name, obj): return acl_fct(obj.can_view_all, False) if tag_name == 'cannot_view_all': return acl_fct(obj.can_view_all, True) + if tag_name == 'can_view_app': + return acl_fct( + lambda user:( + user.has_perms((APP_VIEWING_RIGHT[obj],)), + "Vous ne pouvez pas voir cette application." + ), + False + ) + if tag_name == 'cannot_view_app': + return acl_fct( + lambda user:( + user.has_perms((APP_VIEWING_RIGHT[obj],)), + "Vous ne pouvez pas voir cette application." + ), + True + ) + raise template.TemplateSyntaxError( "%r tag is not a valid can_xxx tag" % tag_name ) @@ -197,6 +216,37 @@ def acl_fct(callback, reverse): return acl_fct_reverse if reverse else acl_fct_normal +@register.tag('can_view_app') +@register.tag('cannot_view_app') +def acl_app_filter(parser, token): + """Templatetag for acl checking on applications.""" + try: + tag_name, app_name = token.split_contents() + except ValueError: + raise template.TemplateSyntaxError( + "%r tag require 1 argument : the application" + % token.contents.split()[0] + ) + if not app_name in APP_VIEWING_RIGHT.keys(): + raise template.TemplateSyntaxError( + "%r is not a registered application for acl." + % app_name + ) + + callback = get_callback(tag_name, app_name) + + oknodes = parser.parse(('acl_else', 'acl_end')) + token = parser.next_token() + if token.contents == 'acl_else': + konodes = parser.parse(('acl_end')) + token = parser.next_token() + else: + konodes = NodeList() + + assert token.contents == 'acl_end' + + return AclAppNode(callback, oknodes, konodes) + @register.tag('can_create') @register.tag('cannot_create') @@ -277,6 +327,21 @@ def acl_instance_filter(parser, token): return AclInstanceNode(tag_name, instance_name, oknodes, konodes, *args) +class AclAppNode(Node): + """A node for compiled ACL block when ACL is based on application.""" + + def __init__(self, callback, oknodes, konodes): + self.callback = callback + self.oknodes = oknodes + self.konodes = konodes + + def render(self, context): + can, _ = self.callback(context['user']) + if can: + return self.oknodes.render(context) + return self.konodes.render(context) + + class AclModelNode(Node): """A node for the compiled ACL block when acl is base on model""" diff --git a/templates/base.html b/templates/base.html index 01f1ac4e..818d04e3 100644 --- a/templates/base.html +++ b/templates/base.html @@ -25,7 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc., {# Load the tag library #} {% load bootstrap3 %} - +{% load acl %} @@ -73,13 +73,22 @@ with this program; if not, write to the Free Software Foundation, Inc.,