mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-30 00:22:25 +00:00
Templatetag can_view_app
This commit is contained in:
parent
d7bc8fc816
commit
c9a9324d0f
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 import template
|
||||||
from django.template.base import Node, NodeList
|
from django.template.base import Node, NodeList
|
||||||
|
|
||||||
|
from re2o.utils import APP_VIEWING_RIGHT
|
||||||
|
|
||||||
import cotisations.models as cotisations
|
import cotisations.models as cotisations
|
||||||
import machines.models as machines
|
import machines.models as machines
|
||||||
import preferences.models as preferences
|
import preferences.models as preferences
|
||||||
|
@ -178,6 +180,23 @@ def get_callback(tag_name, obj):
|
||||||
return acl_fct(obj.can_view_all, False)
|
return acl_fct(obj.can_view_all, False)
|
||||||
if tag_name == 'cannot_view_all':
|
if tag_name == 'cannot_view_all':
|
||||||
return acl_fct(obj.can_view_all, True)
|
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(
|
raise template.TemplateSyntaxError(
|
||||||
"%r tag is not a valid can_xxx tag" % tag_name
|
"%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
|
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('can_create')
|
||||||
@register.tag('cannot_create')
|
@register.tag('cannot_create')
|
||||||
|
@ -277,6 +327,21 @@ def acl_instance_filter(parser, token):
|
||||||
return AclInstanceNode(tag_name, instance_name, oknodes, konodes, *args)
|
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):
|
class AclModelNode(Node):
|
||||||
"""A node for the compiled ACL block when acl is base on model"""
|
"""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 the tag library #}
|
||||||
{% load bootstrap3 %}
|
{% load bootstrap3 %}
|
||||||
|
{% load acl %}
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="fr">
|
<html lang="fr">
|
||||||
<head prefix="og: http://ogp.me/ns#">
|
<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">
|
<div class="collapse navbar-collapse" id="myNavbar">
|
||||||
<ul class="nav navbar-nav">
|
<ul class="nav navbar-nav">
|
||||||
<li><a href="{% url "users:mon-profil" %}">Mon profil</a></li>
|
<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>
|
<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>
|
<li><a href="{% url "machines:index" %}">Machines</a></li>
|
||||||
|
{% acl_end %}
|
||||||
|
{% can_view_app cotisations %}
|
||||||
<li><a href="{% url "cotisations:index" %}">Cotisations</a></li>
|
<li><a href="{% url "cotisations:index" %}">Cotisations</a></li>
|
||||||
|
{% acl_end %}
|
||||||
|
{% can_view_app topologie %}
|
||||||
<li><a href="{% url "topologie:index" %}">Topologie</a></li>
|
<li><a href="{% url "topologie:index" %}">Topologie</a></li>
|
||||||
|
{% acl_end %}
|
||||||
|
{% can_view_app logs %}
|
||||||
<li><a href="{% url "logs:index" %}">Statistiques</a></li>
|
<li><a href="{% url "logs:index" %}">Statistiques</a></li>
|
||||||
{% endif %}
|
{% acl_end %}
|
||||||
</ul>
|
</ul>
|
||||||
<div class="col-sm-3 col-md-3 navbar-right">
|
<div class="col-sm-3 col-md-3 navbar-right">
|
||||||
<form action="{% url "search:search"%}" class="navbar-form" role="search">
|
<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 %}
|
{% endif %}
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
{% if is_cableur %}
|
{% can_view_app preferences %}
|
||||||
<ul class="nav navbar-nav navbar-right">
|
<ul class="nav navbar-nav navbar-right">
|
||||||
<li>
|
<li>
|
||||||
<a href="{% url 'preferences:display-options' %}">
|
<a href="{% url 'preferences:display-options' %}">
|
||||||
|
@ -113,7 +122,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
{% endif %}
|
{% acl_end %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</nav>
|
</nav>
|
||||||
|
|
Loading…
Reference in a new issue