001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kim.service.impl;
017    
018    import java.sql.Timestamp;
019    import java.util.ArrayList;
020    import java.util.HashMap;
021    import java.util.List;
022    import java.util.Map;
023    
024    import org.apache.commons.lang.StringUtils;
025    import org.joda.time.DateTime;
026    import org.kuali.rice.core.api.criteria.CriteriaValue;
027    import org.kuali.rice.core.api.criteria.EqualPredicate;
028    import org.kuali.rice.core.api.criteria.Predicate;
029    import org.kuali.rice.core.api.criteria.QueryByCriteria;
030    import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
031    import org.kuali.rice.kim.api.identity.IdentityService;
032    import org.kuali.rice.kim.api.identity.entity.Entity;
033    import org.kuali.rice.kim.api.identity.entity.EntityDefault;
034    import org.kuali.rice.kim.api.identity.entity.EntityDefaultQueryResults;
035    import org.kuali.rice.kim.api.identity.principal.Principal;
036    import org.kuali.rice.kim.api.identity.privacy.EntityPrivacyPreferences;
037    import org.kuali.rice.kim.dao.LdapPrincipalDao;
038    import org.kuali.rice.kim.impl.identity.IdentityServiceImpl;
039    import org.kuali.rice.kim.impl.identity.entity.EntityBo;
040    
041    /**
042     * Implementation of {@link IdentityService} that communicates with and serves information
043     * from the UA Enterprise Directory Service.
044     * 
045     * 
046     * @author Kuali Rice Team (rice.collab@kuali.org)
047     */
048    public class LdapIdentityDelegateServiceImpl extends IdentityServiceImpl {
049        private LdapPrincipalDao principalDao;
050    
051        @Override
052            public Entity getEntity(String entityId) {
053            if (StringUtils.isBlank(entityId)) {
054                throw new RiceIllegalArgumentException("entityId is blank");
055            }
056    
057            Entity edsInfo = getPrincipalDao().getEntity(entityId);
058            if (edsInfo != null) {
059                return edsInfo;
060            } else {
061                return super.getEntity(entityId);
062            }
063            }
064            
065            /**
066             * Overridden to populate this information from the LdapPrincipalDao
067             */
068        @Override
069            public Entity getEntityByPrincipalId(String principalId) {
070            if (StringUtils.isBlank(principalId)) {
071                throw new RiceIllegalArgumentException("principalId is blank");
072            }
073    
074            Entity edsInfo = getPrincipalDao().getEntityByPrincipalId(principalId);
075            if (edsInfo != null) {
076                return edsInfo;
077            } else {
078                return super.getEntityByPrincipalId(principalId);
079            }
080            }
081            
082        @Override
083            public Entity getEntityByPrincipalName(String principalName) {
084            if (StringUtils.isBlank(principalName)) {
085                throw new RiceIllegalArgumentException("principalName is blank");
086            }
087    
088            final Entity edsInfo = getPrincipalDao().getEntityByPrincipalName(principalName);
089            if (edsInfo != null) {
090                return edsInfo;
091            } else {
092                return super.getEntityByPrincipalName(principalName);
093            }
094            }
095            
096        @Override
097            public EntityDefault getEntityDefault(String entityId) {
098            if (StringUtils.isBlank(entityId)) {
099                throw new RiceIllegalArgumentException("entityId is blank");
100            }
101    
102            EntityDefault edsInfo = getPrincipalDao().getEntityDefault(entityId);
103            if (edsInfo != null) {
104                return edsInfo;
105            } 
106            else {
107                return super.getEntityDefault(entityId);
108            }
109            }
110            
111        @Override
112            public EntityDefault getEntityDefaultByPrincipalId(String principalId) {
113            if (StringUtils.isBlank(principalId)) {
114                throw new RiceIllegalArgumentException("principalId is blank");
115            }
116    
117            final EntityDefault retval = getPrincipalDao().getEntityDefaultByPrincipalId(principalId);
118            if (retval != null) {
119                return retval;
120            }
121            else {
122                return super.getEntityDefaultByPrincipalId(principalId);
123            }
124            }
125            
126        @Override
127            public EntityDefault getEntityDefaultByPrincipalName(String principalName) {
128            if (StringUtils.isBlank(principalName)) {
129                throw new RiceIllegalArgumentException("principalName is blank");
130            }
131    
132            final EntityDefault retval = getPrincipalDao().getEntityDefaultByPrincipalName(principalName);
133            if (retval != null) {
134                return retval;
135            }
136            else {
137                return super.getEntityDefaultByPrincipalName(principalName);
138            }
139            }
140        
141            
142        private static <U extends CriteriaValue<?>> Object getVal(U toConv) {
143            Object o = toConv.getValue();
144            if (o instanceof DateTime) {
145                return new Timestamp(((DateTime) o).getMillis());
146            }
147            return o;
148        }
149        
150        /**
151         * Password lookups not supported by EDS. Use Natural Authentication strategies instead
152         * of this if that's what you need.
153         *
154         */
155        @Override
156        @Deprecated
157            public Principal getPrincipalByPrincipalNameAndPassword(String principalName, String password) {
158            if (StringUtils.isBlank(principalName)) {
159                throw new RiceIllegalArgumentException("principalName is blank");
160            }
161    
162            //not validating password
163    
164            return getPrincipalByPrincipalName(principalName);
165        }
166            
167        @Override
168            public EntityPrivacyPreferences getEntityPrivacyPreferences(String entityId) {
169            if (StringUtils.isBlank(entityId)) {
170                throw new RiceIllegalArgumentException("entityId is blank");
171            }
172    
173            return getPrincipalDao().getEntityPrivacyPreferences(entityId);
174            }
175    
176        @Override
177            public Principal getPrincipal(String principalId) {
178            if (StringUtils.isBlank(principalId)) {
179                throw new RiceIllegalArgumentException("principalId is blank");
180            }
181    
182            final Principal edsInfo = getPrincipalDao().getPrincipal(principalId);
183                if (edsInfo != null) {
184                    return edsInfo;
185                } else {
186                    return super.getPrincipal(principalId);
187                }
188        }
189    
190        @Override
191            public Principal getPrincipalByPrincipalName(String principalName) {
192            if (StringUtils.isBlank(principalName)) {
193                throw new RiceIllegalArgumentException("principalName is blank");
194            }
195    
196            final Principal edsInfo = getPrincipalDao().getPrincipalByName(principalName);
197            if (edsInfo != null) {
198                return edsInfo;
199            } else {
200                return super.getPrincipalByPrincipalName(principalName);
201            }
202        }
203    
204        public void setPrincipalDao(LdapPrincipalDao principalDao) {
205            this.principalDao = principalDao;
206        }
207    
208        public LdapPrincipalDao getPrincipalDao() {
209            return principalDao;
210        } 
211    }