1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kim.ldap;
17
18 import static java.util.Arrays.asList;
19 import static org.kuali.rice.core.util.BufferedLogger.debug;
20
21 import java.util.List;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
25 import org.apache.commons.lang.StringUtils;
26 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
27 import org.kuali.rice.kim.api.identity.principal.Principal;
28 import org.springframework.ldap.core.DirContextOperations;
29
30
31
32
33 public class PrincipalMapper extends BaseMapper<Principal> {
34 private ParameterService parameterService;
35
36 @Override
37 Principal mapDtoFromContext(DirContextOperations context) {
38 Principal.Builder builder = mapBuilderFromContext(context);
39 return builder != null ? builder.build() : null;
40 }
41
42 Principal.Builder mapBuilderFromContext(DirContextOperations context) {
43 final String entityId = context.getStringAttribute(getConstants().getKimLdapIdProperty());
44 final String principalName = context.getStringAttribute(getConstants().getKimLdapNameProperty());
45 final Principal.Builder person = Principal.Builder.create(principalName);
46
47 if (entityId == null) {
48 throw new InvalidLdapEntityException("LDAP Search Results yielded an invalid result with attributes "
49 + context.getAttributes());
50 }
51
52 person.setPrincipalId(entityId);
53 person.setEntityId(entityId);
54 person.setActive(isPersonActive(context));
55
56 return person;
57 }
58
59
60
61
62
63
64
65 protected boolean isPersonActive(DirContextOperations context) {
66 String[] affils = context.getStringAttributes(getConstants().getAffiliationLdapProperty());
67 Object edsVal = getLdapValue("principals.active.Y");
68 if (affils != null && affils.length > 0
69 && edsVal != null) {
70 if (edsVal instanceof List) {
71 List<String> edsValLst = (List<String>)edsVal;
72 for (String affil : affils) {
73 if (edsValLst.contains(affil)) {
74 return true;
75 }
76 }
77 } else {
78 String edsValStr = (String)edsVal;
79 for (String affil : affils) {
80 if (StringUtils.equals(affil, edsValStr)) {
81 return true;
82 }
83 }
84 }
85 }
86 return false;
87 }
88
89 protected Object getLdapValue(String kimAttribute) {
90 Matcher matcher = getKimAttributeMatcher(kimAttribute);
91 debug("Does ", kimAttribute, " match? ", matcher.matches());
92 if (!matcher.matches()) {
93 return null;
94 }
95 String value = matcher.group(2);
96
97
98 if (value.contains(",")) {
99 return asList(value.split(","));
100 }
101
102 return value;
103 }
104
105 protected Matcher getKimAttributeMatcher(String kimAttribute) {
106 String mappedParamValue = getParameterService().getParameterValueAsString(getConstants().getParameterNamespaceCode(),
107 getConstants().getParameterDetailTypeCode(),
108 getConstants().getMappedParameterName());
109
110 String regexStr = String.format("(%s|.*;%s)=([^=;]*).*", kimAttribute, kimAttribute);
111 debug("Matching KIM attribute with regex ", regexStr);
112 Matcher retval = Pattern.compile(regexStr).matcher(mappedParamValue);
113
114 if (!retval.matches()) {
115 mappedParamValue = getParameterService().getParameterValueAsString(getConstants().getParameterNamespaceCode(),
116 getConstants().getParameterDetailTypeCode(),
117 getConstants().getMappedValuesName());
118 retval = Pattern.compile(regexStr).matcher(mappedParamValue);
119 }
120
121 return retval;
122 }
123
124
125 public ParameterService getParameterService() {
126 return this.parameterService;
127 }
128
129 public void setParameterService(ParameterService service) {
130 this.parameterService = service;
131 }
132 }