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