View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  public class PrincipalMapper extends AbstractContextMapper {
39      private Constants constants;
40      private ParameterService parameterService;
41      
42      public Principal.Builder mapFromContext(DirContextOperations context) {
43          return (Principal.Builder) doMapFromContext(context);
44      }
45  
46      public Object doMapFromContext(DirContextOperations context) {
47          final String entityId      = context.getStringAttribute(getConstants().getKimLdapIdProperty());
48          final String principalName = context.getStringAttribute(getConstants().getKimLdapNameProperty());
49          final Principal.Builder person = Principal.Builder.create(principalName);
50          
51          if (entityId == null) {
52              throw new InvalidLdapEntityException("LDAP Search Results yielded an invalid result with attributes " 
53                                                   + context.getAttributes());
54          }
55          
56          person.setPrincipalId(entityId);
57          person.setEntityId(entityId);
58          person.setActive(isPersonActive(context));
59  
60          return person;
61      }
62      
63       /**
64       * 
65       * Checks the configured active principal affiliations, if one is found, returns true
66       * @param context
67       * @return true if a matching active affiliation is found
68       */
69      protected boolean isPersonActive(DirContextOperations context) {
70          String[] affils = context.getStringAttributes(getConstants().getAffiliationLdapProperty());
71          Object edsVal = getLdapValue("principals.active.Y");
72          if (affils != null && affils.length > 0
73                  && edsVal != null) {
74              if (edsVal instanceof List) {
75                  List<String> edsValLst = (List<String>)edsVal;
76                  for (String affil : affils) {
77                      if (edsValLst.contains(affil)) {
78                          return true;
79                      }
80                  }
81              } else {
82                  String edsValStr = (String)edsVal;
83                  for (String affil : affils) {
84                      if (StringUtils.equals(affil, edsValStr)) {
85                          return true;
86                      }
87                  }
88              }
89          }
90          return false;
91      }
92  
93      protected Object getLdapValue(String kimAttribute) {
94          Matcher matcher = getKimAttributeMatcher(kimAttribute);
95          debug("Does ", kimAttribute, " match? ", matcher.matches());
96          if (!matcher.matches()) {
97              return null;
98          }
99          String value = matcher.group(2);
100 
101         // If it's actually a list. It can only be a list if there are commas
102         if (value.contains(",")) {
103             return asList(value.split(","));
104         }
105 
106         return value;
107     }
108 
109     protected Matcher getKimAttributeMatcher(String kimAttribute) {
110         String mappedParamValue = getParameterService().getParameterValueAsString(getConstants().getParameterNamespaceCode(),
111                                                                         getConstants().getParameterDetailTypeCode(),
112                                                                         getConstants().getMappedParameterName());
113 
114         String regexStr = String.format("(%s|.*;%s)=([^=;]*).*", kimAttribute, kimAttribute);
115         debug("Matching KIM attribute with regex ", regexStr);
116         Matcher retval = Pattern.compile(regexStr).matcher(mappedParamValue);
117         
118         if (!retval.matches()) {
119             mappedParamValue = getParameterService().getParameterValueAsString(getConstants().getParameterNamespaceCode(),
120                                                                   getConstants().getParameterDetailTypeCode(),
121                                                                   getConstants().getMappedValuesName());
122             retval = Pattern.compile(regexStr).matcher(mappedParamValue);
123         }
124 
125         return retval;
126     }
127 
128    /**
129      * Gets the value of constants
130      *
131      * @return the value of constants
132      */
133     public final Constants getConstants() {
134         return this.constants;
135     }
136 
137     /**
138      * Sets the value of constants
139      *
140      * @param argConstants Value to assign to this.constants
141      */
142     public final void setConstants(final Constants argConstants) {
143         this.constants = argConstants;
144     }
145 
146     public ParameterService getParameterService() {
147         return this.parameterService;
148     }
149 
150     public void setParameterService(ParameterService service) {
151         this.parameterService = service;
152     }
153 }