Results 1 to 4 of 4

Thread: Velocity & Acegi

  1. #1
    Join Date
    Sep 2004
    Location
    Sydney
    Posts
    27

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

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

  3. #3

    Default

    Any idea how this would be done. Any code example would help.

  4. #4
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    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

Similar Threads

  1. Replies: 8
    Last Post: Mar 19th, 2008, 11:13 AM
  2. Acegi running fine. Howto add roles, ...
    By ThomasBecker in forum Security
    Replies: 9
    Last Post: Sep 16th, 2007, 08:16 AM
  3. Replies: 9
    Last Post: Sep 5th, 2006, 06:50 AM
  4. FreeMarker vs Velocity
    By Martin Kersten in forum Architecture
    Replies: 8
    Last Post: May 30th, 2005, 09:21 AM
  5. Replies: 4
    Last Post: Nov 2nd, 2004, 02:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •