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.Collections;
026import java.util.List;
027import java.util.Map;
028
029/**
030 * Type service for the 'View Line Action' KIM type which matches on the id for a UIF view, group id or collection
031 * property name, field id or action event
032 *
033 * @author Kuali Rice Team (rice.collab@kuali.org)
034 */
035public class ViewLineActionPermissionTypeServiceImpl extends PermissionTypeServiceBase {
036
037    @Override
038    protected List<String> getRequiredAttributes() {
039        List<String> attributes = new ArrayList<String>(super.getRequiredAttributes());
040        attributes.add(KimConstants.AttributeConstants.VIEW_ID);
041
042        return Collections.unmodifiableList(attributes);
043    }
044
045    /**
046     * Filters the given permission list to return those that match on group id or collection property name, field
047     * id or action event, then calls super to filter based on view id
048     *
049     * @param requestedDetails - map of details requested with permission (used for matching)
050     * @param permissionsList - list of permissions to process for matches
051     * @return List<Permission> list of permissions that match the requested details
052     */
053    @Override
054    protected List<Permission> performPermissionMatches(Map<String, String> requestedDetails,
055            List<Permission> permissionsList) {
056
057        String requestedGroupId = null;
058        if (requestedDetails.containsKey(KimConstants.AttributeConstants.GROUP_ID)) {
059            requestedGroupId = requestedDetails.get(KimConstants.AttributeConstants.GROUP_ID);
060        }
061
062        String requestedCollectionPropertyName = null;
063        if (requestedDetails.containsKey(KimConstants.AttributeConstants.COLLECTION_PROPERTY_NAME)) {
064            requestedCollectionPropertyName = requestedDetails.get(
065                    KimConstants.AttributeConstants.COLLECTION_PROPERTY_NAME);
066        }
067
068        String requestedFieldId = null;
069        if (requestedDetails.containsKey(KimConstants.AttributeConstants.FIELD_ID)) {
070            requestedFieldId = requestedDetails.get(KimConstants.AttributeConstants.FIELD_ID);
071        }
072
073        String requestedActionEvent = null;
074        if (requestedDetails.containsKey(KimConstants.AttributeConstants.ACTION_EVENT)) {
075            requestedActionEvent = requestedDetails.get(KimConstants.AttributeConstants.ACTION_EVENT);
076        }
077
078        List<Permission> matchingPermissions = new ArrayList<Permission>();
079        for (Permission permission : permissionsList) {
080            PermissionBo bo = PermissionBo.from(permission);
081
082            String permissionGroupId = null;
083            if (bo.getDetails().containsKey(KimConstants.AttributeConstants.GROUP_ID)) {
084                permissionGroupId = bo.getDetails().get(KimConstants.AttributeConstants.GROUP_ID);
085            }
086
087            String permissionCollectionPropertyName = null;
088            if (bo.getDetails().containsKey(KimConstants.AttributeConstants.COLLECTION_PROPERTY_NAME)) {
089                permissionCollectionPropertyName = bo.getDetails().get(
090                        KimConstants.AttributeConstants.COLLECTION_PROPERTY_NAME);
091            }
092
093            String permissionFieldId = null;
094            if (bo.getDetails().containsKey(KimConstants.AttributeConstants.FIELD_ID)) {
095                permissionFieldId = bo.getDetails().get(KimConstants.AttributeConstants.FIELD_ID);
096            }
097
098            String permissionActionEvent = null;
099            if (bo.getDetails().containsKey(KimConstants.AttributeConstants.ACTION_EVENT)) {
100                permissionActionEvent = bo.getDetails().get(KimConstants.AttributeConstants.ACTION_EVENT);
101            }
102
103            boolean groupMatch = false;
104            if ((requestedGroupId != null) && (permissionGroupId != null) && StringUtils.equals(requestedGroupId,
105                    permissionGroupId)) {
106                groupMatch = true;
107            } else if ((requestedCollectionPropertyName != null)
108                    && (permissionCollectionPropertyName != null)
109                    && StringUtils.equals(requestedCollectionPropertyName, permissionCollectionPropertyName)) {
110                groupMatch = true;
111            }
112
113            if (groupMatch) {
114                if ((requestedFieldId != null) && (permissionFieldId != null) && StringUtils.equals(requestedFieldId,
115                        permissionFieldId)) {
116                    matchingPermissions.add(permission);
117                } else if ((requestedActionEvent != null) && (permissionActionEvent != null) && StringUtils.equals(
118                        requestedActionEvent, permissionActionEvent)) {
119                    matchingPermissions.add(permission);
120                }
121            }
122        }
123
124        return super.performPermissionMatches(requestedDetails, matchingPermissions);
125    }
126
127}