001    /**
002     * Copyright 2005-2013 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.rice.krad.kim;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.kim.api.KimConstants;
020    import org.kuali.rice.kim.api.permission.Permission;
021    import org.kuali.rice.kim.impl.permission.PermissionBo;
022    
023    import java.util.ArrayList;
024    import java.util.Collections;
025    import java.util.List;
026    import java.util.Map;
027    
028    /**
029     * Type service for the 'View Field' KIM type which matches on the id for a UIF view, field id or property name
030     *
031     * @author Kuali Rice Team (rice.collab@kuali.org)
032     */
033    public class ViewFieldPermissionTypeServiceImpl 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    
040            return Collections.unmodifiableList(attributes);
041        }
042    
043        /**
044         * Filters the given permission list to return those that match on field id or property name, then calls super
045         * to filter based on view id
046         *
047         * @param requestedDetails - map of details requested with permission (used for matching)
048         * @param permissionsList - list of permissions to process for matches
049         * @return List<Permission> list of permissions that match the requested details
050         */
051        @Override
052        protected List<Permission> performPermissionMatches(Map<String, String> requestedDetails,
053                List<Permission> permissionsList) {
054    
055            String requestedFieldId = null;
056            if (requestedDetails.containsKey(KimConstants.AttributeConstants.FIELD_ID)) {
057                requestedFieldId = requestedDetails.get(KimConstants.AttributeConstants.FIELD_ID);
058            }
059    
060            String requestedPropertyName = null;
061            if (requestedDetails.containsKey(KimConstants.AttributeConstants.PROPERTY_NAME)) {
062                requestedPropertyName = requestedDetails.get(KimConstants.AttributeConstants.PROPERTY_NAME);
063            }
064    
065            List<Permission> matchingPermissions = new ArrayList<Permission>();
066            for (Permission permission : permissionsList) {
067                PermissionBo bo = PermissionBo.from(permission);
068    
069                String permissionFieldId = null;
070                if (bo.getDetails().containsKey(KimConstants.AttributeConstants.FIELD_ID)) {
071                    permissionFieldId = bo.getDetails().get(KimConstants.AttributeConstants.FIELD_ID);
072                }
073    
074                String permissionPropertyName = null;
075                if (bo.getDetails().containsKey(KimConstants.AttributeConstants.PROPERTY_NAME)) {
076                    permissionPropertyName = bo.getDetails().get(KimConstants.AttributeConstants.PROPERTY_NAME);
077                }
078    
079                if ((requestedFieldId != null) && (permissionFieldId != null) && StringUtils.equals(requestedFieldId,
080                        permissionFieldId)) {
081                    matchingPermissions.add(permission);
082                } else if ((requestedPropertyName != null) && (permissionPropertyName != null) && StringUtils.equals(
083                        requestedPropertyName, permissionPropertyName)) {
084                    matchingPermissions.add(permission);
085                }
086            }
087    
088            return super.performPermissionMatches(requestedDetails, matchingPermissions);
089        }
090    
091    }