Coverage Report - org.kuali.rice.krad.service.impl.NamespaceWildcardAllowedAndOrStringExactMatchPermissionTypeServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
NamespaceWildcardAllowedAndOrStringExactMatchPermissionTypeServiceImpl
0%
0/40
0%
0/22
4.75
 
 1  
 /*
 2  
  * Copyright 2007-2009 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.kim.api.permission.Permission;
 22  
 import org.kuali.rice.kim.api.type.KimType;
 23  
 import org.kuali.rice.kim.impl.permission.PermissionBo;
 24  
 import org.kuali.rice.kim.util.KimConstants;
 25  
 
 26  
 import java.util.ArrayList;
 27  
 import java.util.Collections;
 28  
 import java.util.HashMap;
 29  
 import java.util.List;
 30  
 import java.util.Map;
 31  
 
 32  
 /**
 33  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 34  
  */
 35  0
 public class NamespaceWildcardAllowedAndOrStringExactMatchPermissionTypeServiceImpl
 36  
                 extends NamespacePermissionTypeServiceImpl {
 37  
         protected static final String NAMESPACE_CODE = KimConstants.UniqueKeyConstants.NAMESPACE_CODE;
 38  
         
 39  
         protected String exactMatchStringAttributeName;
 40  
         protected boolean namespaceRequiredOnStoredMap;
 41  
 
 42  
         @Override
 43  
         protected List<Permission> performPermissionMatches(Map<String, String> requestedDetails, List<Permission> permissionsList) {
 44  0
             List<Permission> matchingPermissions = new ArrayList<Permission>();
 45  0
         List<Permission> matchingBlankPermissions = new ArrayList<Permission>();
 46  0
             String requestedAttributeValue = requestedDetails.get(exactMatchStringAttributeName);
 47  0
             for ( Permission kpi : permissionsList ) {
 48  0
             PermissionBo bo = PermissionBo.from(kpi);
 49  0
                 String permissionAttributeValue = bo.getDetails().get(exactMatchStringAttributeName);
 50  0
                 if ( StringUtils.equals(requestedAttributeValue, permissionAttributeValue) ) {
 51  0
                     matchingPermissions.add(kpi);
 52  0
                 } else if ( StringUtils.isBlank(permissionAttributeValue) ) {
 53  0
                     matchingBlankPermissions.add(kpi);
 54  
                 }
 55  0
             }
 56  
             // if the exact match worked, use those when checking the namespace
 57  
             // otherwise, use those with a blank additional property value
 58  0
             if ( !matchingPermissions.isEmpty() ) {
 59  0
             List<Permission> matchingWithNamespace = super.performPermissionMatches(requestedDetails, matchingPermissions);
 60  0
                 if ( !namespaceRequiredOnStoredMap ) {
 61  
                     // if the namespace is not required and the namespace match would have excluded
 62  
                     // the results, return the original set of matches
 63  0
                     if ( matchingWithNamespace.isEmpty() ) {
 64  0
                         return matchingPermissions;
 65  
                     }
 66  
                 }
 67  0
             return matchingWithNamespace;
 68  0
             } else if ( !matchingBlankPermissions.isEmpty() ) {
 69  0
             List<Permission> matchingWithNamespace = super.performPermissionMatches(requestedDetails, matchingBlankPermissions);
 70  0
             if ( !namespaceRequiredOnStoredMap ) {
 71  
                 // if the namespace is not required and the namespace match would have excluded
 72  
                 // the results, return the original set of matches
 73  0
                 if ( matchingWithNamespace.isEmpty() ) {
 74  0
                     return matchingBlankPermissions;
 75  
                 }
 76  
             }
 77  0
             return matchingWithNamespace;
 78  
             }
 79  0
             return matchingPermissions; // will be empty if drops to here
 80  
         }
 81  
         
 82  
         public void setExactMatchStringAttributeName(
 83  
                         String exactMatchStringAttributeName) {
 84  0
                 this.exactMatchStringAttributeName = exactMatchStringAttributeName;
 85  0
                 requiredAttributes.add(exactMatchStringAttributeName);
 86  0
         }
 87  
 
 88  
         public void setNamespaceRequiredOnStoredMap(
 89  
                         boolean namespaceRequiredOnStoredMap) {
 90  0
                 this.namespaceRequiredOnStoredMap = namespaceRequiredOnStoredMap;
 91  0
         }
 92  
 
 93  
         /**
 94  
          * Overrides the superclass's version of this method in order to account for "namespaceCode" permission detail values containing wildcards.
 95  
          */
 96  
         @Override
 97  
         protected Map<String, List<String>> validateReferencesExistAndActive(KimType kimType, Map<String, String> attributes, Map<String, String> previousValidationErrors) {
 98  0
                 Map<String,List<String>> errors = new HashMap<String,List<String>>();
 99  0
                 Map<String, String> nonNamespaceCodeAttributes = new HashMap<String, String>(attributes);
 100  
                 // Check if "namespaceCode" is one of the permission detail values.
 101  0
                 if (attributes.containsKey(NAMESPACE_CODE)) {
 102  0
                         nonNamespaceCodeAttributes.remove(NAMESPACE_CODE);
 103  0
             final Namespace namespace = CoreApiServiceLocator.getNamespaceService().getNamespace(attributes.get(NAMESPACE_CODE));
 104  0
                         if (namespace != null) {
 105  0
                             errors.putAll(super.validateReferencesExistAndActive(kimType, Collections.singletonMap(NAMESPACE_CODE,
 106  
                         namespace.getCode()), previousValidationErrors));
 107  
                         } else {
 108  
                                 // If no namespaces were found, let the superclass generate an appropriate error.
 109  0
                                 errors.putAll(super.validateReferencesExistAndActive(kimType, Collections.singletonMap(NAMESPACE_CODE,
 110  
                         attributes.get(NAMESPACE_CODE)), previousValidationErrors));
 111  
                         }
 112  
                 }
 113  
                 // Validate all non-namespaceCode attributes.
 114  0
                 errors.putAll(super.validateReferencesExistAndActive(kimType, nonNamespaceCodeAttributes, previousValidationErrors));
 115  0
                 return errors;
 116  
         }
 117  
 }