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