View Javadoc
1   /**
2    * Copyright 2005-2015 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.service.impl;
17  
18  import java.sql.Timestamp;
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.joda.time.DateTime;
26  import org.kuali.rice.core.api.criteria.CriteriaValue;
27  import org.kuali.rice.core.api.criteria.EqualPredicate;
28  import org.kuali.rice.core.api.criteria.Predicate;
29  import org.kuali.rice.core.api.criteria.QueryByCriteria;
30  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
31  import org.kuali.rice.kim.api.identity.IdentityService;
32  import org.kuali.rice.kim.api.identity.entity.Entity;
33  import org.kuali.rice.kim.api.identity.entity.EntityDefault;
34  import org.kuali.rice.kim.api.identity.entity.EntityDefaultQueryResults;
35  import org.kuali.rice.kim.api.identity.principal.Principal;
36  import org.kuali.rice.kim.api.identity.privacy.EntityPrivacyPreferences;
37  import org.kuali.rice.kim.dao.LdapPrincipalDao;
38  import org.kuali.rice.kim.impl.identity.IdentityServiceImpl;
39  import org.kuali.rice.kim.impl.identity.entity.EntityBo;
40  
41  import javax.jws.WebParam;
42  
43  /**
44   * Implementation of {@link IdentityService} that communicates with and serves information
45   * from the UA Enterprise Directory Service.
46   * 
47   * 
48   * @author Kuali Rice Team (rice.collab@kuali.org)
49   */
50  public class LdapIdentityDelegateServiceImpl extends IdentityServiceImpl {
51      private LdapPrincipalDao principalDao;
52  
53      @Override
54  	public Entity getEntity(String entityId) {
55          if (StringUtils.isBlank(entityId)) {
56              throw new RiceIllegalArgumentException("entityId is blank");
57          }
58  
59          Entity edsInfo = getPrincipalDao().getEntity(entityId);
60          if (edsInfo != null) {
61              return edsInfo;
62          } else {
63              return super.getEntity(entityId);
64          }
65  	}
66  	
67  	/**
68  	 * Overridden to populate this information from the LdapPrincipalDao
69  	 */
70      @Override
71  	public Entity getEntityByPrincipalId(String principalId) {
72          if (StringUtils.isBlank(principalId)) {
73              throw new RiceIllegalArgumentException("principalId is blank");
74          }
75  
76          Entity edsInfo = getPrincipalDao().getEntityByPrincipalId(principalId);
77          if (edsInfo != null) {
78              return edsInfo;
79          } else {
80              return super.getEntityByPrincipalId(principalId);
81          }
82  	}
83  	
84      @Override
85  	public Entity getEntityByPrincipalName(String principalName) {
86          if (StringUtils.isBlank(principalName)) {
87              throw new RiceIllegalArgumentException("principalName is blank");
88          }
89  
90          final Entity edsInfo = getPrincipalDao().getEntityByPrincipalName(principalName);
91          if (edsInfo != null) {
92              return edsInfo;
93          } else {
94              return super.getEntityByPrincipalName(principalName);
95          }
96  	}
97  	
98      @Override
99  	public EntityDefault getEntityDefault(String entityId) {
100         if (StringUtils.isBlank(entityId)) {
101             throw new RiceIllegalArgumentException("entityId is blank");
102         }
103 
104         EntityDefault edsInfo = getPrincipalDao().getEntityDefault(entityId);
105         if (edsInfo != null) {
106             return edsInfo;
107         } 
108         else {
109             return super.getEntityDefault(entityId);
110         }
111 	}
112 	
113     @Override
114 	public EntityDefault getEntityDefaultByPrincipalId(String principalId) {
115         if (StringUtils.isBlank(principalId)) {
116             throw new RiceIllegalArgumentException("principalId is blank");
117         }
118 
119         final EntityDefault retval = getPrincipalDao().getEntityDefaultByPrincipalId(principalId);
120         if (retval != null) {
121             return retval;
122         }
123         else {
124             return super.getEntityDefaultByPrincipalId(principalId);
125         }
126 	}
127 	
128     @Override
129 	public EntityDefault getEntityDefaultByPrincipalName(String principalName) {
130         if (StringUtils.isBlank(principalName)) {
131             throw new RiceIllegalArgumentException("principalName is blank");
132         }
133 
134         final EntityDefault retval = getPrincipalDao().getEntityDefaultByPrincipalName(principalName);
135         if (retval != null) {
136             return retval;
137         }
138         else {
139             return super.getEntityDefaultByPrincipalName(principalName);
140         }
141 	}
142     
143 	
144     private static <U extends CriteriaValue<?>> Object getVal(U toConv) {
145         Object o = toConv.getValue();
146         if (o instanceof DateTime) {
147             return new Timestamp(((DateTime) o).getMillis());
148         }
149         return o;
150     }
151     
152     /**
153      * Password lookups not supported by EDS. Use Natural Authentication strategies instead
154      * of this if that's what you need.
155      *
156      */
157     @Override
158     @Deprecated
159 	public Principal getPrincipalByPrincipalNameAndPassword(String principalName, String password) {
160         if (StringUtils.isBlank(principalName)) {
161             throw new RiceIllegalArgumentException("principalName is blank");
162         }
163 
164         //not validating password
165 
166         return getPrincipalByPrincipalName(principalName);
167     }
168 	
169     @Override
170 	public EntityPrivacyPreferences getEntityPrivacyPreferences(String entityId) {
171         if (StringUtils.isBlank(entityId)) {
172             throw new RiceIllegalArgumentException("entityId is blank");
173         }
174 
175         return getPrincipalDao().getEntityPrivacyPreferences(entityId);
176 	}
177 
178     @Override
179 	public Principal getPrincipal(String principalId) {
180         if (StringUtils.isBlank(principalId)) {
181             throw new RiceIllegalArgumentException("principalId is blank");
182         }
183 
184         final Principal edsInfo = getPrincipalDao().getPrincipal(principalId);
185             if (edsInfo != null) {
186 	        return edsInfo;
187 	    } else {
188 	        return super.getPrincipal(principalId);
189 	    }
190     }
191 
192     /**
193      * Gets a list of {@link org.kuali.rice.kim.api.identity.principal.Principal} from a string list of principalId.
194      *
195      * <p>
196      * This method will only return principals that exist.  It will return null if the none of the principals exist.
197      * </p>
198      *
199      * @param principalIds the unique id to retrieve the principal by. cannot be null.
200      * @return a list of {@link org.kuali.rice.kim.api.identity.principal.Principal} or null
201      * @throws org.kuali.rice.core.api.exception.RiceIllegalArgumentException if the principalId is blank
202      */
203     @Override
204     public List<Principal> getPrincipals(@WebParam(name = "principalIds") List<String> principalIds) {
205         List<Principal>  ret = new ArrayList<Principal>();
206         for(String p: principalIds) {
207             Principal principalInfo = getPrincipal(p);
208 
209             if (principalInfo != null) {
210                 ret.add(principalInfo) ;
211             }
212         }
213         return ret;
214     }
215 
216     @Override
217 	public Principal getPrincipalByPrincipalName(String principalName) {
218         if (StringUtils.isBlank(principalName)) {
219             throw new RiceIllegalArgumentException("principalName is blank");
220         }
221 
222         final Principal edsInfo = getPrincipalDao().getPrincipalByName(principalName);
223         if (edsInfo != null) {
224             return edsInfo;
225         } else {
226             return super.getPrincipalByPrincipalName(principalName);
227         }
228     }
229 
230     public void setPrincipalDao(LdapPrincipalDao principalDao) {
231         this.principalDao = principalDao;
232     }
233 
234     public LdapPrincipalDao getPrincipalDao() {
235         return principalDao;
236     } 
237 }