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.RiceKeyConstants; |
19 | |
import org.kuali.rice.kim.api.group.Group; |
20 | |
import org.kuali.rice.kim.api.services.IdentityService; |
21 | |
import org.kuali.rice.kim.api.services.KimApiServiceLocator; |
22 | |
import org.kuali.rice.kim.api.type.KimType; |
23 | |
import org.kuali.rice.kim.api.type.KimTypeService; |
24 | |
import org.kuali.rice.kim.bo.ui.GroupDocumentMember; |
25 | |
import org.kuali.rice.kim.bo.ui.GroupDocumentQualifier; |
26 | |
import org.kuali.rice.kim.document.IdentityManagementGroupDocument; |
27 | |
import org.kuali.rice.kim.rule.event.ui.AddGroupMemberEvent; |
28 | |
import org.kuali.rice.kim.rule.ui.AddGroupMemberRule; |
29 | |
import org.kuali.rice.kim.rules.ui.GroupDocumentMemberRule; |
30 | |
import org.kuali.rice.kim.service.KIMServiceLocatorWeb; |
31 | |
import org.kuali.rice.kim.util.KimConstants; |
32 | |
import org.kuali.rice.krad.document.Document; |
33 | |
import org.kuali.rice.krad.rules.TransactionalDocumentRuleBase; |
34 | |
import org.kuali.rice.krad.service.BusinessObjectService; |
35 | |
import org.kuali.rice.krad.service.KRADServiceLocator; |
36 | |
import org.kuali.rice.krad.util.GlobalVariables; |
37 | |
import org.kuali.rice.krad.util.KRADConstants; |
38 | |
import org.kuali.rice.krad.util.MessageMap; |
39 | |
|
40 | |
import java.sql.Timestamp; |
41 | |
import java.util.HashMap; |
42 | |
import java.util.List; |
43 | |
import java.util.Map; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 0 | public class IdentityManagementGroupDocumentRule extends TransactionalDocumentRuleBase implements AddGroupMemberRule { |
49 | |
|
50 | |
protected AddGroupMemberRule addGroupMemberRule; |
51 | 0 | protected AttributeValidationHelper attributeValidationHelper = new AttributeValidationHelper(); |
52 | |
|
53 | |
protected BusinessObjectService businessObjectService; |
54 | 0 | protected Class<? extends GroupDocumentMemberRule> addGroupMemberRuleClass = GroupDocumentMemberRule.class; |
55 | |
|
56 | |
protected IdentityService identityService; |
57 | |
|
58 | |
public IdentityService getIdentityService() { |
59 | 0 | if ( identityService == null) { |
60 | 0 | identityService = KimApiServiceLocator.getIdentityService(); |
61 | |
} |
62 | 0 | return identityService; |
63 | |
} |
64 | |
|
65 | |
@Override |
66 | |
protected boolean processCustomSaveDocumentBusinessRules(Document document) { |
67 | 0 | if (!(document instanceof IdentityManagementGroupDocument)) { |
68 | 0 | return false; |
69 | |
} |
70 | |
|
71 | 0 | IdentityManagementGroupDocument groupDoc = (IdentityManagementGroupDocument)document; |
72 | |
|
73 | 0 | boolean valid = true; |
74 | 0 | GlobalVariables.getMessageMap().addToErrorPath(KRADConstants.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(KRADConstants.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 | |
} |
111 | |
else{ |
112 | 0 | GlobalVariables.getMessageMap().putError("document.groupName", |
113 | |
RiceKeyConstants.ERROR_DUPLICATE_ENTRY, new String[] {"Group Name"}); |
114 | 0 | rulePassed = false; |
115 | |
} |
116 | |
} |
117 | 0 | return rulePassed; |
118 | |
} |
119 | |
|
120 | |
protected boolean validGroupMemberActiveDates(List<GroupDocumentMember> groupMembers) { |
121 | 0 | boolean valid = true; |
122 | 0 | int i = 0; |
123 | 0 | for(GroupDocumentMember groupMember: groupMembers) { |
124 | 0 | valid &= validateActiveDate("document.members["+i+"].activeToDate", groupMember.getActiveFromDate(), groupMember.getActiveToDate()); |
125 | 0 | i++; |
126 | |
} |
127 | 0 | return valid; |
128 | |
} |
129 | |
|
130 | |
protected boolean validateGroupQualifier(List<GroupDocumentQualifier> groupQualifiers, KimType kimType){ |
131 | 0 | Map<String, String> validationErrors = new HashMap<String, String>(); |
132 | |
|
133 | |
Map<String, String> errorsTemp; |
134 | |
Map<String, String> mapToValidate; |
135 | 0 | KimTypeService kimTypeService = KIMServiceLocatorWeb.getKimTypeService(kimType); |
136 | 0 | GlobalVariables.getMessageMap().removeFromErrorPath(KRADConstants.DOCUMENT_PROPERTY_NAME); |
137 | 0 | mapToValidate = attributeValidationHelper.convertQualifiersToMap(groupQualifiers); |
138 | 0 | errorsTemp = kimTypeService.validateAttributes(kimType.getId(), mapToValidate); |
139 | 0 | validationErrors.putAll( attributeValidationHelper.convertErrors("", |
140 | |
attributeValidationHelper.convertQualifiersToAttrIdxMap(groupQualifiers), errorsTemp)); |
141 | 0 | GlobalVariables.getMessageMap().addToErrorPath(KRADConstants.DOCUMENT_PROPERTY_NAME); |
142 | |
|
143 | 0 | if (validationErrors.isEmpty()) { |
144 | 0 | return true; |
145 | |
} |
146 | 0 | attributeValidationHelper.moveValidationErrorsToErrorMap(validationErrors); |
147 | 0 | return false; |
148 | |
} |
149 | |
|
150 | |
protected boolean validateActiveDate(String errorPath, Timestamp activeFromDate, Timestamp activeToDate) { |
151 | |
|
152 | 0 | boolean valid = true; |
153 | 0 | if (activeFromDate != null && activeToDate !=null && activeToDate.before(activeFromDate)) { |
154 | 0 | MessageMap errorMap = GlobalVariables.getMessageMap(); |
155 | 0 | errorMap.putError(errorPath, RiceKeyConstants.ERROR_ACTIVE_TO_DATE_BEFORE_FROM_DATE); |
156 | 0 | valid = false; |
157 | |
|
158 | |
} |
159 | 0 | return valid; |
160 | |
} |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
public AddGroupMemberRule getAddGroupMemberRule() { |
166 | 0 | if(addGroupMemberRule == null){ |
167 | |
try { |
168 | 0 | addGroupMemberRule = addGroupMemberRuleClass.newInstance(); |
169 | 0 | } catch ( Exception ex ) { |
170 | 0 | throw new RuntimeException( "Unable to create AddMemberRule instance using class: " + addGroupMemberRuleClass, ex ); |
171 | 0 | } |
172 | |
} |
173 | 0 | return addGroupMemberRule; |
174 | |
} |
175 | |
|
176 | |
public boolean processAddGroupMember(AddGroupMemberEvent addGroupMemberEvent) { |
177 | 0 | return new GroupDocumentMemberRule().processAddGroupMember(addGroupMemberEvent); |
178 | |
} |
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
public BusinessObjectService getBusinessObjectService() { |
184 | 0 | if(businessObjectService == null){ |
185 | 0 | businessObjectService = KRADServiceLocator.getBusinessObjectService(); |
186 | |
} |
187 | 0 | return businessObjectService; |
188 | |
} |
189 | |
|
190 | |
} |