001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kns.service.impl;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
020import org.kuali.rice.kim.api.KimConstants;
021import org.kuali.rice.kim.api.permission.Permission;
022import org.kuali.rice.kim.impl.permission.PermissionBo;
023import org.kuali.rice.kns.kim.permission.PermissionTypeServiceBase;
024
025import java.util.ArrayList;
026import java.util.List;
027import java.util.Map;
028
029/**
030 * @author Kuali Rice Team (rice.collab@kuali.org)
031 */
032public class ComponentFieldPermissionTypeServiceImpl extends PermissionTypeServiceBase {
033        
034        /**
035         * Compare the component and property names between the request and matching permissions.
036         * Make entries with a matching property name take precedence over those with blank property 
037         * names on the stored permissions.  Only match entries with blank property names if
038         * no entries match on the exact property name. 
039         */
040        @Override
041        protected List<Permission> performPermissionMatches(Map<String, String> requestedDetails,
042                        List<Permission> permissionsList) {
043
044        List<Permission> propertyMatches = new ArrayList<Permission>();
045                List<Permission> prefixPropertyMatches = new ArrayList<Permission>();
046                List<Permission> blankPropertyMatches = new ArrayList<Permission>();
047                String propertyName = requestedDetails.get(KimConstants.AttributeConstants.PROPERTY_NAME);
048                String componentName = requestedDetails.get(KimConstants.AttributeConstants.COMPONENT_NAME);
049                for ( Permission kpi : permissionsList ) {
050            PermissionBo bo = PermissionBo.from(kpi);
051                        if ( StringUtils.equals( componentName, bo.getDetails().get( KimConstants.AttributeConstants.COMPONENT_NAME ) ) ) {
052                                String permPropertyName = bo.getDetails().get(KimConstants.AttributeConstants.PROPERTY_NAME);
053                                if ( StringUtils.isBlank( permPropertyName ) ) {
054                                        blankPropertyMatches.add( kpi );
055                                } else if ( StringUtils.equals( propertyName, permPropertyName ) ) {
056                                        propertyMatches.add( kpi );
057                                } else if ( doesPropertyNameMatch(propertyName, permPropertyName) ) {
058                                        prefixPropertyMatches.add( kpi );
059                                }
060                        }
061                }
062                if ( !propertyMatches.isEmpty() ) {
063                        return propertyMatches;
064                } else if ( !prefixPropertyMatches.isEmpty() ) {
065                        return prefixPropertyMatches;
066                } else {
067                        return blankPropertyMatches;
068                }
069        }
070
071}