001    /*
002     * Copyright 2011 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.kfs.sec.util;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.kuali.kfs.sec.businessobject.SecurityModelDefinition;
023    import org.kuali.kfs.sec.identity.SecKimAttributes;
024    import org.kuali.kfs.sys.context.SpringContext;
025    import org.kuali.rice.kim.bo.role.dto.RoleMembershipInfo;
026    import org.kuali.rice.kim.bo.types.dto.AttributeSet;
027    import org.kuali.rice.kim.service.RoleManagementService;
028    
029    
030    public class KimUtil {
031    
032        /**
033         * Finds the role membership (if exists) for the given role and member id
034         * 
035         * @param roleId id of role to find member for
036         * @param memberRoleId id of member role
037         * @param membershipQualifications Qualifications to match role membership
038         * @return RoleMembershipInfo containing information on the member record, or null if the member id is not assigned to the role
039         */
040        public static RoleMembershipInfo getRoleMembershipInfoForMemberType(String roleId, String memberRoleId, String memberType, AttributeSet membershipQualifications) {
041            RoleManagementService roleService = SpringContext.getBean(RoleManagementService.class);
042    
043            List<String> roleIds = new ArrayList<String>();
044            roleIds.add(roleId);
045    
046            List<RoleMembershipInfo> roleMembers = roleService.getFirstLevelRoleMembers(roleIds);
047    
048            RoleMembershipInfo modelMembershipInfo = null;
049            for (RoleMembershipInfo roleMembershipInfo : roleMembers) {
050                if (roleMembershipInfo.getMemberTypeCode().equals(memberType) && roleMembershipInfo.getMemberId().equals(memberRoleId)) {
051                    if (membershipQualifications != null) {
052                        boolean qualficationsMatch = doQualficationsMatch(membershipQualifications, roleMembershipInfo.getQualifier());
053    
054                        if (qualficationsMatch) {
055                            modelMembershipInfo = roleMembershipInfo;
056                            break;
057                        }
058                    }
059                    else {
060                        modelMembershipInfo = roleMembershipInfo;
061                        break;
062                    }
063                }
064            }
065    
066            return modelMembershipInfo;
067        }
068    
069        /**
070         * Determines whether an AttributeSet has the same keys and values as another AttributeSet
071         * 
072         * @param qualfiicationToMatch AttributeSet to match keys and values
073         * @param qualfication AttributeSet for matching
074         * @return boolean if second AttributeSet has same keys and values as first
075         */
076        public static boolean doQualficationsMatch(AttributeSet qualfiicationToMatch, AttributeSet qualfication) {
077            boolean qualficationsMatch = true;
078    
079            for (String key : qualfiicationToMatch.keySet()) {
080                String value = qualfiicationToMatch.get(key);
081    
082                if (qualfication.containsKey(key)) {
083                    String matchValue = qualfication.get(key);
084    
085                    if (!StringUtils.equals(value, matchValue)) {
086                        qualficationsMatch = false;
087                    }
088                }
089                else {
090                    qualficationsMatch = false;
091                }
092            }
093    
094            return qualficationsMatch;
095        }
096    
097        /**
098         * Determines whether each of the qualifying values in the given qualification (AttributeSet) match the given corresponding values
099         * 
100         * @param membershipQualifications AttributeSet containing qualifying values to check
101         * @param constraintCode constraint code value to match
102         * @param operator operator value to match
103         * @param attributeValue attribute value to match
104         * @return boolean true if all qualifying values match the given values, false if at least one qualifying value does not match
105         */
106        public static boolean doMembershipQualificationsMatchValues(AttributeSet membershipQualifications, String constraintCode, String operator, String attributeValue) {
107            String constraintQualifyValue = membershipQualifications.get(SecKimAttributes.CONSTRAINT_CODE);
108            String operatorQualifyValue = membershipQualifications.get(SecKimAttributes.OPERATOR);
109            String propertyValueQualifyValue = membershipQualifications.get(SecKimAttributes.PROPERTY_VALUE);
110    
111            if (!StringUtils.equals(constraintQualifyValue, constraintCode)) {
112                return false;
113            }
114    
115            if (!StringUtils.equals(operatorQualifyValue, operator)) {
116                return false;
117            }
118    
119            if (!StringUtils.equals(propertyValueQualifyValue, attributeValue)) {
120                return false;
121            }
122    
123            return true;
124        }
125    
126    }