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.kns.kim.permission.PermissionTypeServiceBase;
021import org.kuali.rice.krad.util.KRADConstants;
022
023import java.util.Map;
024
025public class DocumentTypeAndExistingRecordsOnlyPermissionTypeServiceImpl extends PermissionTypeServiceBase {
026
027    @Override
028    protected boolean performMatch(Map<String, String> inputMap,
029                        Map<String, String> storedMap) {
030
031                String requestedDocumentType = inputMap.get(KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME);
032                String permissionDocumentType = storedMap.get(KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME);
033                if ( requestedDocumentType == null || permissionDocumentType == null ) {
034                        return false; // again, don't match if missing document types
035                }
036                // exit if document types don't match
037                if ( !requestedDocumentType.equals(permissionDocumentType) ) {
038                        return false;
039                }
040                // check the existing attributes only flag
041                if ( !Boolean.parseBoolean(storedMap.get(KimConstants.AttributeConstants.EXISTING_RECORDS_ONLY)) ) {
042                        // if not set, then any document action allowed
043                        return true;
044                }
045                // otherwise, only edit actions are allowed (no New/Copy)
046                return StringUtils.equals(inputMap.get(KRADConstants.MAINTENANCE_ACTN), KRADConstants.MAINTENANCE_EDIT_ACTION);
047        }
048}