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.kim.permission;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.kim.api.permission.Permission;
020import org.kuali.rice.kim.framework.permission.PermissionTypeService;
021import org.kuali.rice.kns.kim.type.DataDictionaryTypeServiceBase;
022
023import java.util.ArrayList;
024import java.util.Collections;
025import java.util.List;
026import java.util.Map;
027
028/**
029 * @deprecated A krad integrated type service base class will be provided in the future.
030 */
031@Deprecated
032public class PermissionTypeServiceBase extends DataDictionaryTypeServiceBase implements PermissionTypeService {
033
034        @Override
035        public final List<Permission> getMatchingPermissions(Map<String, String> requestedDetails, List<Permission> permissionsList) {
036                requestedDetails = translateInputAttributes(requestedDetails);
037                validateRequiredAttributesAgainstReceived(requestedDetails);
038                return Collections.unmodifiableList(performPermissionMatches(requestedDetails, permissionsList));
039        }
040
041        /**
042         * Internal method for matching permissions.  Override this method to customize the matching behavior.
043         * 
044         * This base implementation uses the {@link #performMatch(Map, Map)} method
045         * to perform an exact match on the permission details and return all that are equal.
046         */
047        protected List<Permission> performPermissionMatches(Map<String, String> requestedDetails, List<Permission> permissionsList) {
048                List<Permission> matchingPermissions = new ArrayList<Permission>();
049                for (Permission permission : permissionsList) {
050                        if ( performMatch(requestedDetails, permission.getAttributes()) ) {
051                                matchingPermissions.add( permission );
052                        }
053                }
054                return matchingPermissions;
055        }
056        
057        /**
058         * 
059         * Internal method for checking if property name matches
060         * 
061         * @param requestedDetailsPropertyName name of requested details property
062         * @param permissionDetailsPropertyName name of permission details property
063         * @return boolean 
064         */
065        protected boolean doesPropertyNameMatch(
066                        String requestedDetailsPropertyName,
067                        String permissionDetailsPropertyName) {
068                if (StringUtils.isBlank(permissionDetailsPropertyName)) {
069                        return true;
070                }
071                if ( requestedDetailsPropertyName == null ) {
072                    requestedDetailsPropertyName = ""; // prevent NPE
073                }
074                return StringUtils.equals(requestedDetailsPropertyName, permissionDetailsPropertyName)
075                                || (requestedDetailsPropertyName.startsWith(permissionDetailsPropertyName+"."));
076        }
077}