001 /** 002 * Copyright 2005-2012 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.kim.rules.ui; 017 018 import org.apache.commons.lang.StringUtils; 019 import org.kuali.rice.core.api.uif.RemotableAttributeError; 020 import org.kuali.rice.core.api.util.RiceKeyConstants; 021 import org.kuali.rice.kim.api.KimConstants; 022 import org.kuali.rice.kim.bo.ui.KimDocumentRoleMember; 023 import org.kuali.rice.kim.document.IdentityManagementRoleDocument; 024 import org.kuali.rice.kim.document.rule.AttributeValidationHelper; 025 import org.kuali.rice.kim.framework.services.KimFrameworkServiceLocator; 026 import org.kuali.rice.kim.framework.type.KimTypeService; 027 import org.kuali.rice.kim.rule.event.ui.AddMemberEvent; 028 import org.kuali.rice.kim.rule.ui.AddMemberRule; 029 import org.kuali.rice.krad.rules.DocumentRuleBase; 030 import org.kuali.rice.krad.util.GlobalVariables; 031 032 import java.util.ArrayList; 033 import java.util.HashMap; 034 import java.util.List; 035 import java.util.Map; 036 037 038 /** 039 * This is a description of what this class does - shyu don't forget to fill this in. 040 * 041 * @author Kuali Rice Team (rice.collab@kuali.org) 042 * 043 */ 044 public class KimDocumentMemberRule extends DocumentRuleBase implements AddMemberRule { 045 046 private static final String ERROR_PATH = "member.memberId"; 047 048 protected AttributeValidationHelper attributeValidationHelper = new AttributeValidationHelper(); 049 050 public boolean processAddMember(AddMemberEvent addMemberEvent){ 051 KimDocumentRoleMember newMember = addMemberEvent.getMember(); 052 IdentityManagementRoleDocument document = (IdentityManagementRoleDocument)addMemberEvent.getDocument(); 053 boolean rulePassed = true; 054 055 if (newMember == null || StringUtils.isBlank(newMember.getMemberId())){ 056 GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_EMPTY_ENTRY, new String[] {"Member"}); 057 return false; 058 } 059 if(!validAssignRole(newMember, document)) { 060 return false; 061 } 062 List<RemotableAttributeError> validationErrors = new ArrayList<RemotableAttributeError>(); 063 KimTypeService kimTypeService = KimFrameworkServiceLocator.getKimTypeService(document.getKimType()); 064 065 Long newMemberFromTime = newMember.getActiveFromDate() == null ? 0L : newMember.getActiveFromDate().getTime(); 066 Long newMemberToTime = newMember.getActiveToDate() == null ? Long.MAX_VALUE : newMember.getActiveToDate().getTime(); 067 068 List<RemotableAttributeError> errorsAttributesAgainstExisting; 069 int i = 0; 070 Map<String, String> newMemberQualifiers; 071 Map<String, String> oldMemberQualifiers; 072 for (KimDocumentRoleMember member: document.getMembers()){ 073 Long memberFromTime = member.getActiveFromDate() == null ? 0L : member.getActiveFromDate().getTime(); 074 Long memberToTime = member.getActiveToDate() == null ? Long.MAX_VALUE : member.getActiveToDate().getTime(); 075 newMemberQualifiers = attributeValidationHelper.convertQualifiersToMap(newMember.getQualifiers()); 076 oldMemberQualifiers = attributeValidationHelper.convertQualifiersToMap(member.getQualifiers()); 077 errorsAttributesAgainstExisting = kimTypeService.validateAttributesAgainstExisting( 078 document.getKimType().getId(), newMemberQualifiers, oldMemberQualifiers); 079 validationErrors.addAll( 080 attributeValidationHelper.convertErrorsForMappedFields(ERROR_PATH, errorsAttributesAgainstExisting)); 081 if (!errorsAttributesAgainstExisting.isEmpty() && (member.getMemberId().equals(newMember.getMemberId()) && 082 member.getMemberTypeCode().equals(newMember.getMemberTypeCode())) 083 && ((newMemberFromTime >= memberFromTime && newMemberFromTime < memberToTime) 084 || (newMemberToTime >= memberFromTime && newMemberToTime <= memberToTime)) 085 ){ 086 rulePassed = false; 087 GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_DUPLICATE_ENTRY, new String[] {"Member"}); 088 break; 089 } 090 i++; 091 } 092 093 if ( kimTypeService != null && !newMember.isRole()) { 094 List<RemotableAttributeError> localErrors = kimTypeService.validateAttributes( document.getKimType().getId(), attributeValidationHelper.convertQualifiersToMap( newMember.getQualifiers() ) ); 095 validationErrors.addAll( attributeValidationHelper.convertErrors("member", 096 attributeValidationHelper.convertQualifiersToAttrIdxMap(newMember.getQualifiers()), localErrors) ); 097 } 098 if (!validationErrors.isEmpty()) { 099 attributeValidationHelper.moveValidationErrorsToErrorMap(validationErrors); 100 rulePassed = false; 101 } 102 103 return rulePassed; 104 } 105 106 protected boolean validAssignRole(KimDocumentRoleMember roleMember, IdentityManagementRoleDocument document){ 107 boolean rulePassed = true; 108 if(StringUtils.isNotEmpty(document.getRoleNamespace())){ 109 Map<String,String> roleDetails = new HashMap<String,String>(); 110 roleDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, document.getRoleNamespace()); 111 roleDetails.put(KimConstants.AttributeConstants.ROLE_NAME, document.getRoleName()); 112 if (!getDocumentDictionaryService().getDocumentAuthorizer(document).isAuthorizedByTemplate( 113 document, 114 KimConstants.NAMESPACE_CODE, 115 KimConstants.PermissionTemplateNames.ASSIGN_ROLE, 116 GlobalVariables.getUserSession().getPerson().getPrincipalId(), 117 roleDetails, null)){ 118 GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_ASSIGN_ROLE, 119 new String[] {document.getRoleNamespace(), document.getRoleName()}); 120 rulePassed = false; 121 } 122 } 123 return rulePassed; 124 } 125 126 }