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