Clover coverage report - Acegi Security System for Spring - 1.0.0-RC1
Coverage timestamp: Mon Dec 5 2005 09:05:15 EST
file stats: LOC: 217   Methods: 12
NCLOC: 107   Classes: 3
 
 Source file Conditionals Statements Methods TOTAL
AuthzImpl.java 0% 0% 0% 0%
coverage
 1    /* Copyright 2004, 2005 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
 33    * class to wrap these JSP tag. To using this class, you need to inject
 34    * Spring Context via SetAppCtx() method. AclTag need Spring Context to get
 35    * AclManger bean.
 36    */
 37    public class AuthzImpl implements Authz {
 38    //~ Static fields/initializers =============================================
 39   
 40    static final int ALL_GRANTED = 1;
 41    static final int ANY_GRANTED = 2;
 42    static final int NONE_GRANTED = 3;
 43   
 44    //~ Instance fields ========================================================
 45   
 46    private ApplicationContext appCtx;
 47   
 48    //~ Methods ================================================================
 49   
 50  0 public boolean allGranted(String roles) {
 51  0 return ifGranted(roles, ALL_GRANTED);
 52    }
 53   
 54  0 public boolean anyGranted(String roles) {
 55  0 return ifGranted(roles, ANY_GRANTED);
 56    }
 57   
 58  0 public ApplicationContext getAppCtx() {
 59  0 return appCtx;
 60    }
 61   
 62    /**
 63    * implementation of AuthenticationTag
 64    *
 65    * @return DOCUMENT ME!
 66    *
 67    * @throws IllegalArgumentException DOCUMENT ME!
 68    */
 69  0 public String getPrincipal() {
 70  0 MyAuthenticationTag authenticationTag = new MyAuthenticationTag();
 71   
 72  0 authenticationTag.setOperation("username");
 73   
 74  0 try {
 75  0 authenticationTag.doStartTag();
 76    } catch (JspException je) {
 77  0 je.printStackTrace();
 78  0 throw new IllegalArgumentException(je.getMessage());
 79    }
 80   
 81  0 return authenticationTag.getLastMessage();
 82    }
 83   
 84    /**
 85    * implementation of AclTag
 86    *
 87    * @param domainObject DOCUMENT ME!
 88    * @param permissions DOCUMENT ME!
 89    *
 90    * @return DOCUMENT ME!
 91    *
 92    * @throws IllegalArgumentException DOCUMENT ME!
 93    */
 94  0 public boolean hasPermission(Object domainObject, String permissions) {
 95  0 MyAclTag aclTag = new MyAclTag();
 96  0 aclTag.setPageContext(null);
 97  0 aclTag.setContext(getAppCtx());
 98  0 aclTag.setDomainObject(domainObject);
 99  0 aclTag.setHasPermission(permissions);
 100   
 101  0 int result = -1;
 102   
 103  0 try {
 104  0 result = aclTag.doStartTag();
 105    } catch (JspException je) {
 106  0 throw new IllegalArgumentException(je.getMessage());
 107    }
 108   
 109  0 if (Tag.EVAL_BODY_INCLUDE == result) {
 110  0 return true;
 111    } else {
 112  0 return false;
 113    }
 114    }
 115   
 116    /**
 117    * implementation of AuthorizeTag
 118    *
 119    * @param roles DOCUMENT ME!
 120    * @param grantType DOCUMENT ME!
 121    *
 122    * @return DOCUMENT ME!
 123    *
 124    * @throws IllegalArgumentException DOCUMENT ME!
 125    */
 126  0 private boolean ifGranted(String roles, int grantType) {
 127  0 AuthorizeTag authorizeTag = new AuthorizeTag();
 128   
 129  0 int result = -1;
 130   
 131  0 try {
 132  0 switch (grantType) {
 133  0 case ALL_GRANTED:
 134  0 authorizeTag.setIfAllGranted(roles);
 135   
 136  0 break;
 137   
 138  0 case ANY_GRANTED:
 139  0 authorizeTag.setIfAnyGranted(roles);
 140   
 141  0 break;
 142   
 143  0 case NONE_GRANTED:
 144  0 authorizeTag.setIfNotGranted(roles);
 145   
 146  0 break;
 147   
 148  0 default:
 149  0 throw new IllegalArgumentException("invalid granted type : "
 150    + grantType + " role=" + roles);
 151    }
 152   
 153  0 result = authorizeTag.doStartTag();
 154    } catch (JspException je) {
 155  0 throw new IllegalArgumentException(je.getMessage());
 156    }
 157   
 158  0 if (Tag.EVAL_BODY_INCLUDE == result) {
 159  0 return true;
 160    } else {
 161  0 return false;
 162    }
 163    }
 164   
 165  0 public boolean noneGranted(String roles) {
 166  0 return ifGranted(roles, NONE_GRANTED);
 167    }
 168   
 169    /**
 170    * test case can use this class to mock application context with aclManager
 171    * bean in it.
 172    *
 173    * @param appCtx DOCUMENT ME!
 174    */
 175  0 public void setAppCtx(ApplicationContext appCtx) {
 176  0 this.appCtx = appCtx;
 177    }
 178   
 179    //~ Inner Classes ==========================================================
 180   
 181    /**
 182    * AclTag need to access the application context via the <code>
 183    * WebApplicationContextUtils</code> and locate an {@link AclManager}.
 184    * WebApplicationContextUtils get application context via ServletContext.
 185    * I decided to let the Authz provide the Spring application context.
 186    */
 187    private class MyAclTag extends AclTag {
 188    private static final long serialVersionUID = 6752340622125924108L;
 189    ApplicationContext context;
 190   
 191  0 protected ApplicationContext getContext(PageContext pageContext) {
 192  0 return context;
 193    }
 194   
 195  0 protected void setContext(ApplicationContext context) {
 196  0 this.context = context;
 197    }
 198    }
 199   
 200    /**
 201    * it must output somthing to JSP page, so have to override the
 202    * writeMessage method to avoid JSP related operation. Get Idea from Acegi
 203    * Test class.
 204    */
 205    private class MyAuthenticationTag extends AuthenticationTag {
 206    private static final long serialVersionUID = -1094246833893599161L;
 207    String lastMessage = null;
 208   
 209  0 public String getLastMessage() {
 210  0 return lastMessage;
 211    }
 212   
 213  0 protected void writeMessage(String msg) throws JspException {
 214  0 lastMessage = msg;
 215    }
 216    }
 217    }