[ZPT] Has_role in ZPT (and if-then-else) ?

Tony McDonald tony.mcdonald@ncl.ac.uk
Sat, 20 Oct 2001 12:22:04 +0100


Hi all,
I was demoing the coolness and scalability of ZPT to a colleague the other
day and came across a problem I couldn't figure out (fortunately my
colleague trusts me on these things and knows there'll be a fix! :)

We were trying to change this code;
<dtml-if "AUTHENTICATED_USER.has_role('fmcc_man') or
AUTHENTICATED_USER.has_role('fmcc_admin') or
AUTHENTICATED_USER.has_role('fmcc') or AUTHENTICATED_USER.has_role('ltsn')
or AUTHENTICATED_USER.has_role('ltsn_man')">

<select> ... </select>
</dtml-if>

Into PageTemplate speak;

I initially tried;

<div tal:define="roles user/getRoles">
<span tal:content="roles">roles?</span>
<select tal:condition="python: roles in ['Manager', 'fmcc_admin', 'fmcc',
'fmcc_man']"
...
</select>
</span>
</div>

But that doesn't work as user/getRoles returns a tuple, eg ('Manager',
'fmcc_admin') and the condition doesn't trigger.

I then made a PythonScript that I thought might work;

## Script (Python) "check_role"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=therole
##title=
##
# Example code:

# Import a standard function, and get the HTML request and response objects.
from Products.PythonScripts.standard import html_quote
request = container.REQUEST
RESPONSE =  request.RESPONSE

if request.AUTHENTICATED_USER.has_role(therole):
    return 1
else:
    return 0

Now this *does* work if the AUTHENTICATED_USER has the role I'm looking for
(ie '1' returned). If AUTHENTICATED_USER does not have that role, this
PythonScript doesn't even return :(

Anyone know what's the perceived knowledge on using roles and ZPT?

My colleague also asked about the
<dtml-if >
...
<dtml-else>
...
</dtml-if>

construct too, and how this might be done in ZPT. Again, what's the best
practice on this as well?

TIA
Tone.
-- 
Dr Tony McDonald,  Assistant Director, FMCC, http://www.fmcc.org.uk/
The Medical School, Newcastle University Tel: +44 191 243 6140
A Zope list for UK HE/FE  http://www.fmcc.org.uk/mailman/listinfo/zope