Announcement Announcement Module
Collapse
No announcement yet.
Velocity & Acegi Page Title Module
Move Remove Collapse
This topic is closed
X
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Velocity & Acegi

    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:
    Code:
    <authz&#58;authorize ifAllGranted="ROLE_SUPERVISOR">
      <td>
        <A HREF="del.htm?id=<c&#58;out value="$&#123;contact.id&#125;"/>">Del</A>
      </td>
    </authz&#58;authorize>
    I'm wondering if there is anything similar for velocity?

    Thanks.

  • #2
    You'll need to expose a Velocity helper that will access the ContextHolder (which is a ThreadLocal that holds the current Authentication).

    Comment


    • #3
      Any idea how this would be done. Any code example would help.

      Comment


      • #4
        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&#40;&#41; &#123;
          if &#40;ContextHolder.getContext&#40;&#41; != null && ContextHolder.getContext&#40;&#41; instanceof SecureContext&#41; &#123;
            return &#40;&#40;SecureContext&#41; ContextHolder.getContext&#40;&#41;&#41;.getAuthentication&#40;&#41;;
          &#125;
          return null;
        &#125;
        
        public boolean isGranted&#40;String role&#41; &#123;
          Authentication auth = getAuthentication&#40;&#41;;
          if &#40;auth == null&#41;
            return false;
          for &#40;int i=0; i < auth.getAuthorities&#40;&#41;.length; i++&#41; &#123;
            if &#40;role.equals&#40;auth.getAuthorities&#40;&#41;&#91;i&#93;.getAuthority&#40;&#41;&#41;&#41;
              return true;
          &#125;
          return false;
        &#125;
        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&#40;authority&#41;>
            <#nested>
          </#if>
        </#macro>
        HTH

        Comment

        Working...
        X