View Full Version : Velocity & Acegi
todds
Oct 14th, 2004, 11:41 PM
Hi,
I'm interested in using Acegi for security in my system, however I'm not sure how it integrates with Velocity, since that's what I'm going to use rather than JSP. In the docs, this is quoted:
<authz:authorize ifAllGranted="ROLE_SUPERVISOR">
<td>
<A HREF="del.htm?id=<c:out value="${contact.id}"/>">Del</A>
</td>
</authz:authorize>
I'm wondering if there is anything similar for velocity?
Thanks.
Ben Alex
Oct 15th, 2004, 12:18 AM
You'll need to expose a Velocity helper that will access the ContextHolder (which is a ThreadLocal that holds the current Authentication).
2devnull
Nov 2nd, 2004, 06:25 PM
Any idea how this would be done. Any code example would help.
Ben Alex
Nov 3rd, 2004, 03:40 PM
You'd write a POJO and expose it via a VelocityView subclass that overrides exposeHelpers(Map, HttpServletRequest). The POJO will contain some code like this (NB: I haven't testing this):
public Authentication getAuthentication() {
if (ContextHolder.getContext() != null && ContextHolder.getContext() instanceof SecureContext) {
return ((SecureContext) ContextHolder.getContext()).getAuthentication();
}
return null;
}
public boolean isGranted(String role) {
Authentication auth = getAuthentication();
if (auth == null)
return false;
for (int i=0; i < auth.getAuthorities().length; i++) {
if (role.equals(auth.getAuthorities()[i].getAuthority ()))
return true;
}
return false;
}
I personally use FreeMarker, so I'm not 100% how to include the body of your macro in Velocity. In FreeMarker you'd do something like this:
<#macro granted authority>
<#if acegiSecurityHelper.isGranted(authority)>
<#nested>
</#if>
</#macro>
HTH
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.