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):
Code:
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:
Code:
<#macro granted authority>
<#if acegiSecurityHelper.isGranted(authority)>
<#nested>
</#if>
</#macro>
HTH