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.maintenance;
017
018import org.kuali.rice.kim.api.KimConstants;
019import org.kuali.rice.kim.api.identity.Person;
020import org.kuali.rice.krad.document.DocumentAuthorizerBase;
021import org.kuali.rice.krad.service.DocumentDictionaryService;
022import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
023import org.kuali.rice.krad.util.KRADConstants;
024import org.kuali.rice.krad.util.KRADUtils;
025
026import java.util.HashMap;
027import java.util.Map;
028
029/**
030 * @author Kuali Rice Team (rice.collab@kuali.org)
031 */
032public class MaintenanceDocumentAuthorizerBase extends DocumentAuthorizerBase implements MaintenanceDocumentAuthorizer {
033    private static final long serialVersionUID = 6780013889553259327L;
034
035    private transient DocumentDictionaryService documentDictionaryService;
036
037    public final boolean canCreate(Class boClass, Person user) {
038        Map<String, String> permissionDetails = new HashMap<String, String>();
039        permissionDetails.put(KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME,
040                getDocumentDictionaryService().getMaintenanceDocumentTypeName(boClass));
041        permissionDetails.put(KRADConstants.MAINTENANCE_ACTN, KRADConstants.MAINTENANCE_NEW_ACTION);
042
043        return !permissionExistsByTemplate(KRADConstants.KNS_NAMESPACE,
044                KimConstants.PermissionTemplateNames.CREATE_MAINTAIN_RECORDS, permissionDetails)
045                || getPermissionService().isAuthorizedByTemplate(user.getPrincipalId(), KRADConstants.KNS_NAMESPACE,
046                KimConstants.PermissionTemplateNames.CREATE_MAINTAIN_RECORDS, permissionDetails,
047                new HashMap<String, String>());
048    }
049
050    public final boolean canMaintain(Object dataObject, Person user) {
051        Map<String, String> permissionDetails = new HashMap<String, String>(2);
052        permissionDetails.put(KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME,
053                getDocumentDictionaryService().getMaintenanceDocumentTypeName(dataObject.getClass()));
054        permissionDetails.put(KRADConstants.MAINTENANCE_ACTN, KRADConstants.MAINTENANCE_EDIT_ACTION);
055
056        return !permissionExistsByTemplate(KRADConstants.KNS_NAMESPACE,
057                KimConstants.PermissionTemplateNames.CREATE_MAINTAIN_RECORDS, permissionDetails)
058                || isAuthorizedByTemplate(dataObject, KRADConstants.KNS_NAMESPACE,
059                KimConstants.PermissionTemplateNames.CREATE_MAINTAIN_RECORDS, user.getPrincipalId(), permissionDetails,
060                null);
061    }
062
063    public final boolean canCreateOrMaintain(MaintenanceDocument maintenanceDocument, Person user) {
064        return !permissionExistsByTemplate(maintenanceDocument, KRADConstants.KNS_NAMESPACE,
065                KimConstants.PermissionTemplateNames.CREATE_MAINTAIN_RECORDS) || isAuthorizedByTemplate(
066                maintenanceDocument, KRADConstants.KNS_NAMESPACE,
067                KimConstants.PermissionTemplateNames.CREATE_MAINTAIN_RECORDS, user.getPrincipalId());
068    }
069
070    @SuppressWarnings("unchecked")
071    @Override
072    protected void addRoleQualification(Object dataObject, Map<String, String> attributes) {
073        super.addRoleQualification(dataObject, attributes);
074
075        if (dataObject instanceof MaintenanceDocument) {
076            MaintenanceDocument maintDoc = (MaintenanceDocument) dataObject;
077            if (maintDoc.getNewMaintainableObject() != null) {
078                attributes.putAll(KRADUtils.getNamespaceAndComponentSimpleName(
079                        maintDoc.getNewMaintainableObject().getDataObjectClass()));
080            }
081        }
082    }
083
084    @SuppressWarnings("unchecked")
085    @Override
086    protected void addPermissionDetails(Object dataObject, Map<String, String> attributes) {
087        super.addPermissionDetails(dataObject, attributes);
088
089        if (dataObject instanceof MaintenanceDocument) {
090            MaintenanceDocument maintDoc = (MaintenanceDocument) dataObject;
091            if (maintDoc.getNewMaintainableObject() != null) {
092                attributes.putAll(KRADUtils.getNamespaceAndComponentSimpleName(
093                        maintDoc.getNewMaintainableObject().getDataObjectClass()));
094                attributes.put(KRADConstants.MAINTENANCE_ACTN,
095                        maintDoc.getNewMaintainableObject().getMaintenanceAction());
096            }
097        }
098    }
099
100    protected DocumentDictionaryService getDocumentDictionaryService() {
101        if (documentDictionaryService == null) {
102            documentDictionaryService = KRADServiceLocatorWeb.getDocumentDictionaryService();
103        }
104        return documentDictionaryService;
105    }
106
107    public void setDocumentDictionaryService(DocumentDictionaryService documentDictionaryService) {
108        this.documentDictionaryService = documentDictionaryService;
109    }
110}