1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kim.rules.ui;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.CoreConstants;
20 import org.kuali.rice.core.api.membership.MemberType;
21 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
22 import org.kuali.rice.core.api.uif.RemotableAttributeError;
23 import org.kuali.rice.core.api.util.RiceKeyConstants;
24 import org.kuali.rice.kim.api.KimConstants;
25 import org.kuali.rice.kim.api.type.KimType;
26 import org.kuali.rice.kim.bo.ui.KimDocumentRoleMember;
27 import org.kuali.rice.kim.document.IdentityManagementRoleDocument;
28 import org.kuali.rice.kim.document.rule.AttributeValidationHelper;
29 import org.kuali.rice.kim.framework.role.RoleTypeService;
30 import org.kuali.rice.kim.framework.services.KimFrameworkServiceLocator;
31 import org.kuali.rice.kim.framework.type.KimTypeService;
32 import org.kuali.rice.kim.impl.services.KimImplServiceLocator;
33 import org.kuali.rice.kim.rule.event.ui.AddMemberEvent;
34 import org.kuali.rice.kim.rule.ui.AddMemberRule;
35 import org.kuali.rice.kns.rules.DocumentRuleBase;
36 import org.kuali.rice.krad.service.KRADServiceLocator;
37 import org.kuali.rice.krad.service.impl.KRADModuleService;
38 import org.kuali.rice.core.api.util.VersionHelper;
39 import org.kuali.rice.krad.util.GlobalVariables;
40 import org.kuali.rice.krad.util.ObjectUtils;
41 import org.kuali.rice.ksb.api.KsbApiServiceLocator;
42 import org.kuali.rice.ksb.api.bus.Endpoint;
43 import org.kuali.rice.ksb.api.bus.ServiceBus;
44
45 import javax.xml.namespace.QName;
46 import java.util.ArrayList;
47 import java.util.HashMap;
48 import java.util.List;
49 import java.util.Map;
50
51
52
53
54
55
56
57
58 public class KimDocumentMemberRule extends DocumentRuleBase implements AddMemberRule {
59
60 private static final String ERROR_PATH = "member.memberId";
61
62 protected AttributeValidationHelper attributeValidationHelper = new AttributeValidationHelper();
63
64 public boolean processAddMember(AddMemberEvent addMemberEvent){
65 KimDocumentRoleMember newMember = addMemberEvent.getMember();
66 IdentityManagementRoleDocument document = (IdentityManagementRoleDocument)addMemberEvent.getDocument();
67 boolean rulePassed = true;
68
69 if (newMember == null || StringUtils.isBlank(newMember.getMemberId())){
70 GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_EMPTY_ENTRY, new String[] {"Member"});
71 return false;
72 }
73 if(!validAssignRole(newMember, document)) {
74 return false;
75 }
76 List<RemotableAttributeError> validationErrors = new ArrayList<RemotableAttributeError>();
77 KimTypeService kimTypeService = KimFrameworkServiceLocator.getKimTypeService(document.getKimType());
78
79 Long newMemberFromTime = newMember.getActiveFromDate() == null ? 0L : newMember.getActiveFromDate().getTime();
80 Long newMemberToTime = newMember.getActiveToDate() == null ? Long.MAX_VALUE : newMember.getActiveToDate().getTime();
81
82 List<RemotableAttributeError> errorsAttributesAgainstExisting = new ArrayList<RemotableAttributeError>();
83 Map<String, String> newMemberQualifiers = attributeValidationHelper.convertQualifiersToMap(newMember.getQualifiers());
84
85 Map<String, String> oldMemberQualifiers;
86 for (KimDocumentRoleMember member: document.getMembers()){
87 Long memberFromTime = member.getActiveFromDate() == null ? 0L : member.getActiveFromDate().getTime();
88 Long memberToTime = member.getActiveToDate() == null ? Long.MAX_VALUE : member.getActiveToDate().getTime();
89 oldMemberQualifiers = attributeValidationHelper.convertQualifiersToMap(member.getQualifiers());
90
91 if ((member.getMemberId().equals(newMember.getMemberId()) &&
92 member.getMemberTypeCode().equals(newMember.getMemberTypeCode()))
93 && ((newMemberFromTime >= memberFromTime && newMemberFromTime < memberToTime)
94 || (newMemberToTime >= memberFromTime && newMemberToTime <= memberToTime))) {
95
96 errorsAttributesAgainstExisting = kimTypeService.validateAttributesAgainstExisting(
97 document.getKimType().getId(), newMemberQualifiers, oldMemberQualifiers);
98 validationErrors.addAll(
99 attributeValidationHelper.convertErrorsForMappedFields(ERROR_PATH, errorsAttributesAgainstExisting));
100 if (!errorsAttributesAgainstExisting.isEmpty()) {
101 rulePassed = false;
102 GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_DUPLICATE_ENTRY, new String[] {"Member"});
103 break;
104 }
105 }
106 }
107
108 boolean shouldNotValidate = newMember.isRole();
109 if ( kimTypeService != null && ObjectUtils.isNotNull( document.getKimType() ) && StringUtils.isNotBlank(document.getKimType().getServiceName()) ) {
110 ServiceBus serviceBus = KsbApiServiceLocator.getServiceBus();
111 Endpoint endpoint = serviceBus.getEndpoint(QName.valueOf(document.getKimType().getServiceName()));
112 if ( endpoint != null ) {
113 boolean versionOk = true;
114 String endpointVersion = endpoint.getServiceConfiguration().getServiceVersion();
115 if ( StringUtils.isNotBlank(endpointVersion) ) {
116 versionOk = VersionHelper.compareVersion(endpointVersion, CoreConstants.Versions.VERSION_2_1_2) != -1;
117 }
118 if(versionOk) {
119 shouldNotValidate = ((RoleTypeService)kimTypeService).shouldValidateQualifiersForMemberType( MemberType.fromCode(newMember.getMemberTypeCode()));
120 }
121 }
122 }
123 if (kimTypeService !=null && !shouldNotValidate) {
124 List<RemotableAttributeError> localErrors = kimTypeService.validateAttributes( document.getKimType().getId(), attributeValidationHelper.convertQualifiersToMap( newMember.getQualifiers() ) );
125 validationErrors.addAll( attributeValidationHelper.convertErrors("member",
126 attributeValidationHelper.convertQualifiersToAttrIdxMap(newMember.getQualifiers()), localErrors) );
127 }
128 if (!validationErrors.isEmpty()) {
129 attributeValidationHelper.moveValidationErrorsToErrorMap(validationErrors);
130 rulePassed = false;
131 }
132
133 return rulePassed;
134 }
135
136 protected boolean validAssignRole(KimDocumentRoleMember roleMember, IdentityManagementRoleDocument document){
137 boolean rulePassed = true;
138 if(StringUtils.isNotEmpty(document.getRoleNamespace())){
139 Map<String,String> roleDetails = new HashMap<String,String>();
140 roleDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, document.getRoleNamespace());
141 roleDetails.put(KimConstants.AttributeConstants.ROLE_NAME, document.getRoleName());
142 if (!getDocumentDictionaryService().getDocumentAuthorizer(document).isAuthorizedByTemplate(
143 document,
144 KimConstants.NAMESPACE_CODE,
145 KimConstants.PermissionTemplateNames.ASSIGN_ROLE,
146 GlobalVariables.getUserSession().getPerson().getPrincipalId(),
147 roleDetails, null)){
148 GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_ASSIGN_ROLE,
149 new String[] {document.getRoleNamespace(), document.getRoleName()});
150 rulePassed = false;
151 }
152 }
153 return rulePassed;
154 }
155
156 }