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 Line Field' KIM type which matches on the id for a UIF view, group id or collection
030     * property name, field id or property name
031     *
032     * @author Kuali Rice Team (rice.collab@kuali.org)
033     */
034    public class ViewLineFieldPermissionTypeServiceImpl extends ViewPermissionTypeServiceImpl {
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    
041            return Collections.unmodifiableList(attributes);
042        }
043    
044        /**
045         * Filters the given permission list to return those that match on group id or collection property name, field id
046         * or property name, then calls super 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 requestedGroupId = null;
057            if (requestedDetails.containsKey(KimConstants.AttributeConstants.GROUP_ID)) {
058                requestedGroupId = requestedDetails.get(KimConstants.AttributeConstants.GROUP_ID);
059            }
060    
061            String requestedCollectionPropertyName = null;
062            if (requestedDetails.containsKey(KimConstants.AttributeConstants.COLLECTION_PROPERTY_NAME)) {
063                requestedCollectionPropertyName = requestedDetails.get(
064                        KimConstants.AttributeConstants.COLLECTION_PROPERTY_NAME);
065            }
066    
067            String requestedFieldId = null;
068            if (requestedDetails.containsKey(KimConstants.AttributeConstants.FIELD_ID)) {
069                requestedFieldId = requestedDetails.get(KimConstants.AttributeConstants.FIELD_ID);
070            }
071    
072            String requestedPropertyName = null;
073            if (requestedDetails.containsKey(KimConstants.AttributeConstants.PROPERTY_NAME)) {
074                requestedPropertyName = requestedDetails.get(KimConstants.AttributeConstants.PROPERTY_NAME);
075            }
076    
077            List<Permission> matchingPermissions = new ArrayList<Permission>();
078            for (Permission permission : permissionsList) {
079                PermissionBo bo = PermissionBo.from(permission);
080    
081                String permissionGroupId = null;
082                if (bo.getDetails().containsKey(KimConstants.AttributeConstants.GROUP_ID)) {
083                    permissionGroupId = bo.getDetails().get(KimConstants.AttributeConstants.GROUP_ID);
084                }
085    
086                String permissionCollectionPropertyName = null;
087                if (bo.getDetails().containsKey(KimConstants.AttributeConstants.COLLECTION_PROPERTY_NAME)) {
088                    permissionCollectionPropertyName = bo.getDetails().get(
089                            KimConstants.AttributeConstants.COLLECTION_PROPERTY_NAME);
090                }
091    
092                String permissionFieldId = null;
093                if (bo.getDetails().containsKey(KimConstants.AttributeConstants.FIELD_ID)) {
094                    permissionFieldId = bo.getDetails().get(KimConstants.AttributeConstants.FIELD_ID);
095                }
096    
097                String permissionPropertyName = null;
098                if (bo.getDetails().containsKey(KimConstants.AttributeConstants.PROPERTY_NAME)) {
099                    permissionPropertyName = bo.getDetails().get(KimConstants.AttributeConstants.PROPERTY_NAME);
100                }
101    
102                boolean groupMatch = false;
103                if ((requestedGroupId != null) && (permissionGroupId != null) && StringUtils.equals(requestedGroupId,
104                        permissionGroupId)) {
105                    groupMatch = true;
106                } else if ((requestedCollectionPropertyName != null)
107                        && (permissionCollectionPropertyName != null)
108                        && StringUtils.equals(requestedCollectionPropertyName, permissionCollectionPropertyName)) {
109                    groupMatch = true;
110                }
111    
112                if (groupMatch) {
113                    if ((requestedFieldId != null) && (permissionFieldId != null) && StringUtils.equals(requestedFieldId,
114                            permissionFieldId)) {
115                        matchingPermissions.add(permission);
116                    } else if ((requestedPropertyName != null) && (permissionPropertyName != null) && StringUtils.equals(
117                            requestedPropertyName, permissionPropertyName)) {
118                        matchingPermissions.add(permission);
119                    }
120                }
121            }
122    
123            return super.performPermissionMatches(requestedDetails, matchingPermissions);
124        }
125    
126    }