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.uif.RemotableAttributeError; |
20 | |
import org.kuali.rice.core.api.util.RiceKeyConstants; |
21 | |
import org.kuali.rice.kim.api.type.KimTypeService; |
22 | |
import org.kuali.rice.kim.bo.ui.KimDocumentRoleMember; |
23 | |
import org.kuali.rice.kim.document.IdentityManagementRoleDocument; |
24 | |
import org.kuali.rice.kim.document.rule.AttributeValidationHelper; |
25 | |
import org.kuali.rice.kim.rule.event.ui.AddMemberEvent; |
26 | |
import org.kuali.rice.kim.rule.ui.AddMemberRule; |
27 | |
import org.kuali.rice.kim.service.KIMServiceLocatorWeb; |
28 | |
import org.kuali.rice.kim.util.KimConstants; |
29 | |
import org.kuali.rice.krad.rules.DocumentRuleBase; |
30 | |
import org.kuali.rice.krad.util.GlobalVariables; |
31 | |
|
32 | |
import java.util.ArrayList; |
33 | |
import java.util.HashMap; |
34 | |
import java.util.List; |
35 | |
import java.util.Map; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 0 | public class KimDocumentMemberRule extends DocumentRuleBase implements AddMemberRule { |
45 | |
|
46 | |
private static final String ERROR_PATH = "member.memberId"; |
47 | |
|
48 | 0 | protected AttributeValidationHelper attributeValidationHelper = new AttributeValidationHelper(); |
49 | |
|
50 | |
public boolean processAddMember(AddMemberEvent addMemberEvent){ |
51 | 0 | KimDocumentRoleMember newMember = addMemberEvent.getMember(); |
52 | 0 | IdentityManagementRoleDocument document = (IdentityManagementRoleDocument)addMemberEvent.getDocument(); |
53 | 0 | boolean rulePassed = true; |
54 | |
|
55 | 0 | if (newMember == null || StringUtils.isBlank(newMember.getMemberId())){ |
56 | 0 | GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_EMPTY_ENTRY, new String[] {"Member"}); |
57 | 0 | return false; |
58 | |
} |
59 | 0 | if(!validAssignRole(newMember, document)) { |
60 | 0 | return false; |
61 | |
} |
62 | 0 | List<RemotableAttributeError> validationErrors = new ArrayList<RemotableAttributeError>(); |
63 | 0 | KimTypeService kimTypeService = KIMServiceLocatorWeb.getKimTypeService(document.getKimType()); |
64 | |
|
65 | 0 | Long newMemberFromTime = newMember.getActiveFromDate() == null ? 0L : newMember.getActiveFromDate().getTime(); |
66 | 0 | Long newMemberToTime = newMember.getActiveToDate() == null ? Long.MAX_VALUE : newMember.getActiveToDate().getTime(); |
67 | |
|
68 | |
List<RemotableAttributeError> errorsAttributesAgainstExisting; |
69 | 0 | int i = 0; |
70 | |
Map<String, String> newMemberQualifiers; |
71 | |
Map<String, String> oldMemberQualifiers; |
72 | 0 | for (KimDocumentRoleMember member: document.getMembers()){ |
73 | 0 | Long memberFromTime = member.getActiveFromDate() == null ? 0L : member.getActiveFromDate().getTime(); |
74 | 0 | Long memberToTime = member.getActiveToDate() == null ? Long.MAX_VALUE : member.getActiveToDate().getTime(); |
75 | 0 | newMemberQualifiers = attributeValidationHelper.convertQualifiersToMap(newMember.getQualifiers()); |
76 | 0 | oldMemberQualifiers = attributeValidationHelper.convertQualifiersToMap(member.getQualifiers()); |
77 | 0 | errorsAttributesAgainstExisting = kimTypeService.validateAttributesAgainstExisting( |
78 | |
document.getKimType().getId(), newMemberQualifiers, oldMemberQualifiers); |
79 | 0 | validationErrors.addAll( |
80 | |
attributeValidationHelper.convertErrorsForMappedFields(ERROR_PATH, errorsAttributesAgainstExisting)); |
81 | 0 | if (!errorsAttributesAgainstExisting.isEmpty() && (member.getMemberId().equals(newMember.getMemberId()) && |
82 | |
member.getMemberTypeCode().equals(newMember.getMemberTypeCode())) |
83 | |
&& ((newMemberFromTime >= memberFromTime && newMemberFromTime < memberToTime) |
84 | |
|| (newMemberToTime >= memberFromTime && newMemberToTime <= memberToTime)) |
85 | |
){ |
86 | 0 | rulePassed = false; |
87 | 0 | GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_DUPLICATE_ENTRY, new String[] {"Member"}); |
88 | 0 | break; |
89 | |
} |
90 | 0 | i++; |
91 | 0 | } |
92 | |
|
93 | 0 | if ( kimTypeService != null && !newMember.isRole()) { |
94 | 0 | List<RemotableAttributeError> localErrors = kimTypeService.validateAttributes( document.getKimType().getId(), attributeValidationHelper.convertQualifiersToMap( newMember.getQualifiers() ) ); |
95 | 0 | validationErrors.addAll( attributeValidationHelper.convertErrors("member", |
96 | |
attributeValidationHelper.convertQualifiersToAttrIdxMap(newMember.getQualifiers()), localErrors) ); |
97 | |
} |
98 | 0 | if (!validationErrors.isEmpty()) { |
99 | 0 | attributeValidationHelper.moveValidationErrorsToErrorMap(validationErrors); |
100 | 0 | rulePassed = false; |
101 | |
} |
102 | |
|
103 | 0 | return rulePassed; |
104 | |
} |
105 | |
|
106 | |
protected boolean validAssignRole(KimDocumentRoleMember roleMember, IdentityManagementRoleDocument document){ |
107 | 0 | boolean rulePassed = true; |
108 | 0 | if(StringUtils.isNotEmpty(document.getRoleNamespace())){ |
109 | 0 | Map<String,String> roleDetails = new HashMap<String,String>(); |
110 | 0 | roleDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, document.getRoleNamespace()); |
111 | 0 | roleDetails.put(KimConstants.AttributeConstants.ROLE_NAME, document.getRoleName()); |
112 | 0 | if (!getDocumentHelperService().getDocumentAuthorizer(document).isAuthorizedByTemplate( |
113 | |
document, |
114 | |
KimConstants.NAMESPACE_CODE, |
115 | |
KimConstants.PermissionTemplateNames.ASSIGN_ROLE, |
116 | |
GlobalVariables.getUserSession().getPerson().getPrincipalId(), |
117 | |
roleDetails, null)){ |
118 | 0 | GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_ASSIGN_ROLE, |
119 | |
new String[] {document.getRoleNamespace(), document.getRoleName()}); |
120 | 0 | rulePassed = false; |
121 | |
} |
122 | |
} |
123 | 0 | return rulePassed; |
124 | |
} |
125 | |
|
126 | |
} |