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.document.authorization;
017
018import org.kuali.rice.kim.api.KimConstants;
019import org.kuali.rice.kim.api.identity.Person;
020import org.kuali.rice.krad.document.Document;
021import org.kuali.rice.krad.util.KRADConstants;
022
023import java.util.HashMap;
024import java.util.HashSet;
025import java.util.Map;
026import java.util.Set;
027
028/**
029 * Base class for all TransactionalDocumentAuthorizers.
030 */
031public class TransactionalDocumentAuthorizerBase extends DocumentAuthorizerBase
032                implements TransactionalDocumentAuthorizer {
033        public final Set<String> getEditModes(Document document, Person user,
034                        Set<String> editModes) {
035                Set<String> unauthorizedEditModes = new HashSet<String>();
036                for (String editMode : editModes) {
037                        Map<String, String> additionalPermissionDetails = new HashMap<String, String>();
038                        additionalPermissionDetails.put(KimConstants.AttributeConstants.EDIT_MODE, editMode);
039                        if (permissionExistsByTemplate(
040                                        document,
041                                        KRADConstants.KNS_NAMESPACE,
042                                        KimConstants.PermissionTemplateNames.USE_TRANSACTIONAL_DOCUMENT,
043                                        additionalPermissionDetails)
044                                        && !isAuthorizedByTemplate(
045                                                        document,
046                                                        KRADConstants.KNS_NAMESPACE,
047                                                        KimConstants.PermissionTemplateNames.USE_TRANSACTIONAL_DOCUMENT,
048                                                        user.getPrincipalId(), additionalPermissionDetails,
049                                                        null)) {
050                                unauthorizedEditModes.add(editMode);
051                        }
052                }
053                editModes.removeAll(unauthorizedEditModes);
054                return editModes;
055        }
056}