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 java.sql.Timestamp; |
19 | |
import java.util.HashMap; |
20 | |
import java.util.List; |
21 | |
import java.util.Map; |
22 | |
|
23 | |
import org.kuali.rice.kim.bo.impl.GroupImpl; |
24 | |
import org.kuali.rice.kim.bo.impl.KimAttributes; |
25 | |
import org.kuali.rice.kim.bo.types.dto.AttributeSet; |
26 | |
import org.kuali.rice.kim.bo.types.dto.KimTypeInfo; |
27 | |
import org.kuali.rice.kim.bo.ui.GroupDocumentMember; |
28 | |
import org.kuali.rice.kim.bo.ui.GroupDocumentQualifier; |
29 | |
import org.kuali.rice.kim.document.IdentityManagementGroupDocument; |
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.kim.service.IdentityService; |
34 | |
import org.kuali.rice.kim.service.KIMServiceLocator; |
35 | |
import org.kuali.rice.kim.service.support.KimTypeService; |
36 | |
import org.kuali.rice.kim.util.KimCommonUtils; |
37 | |
import org.kuali.rice.kim.util.KimConstants; |
38 | |
import org.kuali.rice.kns.document.Document; |
39 | |
import org.kuali.rice.kns.rules.TransactionalDocumentRuleBase; |
40 | |
import org.kuali.rice.kns.service.BusinessObjectService; |
41 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
42 | |
import org.kuali.rice.kns.util.GlobalVariables; |
43 | |
import org.kuali.rice.kns.util.KNSConstants; |
44 | |
import org.kuali.rice.kns.util.MessageMap; |
45 | |
import org.kuali.rice.kns.util.RiceKeyConstants; |
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 = KIMServiceLocator.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 | 0 | IdentityManagementGroupDocument groupDoc = (IdentityManagementGroupDocument)document; |
73 | |
|
74 | 0 | boolean valid = true; |
75 | 0 | GlobalVariables.getMessageMap().addToErrorPath(KNSConstants.DOCUMENT_PROPERTY_NAME); |
76 | 0 | valid &= validAssignGroup(groupDoc); |
77 | 0 | valid &= validDuplicateGroupName(groupDoc); |
78 | 0 | getDictionaryValidationService().validateDocumentAndUpdatableReferencesRecursively(document, getMaxDictionaryValidationDepth(), true, false); |
79 | 0 | valid &= validateGroupQualifier(groupDoc.getQualifiers(), groupDoc.getKimType()); |
80 | 0 | valid &= validGroupMemberActiveDates(groupDoc.getMembers()); |
81 | 0 | GlobalVariables.getMessageMap().removeFromErrorPath(KNSConstants.DOCUMENT_PROPERTY_NAME); |
82 | |
|
83 | 0 | return valid; |
84 | |
} |
85 | |
|
86 | |
protected boolean validAssignGroup(IdentityManagementGroupDocument document){ |
87 | 0 | boolean rulePassed = true; |
88 | 0 | Map<String,String> additionalPermissionDetails = new HashMap<String,String>(); |
89 | 0 | additionalPermissionDetails.put(KimAttributes.NAMESPACE_CODE, document.getGroupNamespace()); |
90 | 0 | additionalPermissionDetails.put(KimAttributes.GROUP_NAME, document.getGroupName()); |
91 | 0 | if(document.getMembers()!=null && document.getMembers().size()>0){ |
92 | 0 | if(!getDocumentHelperService().getDocumentAuthorizer(document).isAuthorizedByTemplate( |
93 | |
document, KimConstants.NAMESPACE_CODE, KimConstants.PermissionTemplateNames.POPULATE_GROUP, |
94 | |
GlobalVariables.getUserSession().getPrincipalId(), additionalPermissionDetails, null)){ |
95 | 0 | GlobalVariables.getMessageMap().putError("document.groupName", |
96 | |
RiceKeyConstants.ERROR_ASSIGN_GROUP, |
97 | |
new String[] {document.getGroupNamespace(), document.getGroupName()}); |
98 | 0 | rulePassed = false; |
99 | |
} |
100 | |
} |
101 | 0 | return rulePassed; |
102 | |
} |
103 | |
|
104 | |
@SuppressWarnings("unchecked") |
105 | |
protected boolean validDuplicateGroupName(IdentityManagementGroupDocument groupDoc){ |
106 | 0 | Map<String, String> criteria = new HashMap<String, String>(); |
107 | 0 | criteria.put("groupName", groupDoc.getGroupName()); |
108 | 0 | criteria.put("namespaceCode", groupDoc.getGroupNamespace()); |
109 | 0 | List<GroupImpl> groupImpls = (List<GroupImpl>)getBusinessObjectService().findMatching(GroupImpl.class, criteria); |
110 | 0 | boolean rulePassed = true; |
111 | 0 | if(groupImpls!=null && groupImpls.size()>0){ |
112 | 0 | if(groupImpls.size()==1 && groupImpls.get(0).getGroupId().equals(groupDoc.getGroupId())) |
113 | 0 | rulePassed = true; |
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, KimTypeInfo kimType){ |
134 | 0 | AttributeSet validationErrors = new AttributeSet(); |
135 | |
|
136 | |
AttributeSet errorsTemp; |
137 | |
AttributeSet attributeSetToValidate; |
138 | 0 | KimTypeService kimTypeService = KimCommonUtils.getKimTypeService(kimType); |
139 | 0 | GlobalVariables.getMessageMap().removeFromErrorPath(KNSConstants.DOCUMENT_PROPERTY_NAME); |
140 | 0 | attributeSetToValidate = attributeValidationHelper.convertQualifiersToMap(groupQualifiers); |
141 | 0 | errorsTemp = kimTypeService.validateAttributes(kimType.getKimTypeId(), attributeSetToValidate); |
142 | 0 | validationErrors.putAll( attributeValidationHelper.convertErrors("",attributeValidationHelper.convertQualifiersToAttrIdxMap(groupQualifiers),errorsTemp) ); |
143 | 0 | GlobalVariables.getMessageMap().addToErrorPath(KNSConstants.DOCUMENT_PROPERTY_NAME); |
144 | |
|
145 | 0 | if (validationErrors.isEmpty()) { |
146 | 0 | return true; |
147 | |
} else { |
148 | 0 | attributeValidationHelper.moveValidationErrorsToErrorMap(validationErrors); |
149 | 0 | return false; |
150 | |
} |
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 = KNSServiceLocator.getBusinessObjectService(); |
189 | |
} |
190 | 0 | return businessObjectService; |
191 | |
} |
192 | |
|
193 | |
} |