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 public class IdentityManagementGroupDocumentRule extends TransactionalDocumentRuleBase implements AddGroupMemberRule {
51
52 protected AddGroupMemberRule addGroupMemberRule;
53 protected AttributeValidationHelper attributeValidationHelper = new AttributeValidationHelper();
54
55 protected BusinessObjectService businessObjectService;
56 protected Class<? extends GroupDocumentMemberRule> addGroupMemberRuleClass = GroupDocumentMemberRule.class;
57
58 protected IdentityService identityService;
59
60 public IdentityService getIdentityService() {
61 if ( identityService == null) {
62 identityService = KIMServiceLocator.getIdentityService();
63 }
64 return identityService;
65 }
66
67 @Override
68 protected boolean processCustomSaveDocumentBusinessRules(Document document) {
69 if (!(document instanceof IdentityManagementGroupDocument))
70 return false;
71
72 IdentityManagementGroupDocument groupDoc = (IdentityManagementGroupDocument)document;
73
74 boolean valid = true;
75 GlobalVariables.getMessageMap().addToErrorPath(KNSConstants.DOCUMENT_PROPERTY_NAME);
76 valid &= validAssignGroup(groupDoc);
77 valid &= validDuplicateGroupName(groupDoc);
78 getDictionaryValidationService().validateDocumentAndUpdatableReferencesRecursively(document, getMaxDictionaryValidationDepth(), true, false);
79 valid &= validateGroupQualifier(groupDoc.getQualifiers(), groupDoc.getKimType());
80 valid &= validGroupMemberActiveDates(groupDoc.getMembers());
81 GlobalVariables.getMessageMap().removeFromErrorPath(KNSConstants.DOCUMENT_PROPERTY_NAME);
82
83 return valid;
84 }
85
86 protected boolean validAssignGroup(IdentityManagementGroupDocument document){
87 boolean rulePassed = true;
88 Map<String,String> additionalPermissionDetails = new HashMap<String,String>();
89 additionalPermissionDetails.put(KimAttributes.NAMESPACE_CODE, document.getGroupNamespace());
90 additionalPermissionDetails.put(KimAttributes.GROUP_NAME, document.getGroupName());
91 if(document.getMembers()!=null && document.getMembers().size()>0){
92 if(!getDocumentHelperService().getDocumentAuthorizer(document).isAuthorizedByTemplate(
93 document, KimConstants.NAMESPACE_CODE, KimConstants.PermissionTemplateNames.POPULATE_GROUP,
94 GlobalVariables.getUserSession().getPrincipalId(), additionalPermissionDetails, null)){
95 GlobalVariables.getMessageMap().putError("document.groupName",
96 RiceKeyConstants.ERROR_ASSIGN_GROUP,
97 new String[] {document.getGroupNamespace(), document.getGroupName()});
98 rulePassed = false;
99 }
100 }
101 return rulePassed;
102 }
103
104 @SuppressWarnings("unchecked")
105 protected boolean validDuplicateGroupName(IdentityManagementGroupDocument groupDoc){
106 Map<String, String> criteria = new HashMap<String, String>();
107 criteria.put("groupName", groupDoc.getGroupName());
108 criteria.put("namespaceCode", groupDoc.getGroupNamespace());
109 List<GroupImpl> groupImpls = (List<GroupImpl>)getBusinessObjectService().findMatching(GroupImpl.class, criteria);
110 boolean rulePassed = true;
111 if(groupImpls!=null && groupImpls.size()>0){
112 if(groupImpls.size()==1 && groupImpls.get(0).getGroupId().equals(groupDoc.getGroupId()))
113 rulePassed = true;
114 else{
115 GlobalVariables.getMessageMap().putError("document.groupName",
116 RiceKeyConstants.ERROR_DUPLICATE_ENTRY, new String[] {"Group Name"});
117 rulePassed = false;
118 }
119 }
120 return rulePassed;
121 }
122
123 protected boolean validGroupMemberActiveDates(List<GroupDocumentMember> groupMembers) {
124 boolean valid = true;
125 int i = 0;
126 for(GroupDocumentMember groupMember: groupMembers) {
127 valid &= validateActiveDate("document.members["+i+"].activeToDate", groupMember.getActiveFromDate(), groupMember.getActiveToDate());
128 i++;
129 }
130 return valid;
131 }
132
133 protected boolean validateGroupQualifier(List<GroupDocumentQualifier> groupQualifiers, KimTypeInfo kimType){
134 AttributeSet validationErrors = new AttributeSet();
135
136 AttributeSet errorsTemp;
137 AttributeSet attributeSetToValidate;
138 KimTypeService kimTypeService = KimCommonUtils.getKimTypeService(kimType);
139 GlobalVariables.getMessageMap().removeFromErrorPath(KNSConstants.DOCUMENT_PROPERTY_NAME);
140 attributeSetToValidate = attributeValidationHelper.convertQualifiersToMap(groupQualifiers);
141 errorsTemp = kimTypeService.validateAttributes(kimType.getKimTypeId(), attributeSetToValidate);
142 validationErrors.putAll( attributeValidationHelper.convertErrors("",attributeValidationHelper.convertQualifiersToAttrIdxMap(groupQualifiers),errorsTemp) );
143 GlobalVariables.getMessageMap().addToErrorPath(KNSConstants.DOCUMENT_PROPERTY_NAME);
144
145 if (validationErrors.isEmpty()) {
146 return true;
147 } else {
148 attributeValidationHelper.moveValidationErrorsToErrorMap(validationErrors);
149 return false;
150 }
151 }
152
153 protected boolean validateActiveDate(String errorPath, Timestamp activeFromDate, Timestamp activeToDate) {
154
155 boolean valid = true;
156 if (activeFromDate != null && activeToDate !=null && activeToDate.before(activeFromDate)) {
157 MessageMap errorMap = GlobalVariables.getMessageMap();
158 errorMap.putError(errorPath, RiceKeyConstants.ERROR_ACTIVE_TO_DATE_BEFORE_FROM_DATE);
159 valid = false;
160
161 }
162 return valid;
163 }
164
165
166
167
168 public AddGroupMemberRule getAddGroupMemberRule() {
169 if(addGroupMemberRule == null){
170 try {
171 addGroupMemberRule = addGroupMemberRuleClass.newInstance();
172 } catch ( Exception ex ) {
173 throw new RuntimeException( "Unable to create AddMemberRule instance using class: " + addGroupMemberRuleClass, ex );
174 }
175 }
176 return addGroupMemberRule;
177 }
178
179 public boolean processAddGroupMember(AddGroupMemberEvent addGroupMemberEvent) {
180 return new GroupDocumentMemberRule().processAddGroupMember(addGroupMemberEvent);
181 }
182
183
184
185
186 public BusinessObjectService getBusinessObjectService() {
187 if(businessObjectService == null){
188 businessObjectService = KNSServiceLocator.getBusinessObjectService();
189 }
190 return businessObjectService;
191 }
192
193 }