|
1
|
|
|
# vim: set fileencoding=utf-8 : |
|
2
|
|
|
""" |
|
3
|
|
|
permissionif templatetag |
|
4
|
|
|
""" |
|
5
|
|
|
from django import template |
|
6
|
|
|
from django.template import TemplateSyntaxError |
|
7
|
|
|
from django.template.smartif import infix |
|
8
|
|
|
from django.template.smartif import IfParser |
|
9
|
|
|
from django.template.smartif import OPERATORS |
|
10
|
|
|
from django.template.defaulttags import IfNode |
|
11
|
|
|
from django.template.defaulttags import TemplateLiteral |
|
12
|
|
|
from permission.conf import settings |
|
13
|
|
|
|
|
14
|
|
|
register = template.Library() |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
def of_operator(context, x, y): |
|
18
|
|
|
""" |
|
19
|
|
|
'of' operator of permission if |
|
20
|
|
|
|
|
21
|
|
|
This operator is used to specify the target object of permission |
|
22
|
|
|
""" |
|
23
|
|
|
return x.eval(context), y.eval(context) |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
def has_operator(context, x, y): |
|
27
|
|
|
""" |
|
28
|
|
|
'has' operator of permission if |
|
29
|
|
|
|
|
30
|
|
|
This operator is used to specify the user object of permission |
|
31
|
|
|
""" |
|
32
|
|
|
user = x.eval(context) |
|
33
|
|
|
perm = y.eval(context) |
|
34
|
|
|
if isinstance(perm, (list, tuple)): |
|
35
|
|
|
perm, obj = perm |
|
36
|
|
|
else: |
|
37
|
|
|
obj = None |
|
38
|
|
|
return user.has_perm(perm, obj) |
|
39
|
|
|
|
|
40
|
|
|
# Add 'of' and 'has' operator to existing operators |
|
41
|
|
|
EXTRA_OPERATORS = { |
|
42
|
|
|
'of': infix(20, of_operator), |
|
43
|
|
|
'has': infix(10, has_operator), |
|
44
|
|
|
} |
|
45
|
|
|
EXTRA_OPERATORS.update(OPERATORS) |
|
46
|
|
|
for key, op in EXTRA_OPERATORS.items(): |
|
47
|
|
|
op.id = key |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
class PermissionIfParser(IfParser): |
|
51
|
|
|
"""Permission if parser""" |
|
52
|
|
|
OPERATORS = EXTRA_OPERATORS |
|
53
|
|
|
"""use extra operator""" |
|
54
|
|
|
|
|
55
|
|
|
def translate_token(self, token): |
|
56
|
|
|
try: |
|
57
|
|
|
# use own operators instead of builtin operators |
|
58
|
|
|
op = self.OPERATORS[token] |
|
59
|
|
|
except (KeyError, TypeError): |
|
60
|
|
|
return self.create_var(token) |
|
61
|
|
|
else: |
|
62
|
|
|
return op() |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
class TemplatePermissionIfParser(PermissionIfParser): |
|
66
|
|
|
error_class = TemplateSyntaxError |
|
67
|
|
|
|
|
68
|
|
|
def __init__(self, parser, *args, **kwargs): |
|
69
|
|
|
self.template_parser = parser |
|
70
|
|
|
super(TemplatePermissionIfParser, self).__init__(*args, **kwargs) |
|
71
|
|
|
|
|
72
|
|
|
def create_var(self, value): |
|
73
|
|
|
return TemplateLiteral(self.template_parser.compile_filter(value), value) |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
@register.tag('permission') |
|
77
|
|
|
def do_permissionif(parser, token): |
|
78
|
|
|
""" |
|
79
|
|
|
Permission if templatetag |
|
80
|
|
|
|
|
81
|
|
|
Examples |
|
82
|
|
|
-------- |
|
83
|
|
|
:: |
|
84
|
|
|
|
|
85
|
|
|
{% if user has 'blogs.add_article' %} |
|
86
|
|
|
<p>This user have 'blogs.add_article' permission</p> |
|
87
|
|
|
{% elif user has 'blog.change_article' of object %} |
|
88
|
|
|
<p>This user have 'blogs.change_article' permission of {{object}}</p> |
|
89
|
|
|
{% endif %} |
|
90
|
|
|
|
|
91
|
|
|
{# If you set 'PERMISSION_REPLACE_BUILTIN_IF = False' in settings #} |
|
92
|
|
|
{% permission user has 'blogs.add_article' %} |
|
93
|
|
|
<p>This user have 'blogs.add_article' permission</p> |
|
94
|
|
|
{% elpermission user has 'blog.change_article' of object %} |
|
95
|
|
|
<p>This user have 'blogs.change_article' permission of {{object}}</p> |
|
96
|
|
|
{% endpermission %} |
|
97
|
|
|
|
|
98
|
|
|
""" |
|
99
|
|
|
bits = token.split_contents() |
|
100
|
|
|
ELIF = "el%s" % bits[0] |
|
101
|
|
|
ELSE = "else" |
|
102
|
|
|
ENDIF = "end%s" % bits[0] |
|
103
|
|
|
|
|
104
|
|
|
# {% if ... %} |
|
105
|
|
|
bits = bits[1:] |
|
106
|
|
|
condition = do_permissionif.Parser(parser, bits).parse() |
|
107
|
|
|
nodelist = parser.parse((ELIF, ELSE, ENDIF)) |
|
108
|
|
|
conditions_nodelists = [(condition, nodelist)] |
|
109
|
|
|
token = parser.next_token() |
|
110
|
|
|
|
|
111
|
|
|
# {% elif ... %} (repeatable) |
|
112
|
|
|
while token.contents.startswith(ELIF): |
|
113
|
|
|
bits = token.split_contents()[1:] |
|
114
|
|
|
condition = do_permissionif.Parser(parser, bits).parse() |
|
115
|
|
|
nodelist = parser.parse((ELIF, ELSE, ENDIF)) |
|
116
|
|
|
conditions_nodelists.append((condition, nodelist)) |
|
117
|
|
|
token = parser.next_token() |
|
118
|
|
|
|
|
119
|
|
|
# {% else %} (optional) |
|
120
|
|
|
if token.contents == ELSE: |
|
121
|
|
|
nodelist = parser.parse((ENDIF,)) |
|
122
|
|
|
conditions_nodelists.append((None, nodelist)) |
|
123
|
|
|
token = parser.next_token() |
|
124
|
|
|
|
|
125
|
|
|
# {% endif %} |
|
126
|
|
|
assert token.contents == ENDIF |
|
127
|
|
|
|
|
128
|
|
|
return IfNode(conditions_nodelists) |
|
129
|
|
|
do_permissionif.Parser = TemplatePermissionIfParser |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
# To replace builtin if |
|
133
|
|
|
def replace_builtin_if(replace=False): |
|
134
|
|
|
if replace: |
|
135
|
|
|
OPERATORS.update({ |
|
136
|
|
|
'has': EXTRA_OPERATORS['has'], |
|
137
|
|
|
'of': EXTRA_OPERATORS['of'], |
|
138
|
|
|
}) |
|
139
|
|
|
else: |
|
140
|
|
|
# remove of, has from OPERATORS |
|
141
|
|
|
OPERATORS.pop('of', None) |
|
142
|
|
|
OPERATORS.pop('has', None) |
|
143
|
|
|
|
|
144
|
|
|
replace_builtin_if(settings.PERMISSION_REPLACE_BUILTIN_IF) |
|
145
|
|
|
|