Coverage Report - org.kuali.rice.krad.service.impl.NamespaceWildcardAllowedAndOrStringExactMatchPermissionTypeServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
NamespaceWildcardAllowedAndOrStringExactMatchPermissionTypeServiceImpl
0%
0/42
0%
0/22
4
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.krad.service.impl;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.core.api.CoreApiServiceLocator;
 20  
 import org.kuali.rice.core.api.namespace.Namespace;
 21  
 import org.kuali.rice.core.api.uif.RemotableAttributeError;
 22  
 import org.kuali.rice.kim.api.KimConstants;
 23  
 import org.kuali.rice.kim.api.permission.Permission;
 24  
 import org.kuali.rice.kim.api.type.KimType;
 25  
 import org.kuali.rice.kim.impl.permission.PermissionBo;
 26  
 
 27  
 import java.util.ArrayList;
 28  
 import java.util.Collections;
 29  
 import java.util.HashMap;
 30  
 import java.util.List;
 31  
 import java.util.Map;
 32  
 
 33  
 /**
 34  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 35  
  */
 36  0
 public class NamespaceWildcardAllowedAndOrStringExactMatchPermissionTypeServiceImpl
 37  
                 extends NamespacePermissionTypeServiceImpl {
 38  
         protected static final String NAMESPACE_CODE = KimConstants.UniqueKeyConstants.NAMESPACE_CODE;
 39  
         
 40  
         protected String exactMatchStringAttributeName;
 41  
         protected boolean namespaceRequiredOnStoredMap;
 42  0
     private List<String> requiredAttributes = new ArrayList<String>();
 43  
 
 44  
     @Override
 45  
     protected List<String> getRequiredAttributes() {
 46  0
         return Collections.unmodifiableList(requiredAttributes);
 47  
     }
 48  
 
 49  
         @Override
 50  
         protected List<Permission> performPermissionMatches(Map<String, String> requestedDetails, List<Permission> permissionsList) {
 51  0
             List<Permission> matchingPermissions = new ArrayList<Permission>();
 52  0
         List<Permission> matchingBlankPermissions = new ArrayList<Permission>();
 53  0
             String requestedAttributeValue = requestedDetails.get(exactMatchStringAttributeName);
 54  0
             for ( Permission kpi : permissionsList ) {
 55  0
             PermissionBo bo = PermissionBo.from(kpi);
 56  0
                 String permissionAttributeValue = bo.getDetails().get(exactMatchStringAttributeName);
 57  0
                 if ( StringUtils.equals(requestedAttributeValue, permissionAttributeValue) ) {
 58  0
                     matchingPermissions.add(kpi);
 59  0
                 } else if ( StringUtils.isBlank(permissionAttributeValue) ) {
 60  0
                     matchingBlankPermissions.add(kpi);
 61  
                 }
 62  0
             }
 63  
             // if the exact match worked, use those when checking the namespace
 64  
             // otherwise, use those with a blank additional property value
 65  0
             if ( !matchingPermissions.isEmpty() ) {
 66  0
             List<Permission> matchingWithNamespace = super.performPermissionMatches(requestedDetails, matchingPermissions);
 67  0
                 if ( !namespaceRequiredOnStoredMap ) {
 68  
                     // if the namespace is not required and the namespace match would have excluded
 69  
                     // the results, return the original set of matches
 70  0
                     if ( matchingWithNamespace.isEmpty() ) {
 71  0
                         return matchingPermissions;
 72  
                     }
 73  
                 }
 74  0
             return matchingWithNamespace;
 75  0
             } else if ( !matchingBlankPermissions.isEmpty() ) {
 76  0
             List<Permission> matchingWithNamespace = super.performPermissionMatches(requestedDetails, matchingBlankPermissions);
 77  0
             if ( !namespaceRequiredOnStoredMap ) {
 78  
                 // if the namespace is not required and the namespace match would have excluded
 79  
                 // the results, return the original set of matches
 80  0
                 if ( matchingWithNamespace.isEmpty() ) {
 81  0
                     return matchingBlankPermissions;
 82  
                 }
 83  
             }
 84  0
             return matchingWithNamespace;
 85  
             }
 86  0
             return matchingPermissions; // will be empty if drops to here
 87  
         }
 88  
         
 89  
         public void setExactMatchStringAttributeName(
 90  
                         String exactMatchStringAttributeName) {
 91  0
                 this.exactMatchStringAttributeName = exactMatchStringAttributeName;
 92  0
                 requiredAttributes.add(exactMatchStringAttributeName);
 93  0
         }
 94  
 
 95  
         public void setNamespaceRequiredOnStoredMap(
 96  
                         boolean namespaceRequiredOnStoredMap) {
 97  0
                 this.namespaceRequiredOnStoredMap = namespaceRequiredOnStoredMap;
 98  0
         }
 99  
 
 100  
         /**
 101  
          * Overrides the superclass's version of this method in order to account for "namespaceCode" permission detail values containing wildcards.
 102  
          */
 103  
         @Override
 104  
         protected List<RemotableAttributeError> validateReferencesExistAndActive(KimType kimType, Map<String, String> attributes, List<RemotableAttributeError> previousValidationErrors) {
 105  0
                 List<RemotableAttributeError> errors = new ArrayList<RemotableAttributeError>();
 106  0
                 Map<String, String> nonNamespaceCodeAttributes = new HashMap<String, String>(attributes);
 107  
                 // Check if "namespaceCode" is one of the permission detail values.
 108  0
                 if (attributes.containsKey(NAMESPACE_CODE)) {
 109  0
                         nonNamespaceCodeAttributes.remove(NAMESPACE_CODE);
 110  0
             final Namespace namespace = CoreApiServiceLocator.getNamespaceService().getNamespace(attributes.get(NAMESPACE_CODE));
 111  0
                         if (namespace != null) {
 112  0
                             errors.addAll(super.validateReferencesExistAndActive(kimType, Collections.singletonMap(NAMESPACE_CODE,
 113  
                         namespace.getCode()), previousValidationErrors));
 114  
                         } else {
 115  
                                 // If no namespaces were found, let the superclass generate an appropriate error.
 116  0
                                 errors.addAll(super.validateReferencesExistAndActive(kimType, Collections.singletonMap(NAMESPACE_CODE,
 117  
                         attributes.get(NAMESPACE_CODE)), previousValidationErrors));
 118  
                         }
 119  
                 }
 120  
                 // Validate all non-namespaceCode attributes.
 121  0
                 errors.addAll(super.validateReferencesExistAndActive(kimType, nonNamespaceCodeAttributes, previousValidationErrors));
 122  0
                 return errors;
 123  
         }
 124  
 }