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.api.CoreConstants;
20 import org.kuali.rice.core.api.membership.MemberType;
21 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
22 import org.kuali.rice.core.api.uif.RemotableAttributeError;
23 import org.kuali.rice.core.api.util.RiceKeyConstants;
24 import org.kuali.rice.core.api.util.VersionHelper;
25 import org.kuali.rice.kim.api.KimConstants;
26 import org.kuali.rice.kim.api.type.KimType;
27 import org.kuali.rice.kim.bo.ui.KimDocumentRoleMember;
28 import org.kuali.rice.kim.document.IdentityManagementRoleDocument;
29 import org.kuali.rice.kim.document.rule.AttributeValidationHelper;
30 import org.kuali.rice.kim.framework.role.RoleTypeService;
31 import org.kuali.rice.kim.framework.services.KimFrameworkServiceLocator;
32 import org.kuali.rice.kim.framework.type.KimTypeService;
33 import org.kuali.rice.kim.impl.services.KimImplServiceLocator;
34 import org.kuali.rice.kim.rule.event.ui.AddMemberEvent;
35 import org.kuali.rice.kim.rule.ui.AddMemberRule;
36 import org.kuali.rice.kns.rules.DocumentRuleBase;
37 import org.kuali.rice.krad.util.GlobalVariables;
38 import org.kuali.rice.krad.util.KRADUtils;
39 import org.kuali.rice.ksb.api.KsbApiServiceLocator;
40 import org.kuali.rice.ksb.api.bus.Endpoint;
41 import org.kuali.rice.ksb.api.bus.ServiceBus;
42
43 import javax.xml.namespace.QName;
44 import java.util.ArrayList;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Map;
48
49
50
51
52
53
54
55
56 public class KimDocumentMemberRule extends DocumentRuleBase implements AddMemberRule {
57
58 private static final String ERROR_PATH = "member.memberId";
59
60 protected AttributeValidationHelper attributeValidationHelper = new AttributeValidationHelper();
61
62 public boolean processAddMember(AddMemberEvent addMemberEvent){
63 KimDocumentRoleMember newMember = addMemberEvent.getMember();
64 IdentityManagementRoleDocument document = (IdentityManagementRoleDocument)addMemberEvent.getDocument();
65 boolean rulePassed = true;
66
67 if (newMember == null || StringUtils.isBlank(newMember.getMemberId())){
68 GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_EMPTY_ENTRY, new String[] {"Member"});
69 return false;
70 }
71 if(!validAssignRole(newMember, document)) {
72 return false;
73 }
74 List<RemotableAttributeError> validationErrors = new ArrayList<RemotableAttributeError>();
75 KimTypeService kimTypeService = KimFrameworkServiceLocator.getKimTypeService(document.getKimType());
76
77 Long newMemberFromTime = newMember.getActiveFromDate() == null ? 0L : newMember.getActiveFromDate().getTime();
78 Long newMemberToTime = newMember.getActiveToDate() == null ? Long.MAX_VALUE : newMember.getActiveToDate().getTime();
79
80 List<RemotableAttributeError> errorsAttributesAgainstExisting = new ArrayList<RemotableAttributeError>();
81 Map<String, String> newMemberQualifiers = attributeValidationHelper.convertQualifiersToMap(newMember.getQualifiers());
82
83 Map<String, String> oldMemberQualifiers;
84 for (KimDocumentRoleMember member: document.getMembers()){
85 Long memberFromTime = member.getActiveFromDate() == null ? 0L : member.getActiveFromDate().getTime();
86 Long memberToTime = member.getActiveToDate() == null ? Long.MAX_VALUE : member.getActiveToDate().getTime();
87 oldMemberQualifiers = attributeValidationHelper.convertQualifiersToMap(member.getQualifiers());
88
89 if ((member.getMemberId().equals(newMember.getMemberId()) &&
90 member.getMemberTypeCode().equals(newMember.getMemberTypeCode()))
91 && ((newMemberFromTime >= memberFromTime && newMemberFromTime < memberToTime)
92 || (newMemberToTime >= memberFromTime && newMemberToTime <= memberToTime))) {
93
94 errorsAttributesAgainstExisting = kimTypeService.validateAttributesAgainstExisting(
95 document.getKimType().getId(), newMemberQualifiers, oldMemberQualifiers);
96 validationErrors.addAll(
97 attributeValidationHelper.convertErrorsForMappedFields(ERROR_PATH, errorsAttributesAgainstExisting));
98 if (!errorsAttributesAgainstExisting.isEmpty()) {
99 rulePassed = false;
100 GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_DUPLICATE_ENTRY, new String[] {"Member"});
101 break;
102 }
103 }
104 }
105
106 boolean shouldNotValidate = newMember.isRole();
107 if ( kimTypeService != null && KRADUtils.isNotNull(document.getKimType()) && StringUtils.isNotBlank(document.getKimType().getServiceName()) ) {
108 VersionedService<RoleTypeService> versionedRoleTypeService = getVersionedRoleTypeService(document.getKimType());
109 if (versionedRoleTypeService != null) {
110 boolean versionOk = VersionHelper.compareVersion(versionedRoleTypeService.getVersion(), CoreConstants.Versions.VERSION_2_1_2)!=-1? true:false;
111 if(versionOk) {
112 shouldNotValidate = versionedRoleTypeService.getService().shouldValidateQualifiersForMemberType( MemberType.fromCode(newMember.getMemberTypeCode()));
113 } else {
114 shouldNotValidate = false;
115 }
116 }
117 }
118 if (kimTypeService !=null && !shouldNotValidate) {
119 List<RemotableAttributeError> localErrors = kimTypeService.validateAttributes( document.getKimType().getId(), attributeValidationHelper.convertQualifiersToMap( newMember.getQualifiers() ) );
120 validationErrors.addAll( attributeValidationHelper.convertErrors("member",
121 attributeValidationHelper.convertQualifiersToAttrIdxMap(newMember.getQualifiers()), localErrors) );
122 }
123 if (!validationErrors.isEmpty()) {
124 attributeValidationHelper.moveValidationErrorsToErrorMap(validationErrors);
125 rulePassed = false;
126 }
127
128 return rulePassed;
129 }
130
131 protected boolean validAssignRole(KimDocumentRoleMember roleMember, IdentityManagementRoleDocument document){
132 boolean rulePassed = true;
133 if(StringUtils.isNotEmpty(document.getRoleNamespace())){
134 Map<String,String> roleDetails = new HashMap<String,String>();
135 roleDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, document.getRoleNamespace());
136 roleDetails.put(KimConstants.AttributeConstants.ROLE_NAME, document.getRoleName());
137 if (!getDocumentDictionaryService().getDocumentAuthorizer(document).isAuthorizedByTemplate(
138 document,
139 KimConstants.NAMESPACE_CODE,
140 KimConstants.PermissionTemplateNames.ASSIGN_ROLE,
141 GlobalVariables.getUserSession().getPerson().getPrincipalId(),
142 roleDetails, null)){
143 GlobalVariables.getMessageMap().putError(ERROR_PATH, RiceKeyConstants.ERROR_ASSIGN_ROLE,
144 new String[] {document.getRoleNamespace(), document.getRoleName()});
145 rulePassed = false;
146 }
147 }
148 return rulePassed;
149 }
150
151 private static class VersionedService<T> {
152
153 String version;
154 T service;
155
156 VersionedService(String version, T service) {
157 this.version = version;
158 this.service = service;
159 }
160
161 T getService() {
162 return this.service;
163 }
164
165 String getVersion() {
166 return this.version;
167 }
168
169 }
170
171 protected VersionedService<RoleTypeService> getVersionedRoleTypeService(KimType typeInfo) {
172 String serviceName = typeInfo.getServiceName();
173 if (serviceName != null) {
174 String version = "2.0.0";
175 RoleTypeService roleTypeService = null;
176
177 try {
178
179 ServiceBus serviceBus = KsbApiServiceLocator.getServiceBus();
180 Endpoint endpoint = serviceBus.getEndpoint(QName.valueOf(serviceName));
181 if (endpoint != null) {
182 version = endpoint.getServiceConfiguration().getServiceVersion();
183 }
184 KimTypeService service = (KimTypeService) GlobalResourceLoader.getService(QName.valueOf(serviceName));
185 if (service != null && service instanceof RoleTypeService) {
186 roleTypeService = (RoleTypeService) service;
187 } else {
188 roleTypeService = (RoleTypeService) KimImplServiceLocator.getService("kimNoMembersRoleTypeService");
189 }
190 } catch (Exception ex) {
191 roleTypeService = (RoleTypeService) KimImplServiceLocator.getService("kimNoMembersRoleTypeService");
192 }
193
194 return new VersionedService<RoleTypeService>(version, roleTypeService);
195 }
196
197 return null;
198 }
199
200
201 }