001    /*
002     * Copyright 2011 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     */
016    package org.kuali.kfs.sys.document.authorization;
017    
018    import java.util.Set;
019    
020    import org.apache.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    import org.kuali.kfs.sys.KFSConstants;
023    import org.kuali.kfs.sys.KFSConstants.PermissionTemplate;
024    import org.kuali.rice.kim.bo.Person;
025    import org.kuali.rice.kns.document.Document;
026    import org.kuali.rice.kns.document.authorization.TransactionalDocumentAuthorizerBase;
027    import org.kuali.rice.kns.util.KNSConstants;
028    
029    public class FinancialSystemTransactionalDocumentAuthorizerBase extends TransactionalDocumentAuthorizerBase {
030        protected static Log LOG = LogFactory.getLog(FinancialSystemTransactionalDocumentAuthorizerBase.class);
031    
032        /**
033         * Overridden to check if document error correction can be allowed here.
034         * 
035         * @see org.kuali.rice.kns.document.authorization.DocumentAuthorizerBase#getDocumentActions(org.kuali.rice.kns.document.Document,
036         *      org.kuali.rice.kim.bo.Person, java.util.Set)
037         */
038        @Override
039        public Set<String> getDocumentActions(Document document, Person user, Set<String> documentActionsFromPresentationController) {
040            Set<String> documentActionsToReturn = super.getDocumentActions(document, user, documentActionsFromPresentationController);
041            
042            if (documentActionsToReturn.contains(KFSConstants.KFS_ACTION_CAN_ERROR_CORRECT) 
043                    && !(documentActionsToReturn.contains(KNSConstants.KUALI_ACTION_CAN_COPY) 
044                    && canErrorCorrect(document, user))) {
045                documentActionsToReturn.remove(KFSConstants.KFS_ACTION_CAN_ERROR_CORRECT);
046            }
047            
048            if (documentActionsToReturn.contains(KFSConstants.KFS_ACTION_CAN_EDIT_BANK) 
049                    && !canEditBankCode(document, user)) {
050                documentActionsToReturn.remove(KFSConstants.KFS_ACTION_CAN_EDIT_BANK);
051            }
052            
053            return documentActionsToReturn;
054        }
055    
056        /**
057         * Determines if the KIM permission is available to error correct the given document
058         * 
059         * @param document the document to correct
060         * @param user the user to check error correction for
061         * @return true if the user can error correct, false otherwise
062         */
063        public boolean canErrorCorrect(Document document, Person user) {
064            return isAuthorizedByTemplate(document, KFSConstants.ParameterNamespaces.KFS, PermissionTemplate.ERROR_CORRECT_DOCUMENT.name, user.getPrincipalId());
065        }
066        
067        /**
068         * Determines if the KIM permission is available to error correct the given document
069         * 
070         * @param document the document to correct
071         * @param user the user to check error correction for
072         * @return true if the user can error correct, false otherwise
073         */
074        public boolean canEditBankCode(Document document, Person user) {
075            return isAuthorizedByTemplate(document, KFSConstants.ParameterNamespaces.KFS, PermissionTemplate.EDIT_BANK_CODE.name, user.getPrincipalId());
076        }
077    }