mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-05 01:16:27 +00:00
Templatetag can_view_app
This commit is contained in:
parent
a1df6136cb
commit
63948821d3
2 changed files with 81 additions and 7 deletions
|
@ -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"""
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
|
||||
{# Load the tag library #}
|
||||
{% load bootstrap3 %}
|
||||
|
||||
{% load acl %}
|
||||
<!DOCTYPE html>
|
||||
<html lang="fr">
|
||||
<head prefix="og: http://ogp.me/ns#">
|
||||
|
@ -73,13 +73,22 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
<div class="collapse navbar-collapse" id="myNavbar">
|
||||
<ul class="nav navbar-nav">
|
||||
<li><a href="{% url "users:mon-profil" %}">Mon profil</a></li>
|
||||
{% if is_cableur %}
|
||||
{% can_view_app users %}
|
||||
|
||||
<li><a href="{% url "users:index" %}">Adhérents</a></li>
|
||||
{% acl_end %}
|
||||
{% can_view_app machines %}
|
||||
<li><a href="{% url "machines:index" %}">Machines</a></li>
|
||||
{% acl_end %}
|
||||
{% can_view_app cotisations %}
|
||||
<li><a href="{% url "cotisations:index" %}">Cotisations</a></li>
|
||||
{% acl_end %}
|
||||
{% can_view_app topologie %}
|
||||
<li><a href="{% url "topologie:index" %}">Topologie</a></li>
|
||||
{% acl_end %}
|
||||
{% can_view_app logs %}
|
||||
<li><a href="{% url "logs:index" %}">Statistiques</a></li>
|
||||
{% endif %}
|
||||
{% acl_end %}
|
||||
</ul>
|
||||
<div class="col-sm-3 col-md-3 navbar-right">
|
||||
<form action="{% url "search:search"%}" class="navbar-form" role="search">
|
||||
|
@ -105,7 +114,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
{% endif %}
|
||||
</li>
|
||||
</ul>
|
||||
{% if is_cableur %}
|
||||
{% can_view_app preferences %}
|
||||
<ul class="nav navbar-nav navbar-right">
|
||||
<li>
|
||||
<a href="{% url 'preferences:display-options' %}">
|
||||
|
@ -113,7 +122,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
{% endif %}
|
||||
{% acl_end %}
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
|
Loading…
Reference in a new issue