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;
022
023import java.util.ArrayList;
024import java.util.Collections;
025import java.util.List;
026import java.util.Map;
027
028/**
029 * Type service for the 'View Widget' KIM type which matches on the id for a UIF view and widget id
030 *
031 * @author Kuali Rice Team (rice.collab@kuali.org)
032 */
033public class ViewWidgetPermissionTypeServiceImpl extends ViewPermissionTypeServiceImpl {
034
035    @Override
036    protected List<String> getRequiredAttributes() {
037        List<String> attributes = new ArrayList<String>(super.getRequiredAttributes());
038        attributes.add(KimConstants.AttributeConstants.VIEW_ID);
039        attributes.add(KimConstants.AttributeConstants.WIDGET_ID);
040
041        return Collections.unmodifiableList(attributes);
042    }
043
044    /**
045     * Filters the given permission list to return those that match on widget id, then calls super
046     * to filter based on view id
047     *
048     * @param requestedDetails - map of details requested with permission (used for matching)
049     * @param permissionsList - list of permissions to process for matches
050     * @return List<Permission> list of permissions that match the requested details
051     */
052    @Override
053    protected List<Permission> performPermissionMatches(Map<String, String> requestedDetails,
054            List<Permission> permissionsList) {
055
056        String requestedWidgetId = requestedDetails.get(KimConstants.AttributeConstants.WIDGET_ID);
057
058        List<Permission> matchingPermissions = new ArrayList<Permission>();
059        for (Permission permission : permissionsList) {
060            PermissionBo bo = PermissionBo.from(permission);
061
062            String permissionWidgetId = bo.getDetails().get(KimConstants.AttributeConstants.WIDGET_ID);
063            if (StringUtils.equals(requestedWidgetId, permissionWidgetId)) {
064                matchingPermissions.add(permission);
065            }
066        }
067
068        return super.performPermissionMatches(requestedDetails, matchingPermissions);
069    }
070
071}