1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kim.service.impl;
17
18 import org.apache.commons.collections.CollectionUtils;
19 import org.apache.commons.lang.StringUtils;
20 import org.kuali.rice.core.api.membership.MemberType;
21 import org.kuali.rice.kim.api.KimConstants;
22 import org.kuali.rice.kim.api.group.Group;
23 import org.kuali.rice.kim.api.identity.address.EntityAddress;
24 import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliation;
25 import org.kuali.rice.kim.api.identity.email.EntityEmail;
26 import org.kuali.rice.kim.api.identity.employment.EntityEmployment;
27 import org.kuali.rice.kim.api.identity.entity.Entity;
28 import org.kuali.rice.kim.api.identity.name.EntityName;
29 import org.kuali.rice.kim.api.identity.phone.EntityPhone;
30 import org.kuali.rice.kim.api.identity.principal.Principal;
31 import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfo;
32 import org.kuali.rice.kim.api.role.Role;
33 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
34 import org.kuali.rice.kim.bo.ui.KimDocumentRoleMember;
35 import org.kuali.rice.kim.bo.ui.PersonDocumentAddress;
36 import org.kuali.rice.kim.bo.ui.PersonDocumentAffiliation;
37 import org.kuali.rice.kim.bo.ui.PersonDocumentEmail;
38 import org.kuali.rice.kim.bo.ui.PersonDocumentEmploymentInfo;
39 import org.kuali.rice.kim.bo.ui.PersonDocumentName;
40 import org.kuali.rice.kim.bo.ui.PersonDocumentPhone;
41 import org.kuali.rice.kim.document.IdentityManagementPersonDocument;
42 import org.kuali.rice.kim.document.IdentityManagementRoleDocument;
43 import org.kuali.rice.kim.framework.services.KimFrameworkServiceLocator;
44 import org.kuali.rice.kim.framework.type.KimTypeService;
45 import org.kuali.rice.kim.impl.common.delegate.DelegateTypeBo;
46 import org.kuali.rice.kim.impl.group.GroupBo;
47 import org.kuali.rice.kim.impl.group.GroupMemberBo;
48 import org.kuali.rice.kim.impl.identity.entity.EntityBo;
49 import org.kuali.rice.kim.impl.identity.principal.PrincipalBo;
50 import org.kuali.rice.kim.impl.role.RoleBo;
51 import org.kuali.rice.kim.impl.role.RoleMemberAttributeDataBo;
52 import org.kuali.rice.kim.impl.role.RoleMemberBo;
53 import org.kuali.rice.kim.impl.role.RoleResponsibilityActionBo;
54 import org.kuali.rice.kim.impl.services.KimImplServiceLocator;
55 import org.kuali.rice.kim.util.KimCommonUtilsInternal;
56 import org.kuali.rice.krad.bo.BusinessObject;
57 import org.kuali.rice.krad.bo.PersistableBusinessObject;
58 import org.kuali.rice.krad.document.Document;
59 import org.kuali.rice.krad.util.ObjectUtils;
60
61 import java.sql.Timestamp;
62 import java.util.ArrayList;
63 import java.util.Collections;
64 import java.util.HashMap;
65 import java.util.List;
66 import java.util.Map;
67
68
69
70
71
72
73 public class LdapUiDocumentServiceImpl extends org.kuali.rice.kim.service.impl.UiDocumentServiceImpl {
74
75
76
77
78
79 public void loadEntityToPersonDoc(IdentityManagementPersonDocument identityManagementPersonDocument, String principalId) {
80 Principal principal = this.getIdentityService().getPrincipal(principalId);
81 if(principal==null) {
82 throw new RuntimeException("Principal does not exist for principal id:"+principalId);
83 }
84
85 identityManagementPersonDocument.setPrincipalId(principal.getPrincipalId());
86 identityManagementPersonDocument.setPrincipalName(principal.getPrincipalName());
87 identityManagementPersonDocument.setPassword(principal.getPassword());
88 identityManagementPersonDocument.setActive(principal.isActive());
89 Entity kimEntity = this.getIdentityService().getEntity(principal.getEntityId());
90 identityManagementPersonDocument.setEntityId(kimEntity.getId());
91 if ( ObjectUtils.isNotNull( kimEntity.getPrivacyPreferences() ) ) {
92 identityManagementPersonDocument.setPrivacy(loadPrivacyReferences(kimEntity.getPrivacyPreferences()));
93 }
94
95 identityManagementPersonDocument.setAffiliations(loadAffiliations(kimEntity.getAffiliations(),kimEntity.getEmploymentInformation()));
96 identityManagementPersonDocument.setNames(loadNames( identityManagementPersonDocument, principalId, kimEntity.getNames(), identityManagementPersonDocument.getPrivacy().isSuppressName() ));
97 EntityTypeContactInfo entityType = null;
98 for (EntityTypeContactInfo type : kimEntity.getEntityTypeContactInfos()) {
99 if (KimConstants.EntityTypes.PERSON.equals(type.getEntityTypeCode())) {
100 entityType = EntityTypeContactInfo.Builder.create(type).build();
101 }
102 }
103
104 if(entityType!=null){
105 identityManagementPersonDocument.setEmails(loadEmails(identityManagementPersonDocument, principalId, entityType.getEmailAddresses(), identityManagementPersonDocument.getPrivacy().isSuppressEmail()));
106 identityManagementPersonDocument.setPhones(loadPhones(identityManagementPersonDocument, principalId, entityType.getPhoneNumbers(), identityManagementPersonDocument.getPrivacy().isSuppressPhone()));
107 identityManagementPersonDocument.setAddrs(loadAddresses(identityManagementPersonDocument, principalId, entityType.getAddresses(), identityManagementPersonDocument.getPrivacy().isSuppressAddress()));
108 }
109
110 List<Group> groups = getGroupService().getGroups(getGroupService().getDirectGroupIdsByPrincipalId(
111 identityManagementPersonDocument.getPrincipalId()));
112 loadGroupToPersonDoc(identityManagementPersonDocument, groups);
113 loadRoleToPersonDoc(identityManagementPersonDocument);
114 loadDelegationsToPersonDoc(identityManagementPersonDocument);
115 }
116
117 protected String getInitiatorPrincipalId(Document document){
118 try{
119 return document.getDocumentHeader().getWorkflowDocument().getInitiatorPrincipalId();
120 } catch(Exception ex){
121 return null;
122 }
123 }
124
125
126
127
128 public void saveEntityPerson(IdentityManagementPersonDocument identityManagementPersonDocument) {
129 final Entity kimEntity = getIdentityService().getEntity(identityManagementPersonDocument.getEntityId());
130 boolean creatingNew = false;
131
132 String initiatorPrincipalId = getInitiatorPrincipalId(identityManagementPersonDocument);
133 boolean inactivatingPrincipal = false;
134
135 List <GroupMemberBo> groupPrincipals = populateGroupMembers(identityManagementPersonDocument);
136 List <RoleMemberBo> rolePrincipals = populateRoleMembers(identityManagementPersonDocument);
137 List <DelegateTypeBo> personDelegations = populateDelegations(identityManagementPersonDocument);
138 List <PersistableBusinessObject> bos = new ArrayList<PersistableBusinessObject>();
139 List <RoleResponsibilityActionBo> roleRspActions = populateRoleRspActions(identityManagementPersonDocument);
140 List <RoleMemberAttributeDataBo> blankRoleMemberAttrs = getBlankRoleMemberAttrs(rolePrincipals);
141
142
143 bos.addAll(groupPrincipals);
144 bos.addAll(rolePrincipals);
145 bos.addAll(roleRspActions);
146 bos.addAll(personDelegations);
147
148 getBusinessObjectService().save(bos);
149
150 if (!blankRoleMemberAttrs.isEmpty()) {
151 getBusinessObjectService().delete(blankRoleMemberAttrs);
152 }
153 if ( inactivatingPrincipal ) {
154
155 KimImplServiceLocator.getRoleInternalService().principalInactivated(identityManagementPersonDocument.getPrincipalId());
156 }
157 }
158
159 protected boolean setupPrincipal(IdentityManagementPersonDocument identityManagementPersonDocument,EntityBo kimEntity, List<PrincipalBo> origPrincipals) {
160 boolean inactivatingPrincipal = false;
161 List<PrincipalBo> principals = new ArrayList<PrincipalBo>();
162 Principal.Builder principal = Principal.Builder.create(identityManagementPersonDocument.getPrincipalName());
163 principal.setPrincipalId(identityManagementPersonDocument.getPrincipalId());
164 principal.setPassword(identityManagementPersonDocument.getPassword());
165 principal.setActive(identityManagementPersonDocument.isActive());
166 principal.setEntityId(identityManagementPersonDocument.getEntityId());
167 if(ObjectUtils.isNotNull(origPrincipals)){
168 for (PrincipalBo prncpl : origPrincipals) {
169 if (prncpl.getPrincipalId()!=null && StringUtils.equals(prncpl.getPrincipalId(), principal.getPrincipalId())) {
170 principal.setVersionNumber(prncpl.getVersionNumber());
171 principal.setObjectId(prncpl.getObjectId());
172
173 if ( prncpl.isActive() && !principal.isActive() ) {
174 inactivatingPrincipal = true;
175 }
176 }
177 }
178 }
179 principals.add(PrincipalBo.from(principal.build()));
180
181 kimEntity.setPrincipals(principals);
182 return inactivatingPrincipal;
183 }
184
185 protected List<PersonDocumentAffiliation> loadAffiliations(List <EntityAffiliation> affiliations, List<EntityEmployment> empInfos) {
186 List<PersonDocumentAffiliation> docAffiliations = new ArrayList<PersonDocumentAffiliation>();
187 if(ObjectUtils.isNotNull(affiliations)){
188 for (EntityAffiliation affiliation: affiliations) {
189 if(affiliation.isActive()){
190 PersonDocumentAffiliation docAffiliation = new PersonDocumentAffiliation();
191 docAffiliation.setAffiliationTypeCode(affiliation.getAffiliationType().getCode());
192 docAffiliation.setCampusCode(affiliation.getCampusCode());
193 docAffiliation.setActive(affiliation.isActive());
194 docAffiliation.setDflt(affiliation.isDefaultValue());
195 docAffiliation.setEntityAffiliationId(affiliation.getId());
196 docAffiliation.refreshReferenceObject("affiliationType");
197
198 docAffiliations.add(docAffiliation);
199 docAffiliation.setEdit(true);
200
201 List<PersonDocumentEmploymentInfo> docEmploymentInformations = new ArrayList<PersonDocumentEmploymentInfo>();
202 if(ObjectUtils.isNotNull(empInfos)){
203 for (EntityEmployment empInfo: empInfos) {
204 if (empInfo.isActive()
205 && StringUtils.equals(docAffiliation.getEntityAffiliationId(),
206 (empInfo.getEntityAffiliation() != null ? empInfo.getEntityAffiliation().getId() : null))) {
207 PersonDocumentEmploymentInfo docEmpInfo = new PersonDocumentEmploymentInfo();
208 docEmpInfo.setEntityEmploymentId(empInfo.getEmployeeId());
209 docEmpInfo.setEmployeeId(empInfo.getEmployeeId());
210 docEmpInfo.setEmploymentRecordId(empInfo.getEmploymentRecordId());
211 docEmpInfo.setBaseSalaryAmount(empInfo.getBaseSalaryAmount());
212 docEmpInfo.setPrimaryDepartmentCode(empInfo.getPrimaryDepartmentCode());
213 docEmpInfo.setEmploymentStatusCode(empInfo.getEmployeeStatus() != null ? empInfo.getEmployeeStatus().getCode() : null);
214 docEmpInfo.setEmploymentTypeCode(empInfo.getEmployeeType() != null ? empInfo.getEmployeeType().getCode() : null);
215 docEmpInfo.setActive(empInfo.isActive());
216 docEmpInfo.setPrimary(empInfo.isPrimary());
217 docEmpInfo.setEntityAffiliationId(empInfo.getEntityAffiliation() != null ? empInfo.getEntityAffiliation().getId() : null);
218
219
220 docEmpInfo.setEdit(true);
221 docEmpInfo.refreshReferenceObject("employmentType");
222 docEmploymentInformations.add(docEmpInfo);
223 }
224 }
225 }
226 docAffiliation.setEmpInfos(docEmploymentInformations);
227 }
228 }
229 }
230 return docAffiliations;
231
232 }
233
234
235 protected List<PersonDocumentName> loadNames( IdentityManagementPersonDocument personDoc, String principalId, List <EntityName> names, boolean suppressDisplay ) {
236 List<PersonDocumentName> docNames = new ArrayList<PersonDocumentName>();
237 if(ObjectUtils.isNotNull(names)){
238 for (EntityName name: names) {
239 if(name.isActive()){
240 PersonDocumentName docName = new PersonDocumentName();
241 if (name.getNameType() != null) {
242 docName.setNameCode(name.getNameType().getCode());
243 }
244
245
246 docName.setFirstName(name.getFirstNameUnmasked());
247 docName.setLastName(name.getLastNameUnmasked());
248 docName.setMiddleName(name.getMiddleNameUnmasked());
249 docName.setNamePrefix(name.getNamePrefixUnmasked());
250 docName.setNameSuffix(name.getNameSuffixUnmasked());
251
252 docName.setActive(name.isActive());
253 docName.setDflt(name.isDefaultValue());
254 docName.setEdit(true);
255 docName.setEntityNameId(name.getId());
256 docNames.add(docName);
257 }
258 }
259 }
260 return docNames;
261 }
262
263 protected List<PersonDocumentAddress> loadAddresses(IdentityManagementPersonDocument identityManagementPersonDocument, String principalId, List<EntityAddress> entityAddresses, boolean suppressDisplay ) {
264 List<PersonDocumentAddress> docAddresses = new ArrayList<PersonDocumentAddress>();
265 if(ObjectUtils.isNotNull(entityAddresses)){
266 for (EntityAddress address: entityAddresses) {
267 if(address.isActive()){
268 PersonDocumentAddress docAddress = new PersonDocumentAddress();
269 docAddress.setEntityTypeCode(address.getEntityTypeCode());
270 docAddress.setAddressTypeCode(address.getAddressType().getCode());
271
272
273 docAddress.setLine1(address.getLine1Unmasked());
274 docAddress.setLine2(address.getLine2Unmasked());
275 docAddress.setLine3(address.getLine3Unmasked());
276 docAddress.setStateProvinceCode(address.getStateProvinceCodeUnmasked());
277 docAddress.setPostalCode(address.getPostalCodeUnmasked());
278 docAddress.setCountryCode(address.getCountryCodeUnmasked());
279 docAddress.setCity(address.getCityUnmasked());
280
281 docAddress.setActive(address.isActive());
282 docAddress.setDflt(address.isDefaultValue());
283 docAddress.setEntityAddressId(address.getId());
284 docAddress.setEdit(true);
285 docAddresses.add(docAddress);
286 }
287 }
288 }
289 return docAddresses;
290 }
291
292 protected List<PersonDocumentEmail> loadEmails(IdentityManagementPersonDocument identityManagementPersonDocument, String principalId, List<EntityEmail> entityEmails, boolean suppressDisplay ) {
293 List<PersonDocumentEmail> emails = new ArrayList<PersonDocumentEmail>();
294 if(ObjectUtils.isNotNull(entityEmails)){
295 for (EntityEmail email: entityEmails) {
296 if(email.isActive()){
297 PersonDocumentEmail docEmail = new PersonDocumentEmail();
298
299 docEmail.setEntityTypeCode(email.getEntityTypeCode());
300 if (email.getEmailType() != null) {
301 docEmail.setEmailTypeCode(email.getEmailType().getCode());
302 }
303
304
305
306 docEmail.setEmailAddress(email.getEmailAddressUnmasked());
307
308 docEmail.setActive(email.isActive());
309 docEmail.setDflt(email.isDefaultValue());
310 docEmail.setEntityEmailId(email.getId());
311 docEmail.setEdit(true);
312 emails.add(docEmail);
313 }
314 }
315 }
316 return emails;
317 }
318
319 protected List<PersonDocumentPhone> loadPhones(IdentityManagementPersonDocument identityManagementPersonDocument, String principalId, List<EntityPhone> entityPhones, boolean suppressDisplay ) {
320 List<PersonDocumentPhone> docPhones = new ArrayList<PersonDocumentPhone>();
321 if(ObjectUtils.isNotNull(entityPhones)){
322 for (EntityPhone phone: entityPhones) {
323 if(phone.isActive()){
324 PersonDocumentPhone docPhone = new PersonDocumentPhone();
325 if (phone.getPhoneType() != null) {
326 docPhone.setPhoneTypeCode(phone.getPhoneType().getCode());
327 }
328
329 docPhone.setEntityTypeCode(phone.getEntityTypeCode());
330
331 docPhone.setPhoneNumber(phone.getPhoneNumberUnmasked());
332 docPhone.setCountryCode(phone.getCountryCodeUnmasked());
333 docPhone.setExtensionNumber(phone.getExtensionNumberUnmasked());
334
335 docPhone.setActive(phone.isActive());
336 docPhone.setDflt(phone.isDefaultValue());
337 docPhone.setEntityPhoneId(phone.getId());
338 docPhone.setEdit(true);
339 docPhones.add(docPhone);
340 }
341 }
342 }
343 return docPhones;
344
345 }
346
347 public BusinessObject getMember(String memberTypeCode, String memberId){
348 Class<? extends BusinessObject> roleMemberTypeClass = null;
349 String roleMemberIdName = "";
350 if(MemberType.PRINCIPAL.getCode().equals(memberTypeCode)){
351 roleMemberTypeClass = PrincipalBo.class;
352 roleMemberIdName = KimConstants.PrimaryKeyConstants.PRINCIPAL_ID;
353 Principal principalInfo = getIdentityService().getPrincipal(memberId);
354 if (principalInfo != null) {
355
356 }
357 } else if(MemberType.GROUP.getCode().equals(memberTypeCode)){
358 roleMemberTypeClass = GroupBo.class;
359 roleMemberIdName = KimConstants.PrimaryKeyConstants.GROUP_ID;
360 Group groupInfo = null;
361 groupInfo = getGroupService().getGroup(memberId);
362 if (groupInfo != null) {
363
364 }
365 } else if(MemberType.ROLE.getCode().equals(memberTypeCode)){
366 roleMemberTypeClass = RoleBo.class;
367 roleMemberIdName = KimConstants.PrimaryKeyConstants.ROLE_ID;
368 Role role = getRoleService().getRole(memberId);
369 if (role != null) {
370
371 }
372 }
373 Map<String, String> criteria = new HashMap<String, String>();
374 criteria.put(roleMemberIdName, memberId);
375 return getBusinessObjectService().findByPrimaryKey(roleMemberTypeClass, criteria);
376 }
377
378
379
380
381
382
383 @Override
384 public boolean canModifyEntity( String currentUserPrincipalId, String toModifyPrincipalId ){
385 return (StringUtils.isNotBlank(currentUserPrincipalId) && StringUtils.isNotBlank(toModifyPrincipalId) &&
386 currentUserPrincipalId.equals(toModifyPrincipalId)) ||
387 getPermissionService().isAuthorized(
388 currentUserPrincipalId,
389 KimConstants.NAMESPACE_CODE,
390 KimConstants.PermissionNames.MODIFY_ENTITY,
391 Collections.<String, String>emptyMap(),
392 Collections.singletonMap(KimConstants.AttributeConstants.PRINCIPAL_ID, currentUserPrincipalId));
393 }
394
395 protected List<RoleMemberBo> getRoleMembers(IdentityManagementRoleDocument identityManagementRoleDocument, List<RoleMemberBo> origRoleMembers){
396 List<RoleMemberBo> roleMembers = new ArrayList<RoleMemberBo>();
397 RoleMemberBo newRoleMember;
398 RoleMemberBo origRoleMemberImplTemp;
399 List<RoleMemberAttributeDataBo> origAttributes;
400 boolean activatingInactive = false;
401 String newRoleMemberIdAssigned = "";
402
403 identityManagementRoleDocument.setKimType(KimApiServiceLocator.getKimTypeInfoService().getKimType(identityManagementRoleDocument.getRoleTypeId()));
404 KimTypeService kimTypeService = KimFrameworkServiceLocator.getKimTypeService(identityManagementRoleDocument.getKimType());
405
406 if(CollectionUtils.isNotEmpty(identityManagementRoleDocument.getMembers())){
407 for(KimDocumentRoleMember documentRoleMember: identityManagementRoleDocument.getMembers()){
408 origRoleMemberImplTemp = null;
409
410 newRoleMember = new RoleMemberBo();
411 KimCommonUtilsInternal.copyProperties(newRoleMember, documentRoleMember);
412 newRoleMember.setRoleId(identityManagementRoleDocument.getRoleId());
413 if(ObjectUtils.isNotNull(origRoleMembers)){
414 for(RoleMemberBo origRoleMemberImpl: origRoleMembers){
415 if((origRoleMemberImpl.getRoleId()!=null && StringUtils.equals(origRoleMemberImpl.getRoleId(), newRoleMember.getRoleId())) &&
416 (origRoleMemberImpl.getMemberId()!=null && StringUtils.equals(origRoleMemberImpl.getMemberId(), newRoleMember.getMemberId())) &&
417 (origRoleMemberImpl.getMemberType()!=null && org.apache.commons.lang.ObjectUtils.equals(origRoleMemberImpl.getMemberType(), newRoleMember.getMemberType())) &&
418 !origRoleMemberImpl.isActive(new Timestamp(System.currentTimeMillis())) &&
419 !kimTypeService.validateAttributesAgainstExisting(identityManagementRoleDocument.getKimType().getId(),
420 documentRoleMember.getQualifierAsMap(), origRoleMemberImpl.getAttributes()).isEmpty()) {
421
422
423
424 newRoleMemberIdAssigned = newRoleMember.getRoleMemberId();
425 newRoleMember.setRoleMemberId(origRoleMemberImpl.getRoleMemberId());
426 activatingInactive = true;
427 }
428 if(origRoleMemberImpl.getRoleMemberId()!=null && StringUtils.equals(origRoleMemberImpl.getRoleMemberId(), newRoleMember.getRoleMemberId())){
429 newRoleMember.setVersionNumber(origRoleMemberImpl.getVersionNumber());
430 origRoleMemberImplTemp = origRoleMemberImpl;
431 }
432 }
433 }
434 origAttributes = (origRoleMemberImplTemp==null || origRoleMemberImplTemp.getAttributes()==null)?
435 new ArrayList<RoleMemberAttributeDataBo>():origRoleMemberImplTemp.getAttributeDetails();
436 newRoleMember.setActiveFromDateValue(documentRoleMember.getActiveFromDate());
437 newRoleMember.setActiveToDateValue(documentRoleMember.getActiveToDate());
438 newRoleMember.setAttributeDetails(getRoleMemberAttributeData(documentRoleMember.getQualifiers(), origAttributes, activatingInactive, newRoleMemberIdAssigned));
439 newRoleMember.setRoleRspActions(getRoleMemberResponsibilityActions(documentRoleMember, origRoleMemberImplTemp, activatingInactive, newRoleMemberIdAssigned));
440 roleMembers.add(newRoleMember);
441 activatingInactive = false;
442 }
443 }
444 return roleMembers;
445 }
446 }