Coverage Report - org.acegisecurity.vote.UnanimousBased
 
Classes in this File Line Coverage Branch Coverage Complexity
UnanimousBased
100% 
100% 
8
 
 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.vote;
 17  
 
 18  
 import org.acegisecurity.AccessDeniedException;
 19  
 import org.acegisecurity.Authentication;
 20  
 import org.acegisecurity.ConfigAttribute;
 21  
 import org.acegisecurity.ConfigAttributeDefinition;
 22  
 
 23  
 import java.util.Iterator;
 24  
 
 25  
 
 26  
 /**
 27  
  * Simple concrete implementation of  {@link org.acegisecurity.AccessDecisionManager} that  requires all voters to
 28  
  * abstain or grant access.
 29  
  */
 30  8
 public class UnanimousBased extends AbstractAccessDecisionManager {
 31  
     //~ Methods ========================================================================================================
 32  
 
 33  
     /**
 34  
      * This concrete implementation polls all configured  {@link AccessDecisionVoter}s for each {@link
 35  
      * ConfigAttribute} and grants access if <b>only</b> grant votes were received.<p>Other voting
 36  
      * implementations usually pass the entire list of {@link ConfigAttributeDefinition}s to the
 37  
      * <code>AccessDecisionVoter</code>. This implementation differs in that each <code>AccessDecisionVoter</code>
 38  
      * knows only about a single <code>ConfigAttribute</code> at a time.</p>
 39  
      *  <p>If every <code>AccessDecisionVoter</code> abstained from voting, the decision will be based on the
 40  
      * {@link #isAllowIfAllAbstainDecisions()} property (defaults to false).</p>
 41  
      *
 42  
      * @param authentication the caller invoking the method
 43  
      * @param object the secured object
 44  
      * @param config the configuration attributes associated with the method being invoked
 45  
      *
 46  
      * @throws AccessDeniedException if access is denied
 47  
      */
 48  
     public void decide(Authentication authentication, Object object, ConfigAttributeDefinition config)
 49  
         throws AccessDeniedException {
 50  31
         int grant = 0;
 51  31
         int abstain = 0;
 52  
 
 53  31
         Iterator configIter = config.getConfigAttributes();
 54  
 
 55  77
         while (configIter.hasNext()) {
 56  54
             ConfigAttributeDefinition thisDef = new ConfigAttributeDefinition();
 57  54
             thisDef.addConfigAttribute((ConfigAttribute) configIter.next());
 58  
 
 59  54
             Iterator voters = this.getDecisionVoters().iterator();
 60  
 
 61  161
             while (voters.hasNext()) {
 62  115
                 AccessDecisionVoter voter = (AccessDecisionVoter) voters.next();
 63  115
                 int result = voter.vote(authentication, object, thisDef);
 64  
 
 65  115
                 switch (result) {
 66  
                 case AccessDecisionVoter.ACCESS_GRANTED:
 67  44
                     grant++;
 68  
 
 69  44
                     break;
 70  
 
 71  
                 case AccessDecisionVoter.ACCESS_DENIED:
 72  8
                     throw new AccessDeniedException(messages.getMessage("AbstractAccessDecisionManager.accessDenied",
 73  
                             "Access is denied"));
 74  
 
 75  
                 default:
 76  63
                     abstain++;
 77  
 
 78  
                     break;
 79  
                 }
 80  107
             }
 81  46
         }
 82  
 
 83  
         // To get this far, there were no deny votes
 84  23
         if (grant > 0) {
 85  21
             return;
 86  
         }
 87  
 
 88  
         // To get this far, every AccessDecisionVoter abstained
 89  2
         checkAllowIfAllAbstainDecisions();
 90  1
     }
 91  
 }