Coverage Report - org.acegisecurity.taglibs.velocity.AuthzImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
AuthzImpl
0% 
0% 
2.167
 
 1  
 /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
 2  
  *
 3  
  * Licensed under the Apache License, Version 2.0 (the "License");
 4  
  * you may not use this file except in compliance with the License.
 5  
  * You may obtain a copy of the License at
 6  
  *
 7  
  *     http://www.apache.org/licenses/LICENSE-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing, software
 10  
  * distributed under the License is distributed on an "AS IS" BASIS,
 11  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
  * See the License for the specific language governing permissions and
 13  
  * limitations under the License.
 14  
  */
 15  
 
 16  
 package org.acegisecurity.taglibs.velocity;
 17  
 
 18  
 import org.acegisecurity.acl.AclManager;
 19  
 
 20  
 import org.acegisecurity.taglibs.authz.AclTag;
 21  
 import org.acegisecurity.taglibs.authz.AuthenticationTag;
 22  
 import org.acegisecurity.taglibs.authz.AuthorizeTag;
 23  
 
 24  
 import org.springframework.context.ApplicationContext;
 25  
 
 26  
 import javax.servlet.jsp.JspException;
 27  
 import javax.servlet.jsp.PageContext;
 28  
 import javax.servlet.jsp.tagext.Tag;
 29  
 
 30  
 
 31  
 /**
 32  
  * I decided to wrap several JSP tag in one class, so I have to using inner class to wrap these JSP tag.  To using
 33  
  * this class, you need to inject Spring Context via SetAppCtx() method. AclTag need Spring Context to get AclManger
 34  
  * bean.
 35  
  */
 36  0
 public class AuthzImpl implements Authz {
 37  
     //~ Static fields/initializers =====================================================================================
 38  
 
 39  
     static final int ALL_GRANTED = 1;
 40  
     static final int ANY_GRANTED = 2;
 41  
     static final int NONE_GRANTED = 3;
 42  
 
 43  
     //~ Instance fields ================================================================================================
 44  
 
 45  
     private ApplicationContext appCtx;
 46  
 
 47  
     //~ Methods ========================================================================================================
 48  
 
 49  
     public boolean allGranted(String roles) {
 50  0
         return ifGranted(roles, ALL_GRANTED);
 51  
     }
 52  
 
 53  
     public boolean anyGranted(String roles) {
 54  0
         return ifGranted(roles, ANY_GRANTED);
 55  
     }
 56  
 
 57  
     public ApplicationContext getAppCtx() {
 58  0
         return appCtx;
 59  
     }
 60  
 
 61  
     /**
 62  
      * implementation of AuthenticationTag
 63  
      *
 64  
      * @return DOCUMENT ME!
 65  
      *
 66  
      * @throws IllegalArgumentException DOCUMENT ME!
 67  
      */
 68  
     public String getPrincipal() {
 69  0
         MyAuthenticationTag authenticationTag = new MyAuthenticationTag();
 70  
 
 71  0
         authenticationTag.setOperation("username");
 72  
 
 73  
         try {
 74  0
             authenticationTag.doStartTag();
 75  0
         } catch (JspException je) {
 76  0
             je.printStackTrace();
 77  0
             throw new IllegalArgumentException(je.getMessage());
 78  0
         }
 79  
 
 80  0
         return authenticationTag.getLastMessage();
 81  
     }
 82  
 
 83  
     /**
 84  
      * implementation of AclTag
 85  
      *
 86  
      * @param domainObject DOCUMENT ME!
 87  
      * @param permissions DOCUMENT ME!
 88  
      *
 89  
      * @return DOCUMENT ME!
 90  
      *
 91  
      * @throws IllegalArgumentException DOCUMENT ME!
 92  
      */
 93  
     public boolean hasPermission(Object domainObject, String permissions) {
 94  0
         MyAclTag aclTag = new MyAclTag();
 95  0
         aclTag.setPageContext(null);
 96  0
         aclTag.setContext(getAppCtx());
 97  0
         aclTag.setDomainObject(domainObject);
 98  0
         aclTag.setHasPermission(permissions);
 99  
 
 100  0
         int result = -1;
 101  
 
 102  
         try {
 103  0
             result = aclTag.doStartTag();
 104  0
         } catch (JspException je) {
 105  0
             throw new IllegalArgumentException(je.getMessage());
 106  0
         }
 107  
 
 108  0
         if (Tag.EVAL_BODY_INCLUDE == result) {
 109  0
             return true;
 110  
         } else {
 111  0
             return false;
 112  
         }
 113  
     }
 114  
 
 115  
     /**
 116  
      * implementation of AuthorizeTag
 117  
      *
 118  
      * @param roles DOCUMENT ME!
 119  
      * @param grantType DOCUMENT ME!
 120  
      *
 121  
      * @return DOCUMENT ME!
 122  
      *
 123  
      * @throws IllegalArgumentException DOCUMENT ME!
 124  
      */
 125  
     private boolean ifGranted(String roles, int grantType) {
 126  0
         AuthorizeTag authorizeTag = new AuthorizeTag();
 127  
 
 128  0
         int result = -1;
 129  
 
 130  
         try {
 131  0
             switch (grantType) {
 132  
             case ALL_GRANTED:
 133  0
                 authorizeTag.setIfAllGranted(roles);
 134  
 
 135  0
                 break;
 136  
 
 137  
             case ANY_GRANTED:
 138  0
                 authorizeTag.setIfAnyGranted(roles);
 139  
 
 140  0
                 break;
 141  
 
 142  
             case NONE_GRANTED:
 143  0
                 authorizeTag.setIfNotGranted(roles);
 144  
 
 145  0
                 break;
 146  
 
 147  
             default:
 148  0
                 throw new IllegalArgumentException("invalid granted type : " + grantType + " role=" + roles);
 149  
             }
 150  
 
 151  0
             result = authorizeTag.doStartTag();
 152  0
         } catch (JspException je) {
 153  0
             throw new IllegalArgumentException(je.getMessage());
 154  0
         }
 155  
 
 156  0
         if (Tag.EVAL_BODY_INCLUDE == result) {
 157  0
             return true;
 158  
         } else {
 159  0
             return false;
 160  
         }
 161  
     }
 162  
 
 163  
     public boolean noneGranted(String roles) {
 164  0
         return ifGranted(roles, NONE_GRANTED);
 165  
     }
 166  
 
 167  
     /**
 168  
      * test case can use this class to mock application context with aclManager bean in it.
 169  
      *
 170  
      * @param appCtx DOCUMENT ME!
 171  
      */
 172  
     public void setAppCtx(ApplicationContext appCtx) {
 173  0
         this.appCtx = appCtx;
 174  0
     }
 175  
 
 176  
     //~ Inner Classes ==================================================================================================
 177  
 
 178  
     /**
 179  
      * AclTag need to access the application context via the <code> WebApplicationContextUtils</code> and
 180  
      * locate an {@link AclManager}. WebApplicationContextUtils get application context via ServletContext. I decided
 181  
      * to let the Authz provide the Spring application context.
 182  
      */
 183  0
     private class MyAclTag extends AclTag {
 184  
         private static final long serialVersionUID = 6752340622125924108L;
 185  
         ApplicationContext context;
 186  
 
 187  
         protected ApplicationContext getContext(PageContext pageContext) {
 188  0
             return context;
 189  
         }
 190  
 
 191  
         protected void setContext(ApplicationContext context) {
 192  0
             this.context = context;
 193  0
         }
 194  
     }
 195  
 
 196  
     /**
 197  
      * it must output somthing to JSP page, so have to override the writeMessage method to avoid JSP related
 198  
      * operation. Get Idea from Acegi Test class.
 199  
      */
 200  0
     private class MyAuthenticationTag extends AuthenticationTag {
 201  
         private static final long serialVersionUID = -1094246833893599161L;
 202  0
         String lastMessage = null;
 203  
 
 204  
         public String getLastMessage() {
 205  0
             return lastMessage;
 206  
         }
 207  
 
 208  
         protected void writeMessage(String msg) throws JspException {
 209  0
             lastMessage = msg;
 210  0
         }
 211  
     }
 212  
 }