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 Widget' KIM type which matches on the id for a UIF view and widget id
031 *
032 * @author Kuali Rice Team (rice.collab@kuali.org)
033 */
034public class ViewWidgetPermissionTypeServiceImpl extends PermissionTypeServiceBase {
035
036    @Override
037    protected List<String> getRequiredAttributes() {
038        List<String> attributes = new ArrayList<String>(super.getRequiredAttributes());
039        attributes.add(KimConstants.AttributeConstants.VIEW_ID);
040        attributes.add(KimConstants.AttributeConstants.WIDGET_ID);
041
042        return Collections.unmodifiableList(attributes);
043    }
044
045    /**
046     * Filters the given permission list to return those that match on widget id, then calls super
047     * 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 requestedWidgetId = requestedDetails.get(KimConstants.AttributeConstants.WIDGET_ID);
058
059        List<Permission> matchingPermissions = new ArrayList<Permission>();
060        for (Permission permission : permissionsList) {
061            PermissionBo bo = PermissionBo.from(permission);
062
063            String permissionWidgetId = bo.getDetails().get(KimConstants.AttributeConstants.WIDGET_ID);
064            if (StringUtils.equals(requestedWidgetId, permissionWidgetId)) {
065                matchingPermissions.add(permission);
066            }
067        }
068
069        return super.performPermissionMatches(requestedDetails, matchingPermissions);
070    }
071
072}