View Javadoc

1   /**
2    * Copyright 2005-2012 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 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       * Checks the configured active principal affiliations, if one is found, returns true
62       * @param context
63       * @return true if a matching active affiliation is found
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          // If it's actually a list. It can only be a list if there are commas
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 }