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