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.krad.kim;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.kim.api.KimConstants;
020import org.kuali.rice.kim.api.permission.Permission;
021import org.kuali.rice.kim.impl.permission.PermissionBo;
022import org.kuali.rice.kns.kim.permission.PermissionTypeServiceBase;
023
024import java.util.ArrayList;
025import java.util.List;
026import java.util.Map;
027
028/**
029 * @author Kuali Rice Team (rice.collab@kuali.org)
030 */
031public class NamespacePermissionTypeServiceImpl extends PermissionTypeServiceBase {
032
033        private Boolean exactMatchPriority = true;
034        {
035//              requiredAttributes.add(KimAttributes.NAMESPACE_CODE);
036        }
037        
038        /**
039         * Check for entries that match the namespace.
040         * 
041         * By default, this method will return all exact matches if any exist, and it will only return partial matches if there are no exact matches.
042         * i.e. KR-NS will have priority over KR-*
043         * 
044     * If ExactMatchPriority is false, then this method will return all exact AND partial matching permissions.  By default, ExactMatchPriority will be set to true.
045         */
046        @Override
047        protected List<Permission> performPermissionMatches(Map<String, String> requestedDetails, List<Permission> permissionsList) {
048                List<Permission> matchingPermissions = new ArrayList<Permission>();
049                
050                String requestedNamespaceCode = requestedDetails.get(KimConstants.AttributeConstants.NAMESPACE_CODE);
051                
052                // Add all exact matches to the list 
053                for ( Permission permission : permissionsList ) {
054            PermissionBo bo = PermissionBo.from(permission);
055                        String permissionNamespaceCode = bo.getDetails().get(KimConstants.AttributeConstants.NAMESPACE_CODE);
056                        if ( StringUtils.equals(requestedNamespaceCode, permissionNamespaceCode ) ) {
057                                matchingPermissions.add(permission);
058                        } 
059                }
060                
061                // Add partial matches to the list if there are no exact matches or if exactMatchPriority is false
062                if ((exactMatchPriority && matchingPermissions.isEmpty()) || (!(exactMatchPriority))) {
063                        for ( Permission kpi : permissionsList ) {
064                PermissionBo bo = PermissionBo.from(kpi);
065                                String permissionNamespaceCode = bo.getDetails().get(KimConstants.AttributeConstants.NAMESPACE_CODE);
066                                if ( requestedNamespaceCode != null
067                                         && permissionNamespaceCode != null
068                                         && (!( StringUtils.equals(requestedNamespaceCode, permissionNamespaceCode ))) 
069                                         && requestedNamespaceCode.matches(permissionNamespaceCode.replaceAll("\\*", ".*") ) ) {
070                                                matchingPermissions.add(kpi);
071                                }
072                        }
073                }
074        return matchingPermissions;
075        }
076                
077        public Boolean getExactMatchPriority() {
078                return this.exactMatchPriority;
079        }
080
081        public void setExactMatchPriority(Boolean exactMatchPriority) {
082                this.exactMatchPriority = exactMatchPriority;
083        }
084}