| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.kim.document.rule; |
| 17 | |
|
| 18 | |
import org.kuali.rice.core.util.AttributeSet; |
| 19 | |
import org.kuali.rice.core.util.RiceKeyConstants; |
| 20 | |
import org.kuali.rice.kim.api.group.Group; |
| 21 | |
import org.kuali.rice.kim.api.services.KimApiServiceLocator; |
| 22 | |
import org.kuali.rice.kim.api.type.KimType; |
| 23 | |
import org.kuali.rice.kim.bo.ui.GroupDocumentMember; |
| 24 | |
import org.kuali.rice.kim.bo.ui.GroupDocumentQualifier; |
| 25 | |
import org.kuali.rice.kim.document.IdentityManagementGroupDocument; |
| 26 | |
import org.kuali.rice.kim.rule.event.ui.AddGroupMemberEvent; |
| 27 | |
import org.kuali.rice.kim.rule.ui.AddGroupMemberRule; |
| 28 | |
import org.kuali.rice.kim.rules.ui.GroupDocumentMemberRule; |
| 29 | |
import org.kuali.rice.kim.api.entity.services.IdentityService; |
| 30 | |
import org.kuali.rice.kim.service.KIMServiceLocatorWeb; |
| 31 | |
import org.kuali.rice.kim.service.support.KimTypeService; |
| 32 | |
import org.kuali.rice.kim.util.KimConstants; |
| 33 | |
import org.kuali.rice.kns.document.Document; |
| 34 | |
import org.kuali.rice.kns.rules.TransactionalDocumentRuleBase; |
| 35 | |
import org.kuali.rice.kns.service.BusinessObjectService; |
| 36 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
| 37 | |
import org.kuali.rice.kns.util.GlobalVariables; |
| 38 | |
import org.kuali.rice.kns.util.KNSConstants; |
| 39 | |
import org.kuali.rice.kns.util.MessageMap; |
| 40 | |
|
| 41 | |
import java.sql.Timestamp; |
| 42 | |
import java.util.HashMap; |
| 43 | |
import java.util.List; |
| 44 | |
import java.util.Map; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | 0 | public class IdentityManagementGroupDocumentRule extends TransactionalDocumentRuleBase implements AddGroupMemberRule { |
| 50 | |
|
| 51 | |
protected AddGroupMemberRule addGroupMemberRule; |
| 52 | 0 | protected AttributeValidationHelper attributeValidationHelper = new AttributeValidationHelper(); |
| 53 | |
|
| 54 | |
protected BusinessObjectService businessObjectService; |
| 55 | 0 | protected Class<? extends GroupDocumentMemberRule> addGroupMemberRuleClass = GroupDocumentMemberRule.class; |
| 56 | |
|
| 57 | |
protected IdentityService identityService; |
| 58 | |
|
| 59 | |
public IdentityService getIdentityService() { |
| 60 | 0 | if ( identityService == null) { |
| 61 | 0 | identityService = KimApiServiceLocator.getIdentityService(); |
| 62 | |
} |
| 63 | 0 | return identityService; |
| 64 | |
} |
| 65 | |
|
| 66 | |
@Override |
| 67 | |
protected boolean processCustomSaveDocumentBusinessRules(Document document) { |
| 68 | 0 | if (!(document instanceof IdentityManagementGroupDocument)) |
| 69 | 0 | return false; |
| 70 | |
|
| 71 | 0 | IdentityManagementGroupDocument groupDoc = (IdentityManagementGroupDocument)document; |
| 72 | |
|
| 73 | 0 | boolean valid = true; |
| 74 | 0 | GlobalVariables.getMessageMap().addToErrorPath(KNSConstants.DOCUMENT_PROPERTY_NAME); |
| 75 | 0 | valid &= validAssignGroup(groupDoc); |
| 76 | 0 | valid &= validDuplicateGroupName(groupDoc); |
| 77 | 0 | getDictionaryValidationService().validateDocumentAndUpdatableReferencesRecursively(document, getMaxDictionaryValidationDepth(), true, false); |
| 78 | 0 | valid &= validateGroupQualifier(groupDoc.getQualifiers(), groupDoc.getKimType()); |
| 79 | 0 | valid &= validGroupMemberActiveDates(groupDoc.getMembers()); |
| 80 | 0 | GlobalVariables.getMessageMap().removeFromErrorPath(KNSConstants.DOCUMENT_PROPERTY_NAME); |
| 81 | |
|
| 82 | 0 | return valid; |
| 83 | |
} |
| 84 | |
|
| 85 | |
protected boolean validAssignGroup(IdentityManagementGroupDocument document){ |
| 86 | 0 | boolean rulePassed = true; |
| 87 | 0 | Map<String,String> additionalPermissionDetails = new HashMap<String,String>(); |
| 88 | 0 | additionalPermissionDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, document.getGroupNamespace()); |
| 89 | 0 | additionalPermissionDetails.put(KimConstants.AttributeConstants.GROUP_NAME, document.getGroupName()); |
| 90 | 0 | if(document.getMembers()!=null && document.getMembers().size()>0){ |
| 91 | 0 | if(!getDocumentHelperService().getDocumentAuthorizer(document).isAuthorizedByTemplate( |
| 92 | |
document, KimConstants.NAMESPACE_CODE, KimConstants.PermissionTemplateNames.POPULATE_GROUP, |
| 93 | |
GlobalVariables.getUserSession().getPrincipalId(), additionalPermissionDetails, null)){ |
| 94 | 0 | GlobalVariables.getMessageMap().putError("document.groupName", |
| 95 | |
RiceKeyConstants.ERROR_ASSIGN_GROUP, |
| 96 | |
new String[] {document.getGroupNamespace(), document.getGroupName()}); |
| 97 | 0 | rulePassed = false; |
| 98 | |
} |
| 99 | |
} |
| 100 | 0 | return rulePassed; |
| 101 | |
} |
| 102 | |
|
| 103 | |
@SuppressWarnings("unchecked") |
| 104 | |
protected boolean validDuplicateGroupName(IdentityManagementGroupDocument groupDoc){ |
| 105 | 0 | Group group = KimApiServiceLocator.getGroupService().getGroupByName(groupDoc.getGroupNamespace(), groupDoc.getGroupName()); |
| 106 | 0 | boolean rulePassed = true; |
| 107 | 0 | if(group!=null){ |
| 108 | 0 | if(group.getId().equals(groupDoc.getGroupId())) |
| 109 | 0 | rulePassed = true; |
| 110 | |
else{ |
| 111 | 0 | GlobalVariables.getMessageMap().putError("document.groupName", |
| 112 | |
RiceKeyConstants.ERROR_DUPLICATE_ENTRY, new String[] {"Group Name"}); |
| 113 | 0 | rulePassed = false; |
| 114 | |
} |
| 115 | |
} |
| 116 | 0 | return rulePassed; |
| 117 | |
} |
| 118 | |
|
| 119 | |
protected boolean validGroupMemberActiveDates(List<GroupDocumentMember> groupMembers) { |
| 120 | 0 | boolean valid = true; |
| 121 | 0 | int i = 0; |
| 122 | 0 | for(GroupDocumentMember groupMember: groupMembers) { |
| 123 | 0 | valid &= validateActiveDate("document.members["+i+"].activeToDate", groupMember.getActiveFromDate(), groupMember.getActiveToDate()); |
| 124 | 0 | i++; |
| 125 | |
} |
| 126 | 0 | return valid; |
| 127 | |
} |
| 128 | |
|
| 129 | |
protected boolean validateGroupQualifier(List<GroupDocumentQualifier> groupQualifiers, KimType kimType){ |
| 130 | 0 | AttributeSet validationErrors = new AttributeSet(); |
| 131 | |
|
| 132 | |
AttributeSet errorsTemp; |
| 133 | |
AttributeSet attributeSetToValidate; |
| 134 | 0 | KimTypeService kimTypeService = KIMServiceLocatorWeb.getKimTypeService(kimType); |
| 135 | 0 | GlobalVariables.getMessageMap().removeFromErrorPath(KNSConstants.DOCUMENT_PROPERTY_NAME); |
| 136 | 0 | attributeSetToValidate = attributeValidationHelper.convertQualifiersToMap(groupQualifiers); |
| 137 | 0 | errorsTemp = kimTypeService.validateAttributes(kimType.getId(), attributeSetToValidate); |
| 138 | 0 | validationErrors.putAll( attributeValidationHelper.convertErrors("",attributeValidationHelper.convertQualifiersToAttrIdxMap(groupQualifiers),errorsTemp) ); |
| 139 | 0 | GlobalVariables.getMessageMap().addToErrorPath(KNSConstants.DOCUMENT_PROPERTY_NAME); |
| 140 | |
|
| 141 | 0 | if (validationErrors.isEmpty()) { |
| 142 | 0 | return true; |
| 143 | |
} |
| 144 | 0 | attributeValidationHelper.moveValidationErrorsToErrorMap(validationErrors); |
| 145 | 0 | return false; |
| 146 | |
} |
| 147 | |
|
| 148 | |
protected boolean validateActiveDate(String errorPath, Timestamp activeFromDate, Timestamp activeToDate) { |
| 149 | |
|
| 150 | 0 | boolean valid = true; |
| 151 | 0 | if (activeFromDate != null && activeToDate !=null && activeToDate.before(activeFromDate)) { |
| 152 | 0 | MessageMap errorMap = GlobalVariables.getMessageMap(); |
| 153 | 0 | errorMap.putError(errorPath, RiceKeyConstants.ERROR_ACTIVE_TO_DATE_BEFORE_FROM_DATE); |
| 154 | 0 | valid = false; |
| 155 | |
|
| 156 | |
} |
| 157 | 0 | return valid; |
| 158 | |
} |
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
public AddGroupMemberRule getAddGroupMemberRule() { |
| 164 | 0 | if(addGroupMemberRule == null){ |
| 165 | |
try { |
| 166 | 0 | addGroupMemberRule = addGroupMemberRuleClass.newInstance(); |
| 167 | 0 | } catch ( Exception ex ) { |
| 168 | 0 | throw new RuntimeException( "Unable to create AddMemberRule instance using class: " + addGroupMemberRuleClass, ex ); |
| 169 | 0 | } |
| 170 | |
} |
| 171 | 0 | return addGroupMemberRule; |
| 172 | |
} |
| 173 | |
|
| 174 | |
public boolean processAddGroupMember(AddGroupMemberEvent addGroupMemberEvent) { |
| 175 | 0 | return new GroupDocumentMemberRule().processAddGroupMember(addGroupMemberEvent); |
| 176 | |
} |
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
public BusinessObjectService getBusinessObjectService() { |
| 182 | 0 | if(businessObjectService == null){ |
| 183 | 0 | businessObjectService = KNSServiceLocator.getBusinessObjectService(); |
| 184 | |
} |
| 185 | 0 | return businessObjectService; |
| 186 | |
} |
| 187 | |
|
| 188 | |
} |