001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/providers/tags/sakai_2-4-1/imsent/src/java/org/sakaiproject/component/imsent/user/IMSEntUserDirectoryProvider.java $
003: * $Id: IMSEntUserDirectoryProvider.java 9187 2006-05-09 19:48:15Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.component.imsent.user;
021:
022: import java.sql.ResultSet;
023: import java.sql.SQLException;
024: import java.util.Collection;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.sakaiproject.db.api.SqlReader;
031: import org.sakaiproject.db.api.SqlService;
032: import org.sakaiproject.user.api.UserDirectoryProvider;
033: import org.sakaiproject.user.api.UserEdit;
034:
035: /**
036: * <p>
037: * IMSEntUserDirectoryProvider is a sample UserDirectoryProvider.
038: * </p>
039: */
040: public class IMSEntUserDirectoryProvider implements
041: UserDirectoryProvider {
042: /** Our log (commons). */
043: private static Log M_log = LogFactory
044: .getLog(IMSEntUserDirectoryProvider.class);
045:
046: /**********************************************************************************************************************************************************************************************************************************************************
047: * Dependencies and their setter methods
048: *********************************************************************************************************************************************************************************************************************************************************/
049: /** Dependency: SqlService */
050: protected SqlService m_sqlService = null;
051:
052: /**
053: * Dependency: SqlService.
054: *
055: * @param service
056: * The SqlService.
057: */
058: public void setSqlService(SqlService service) {
059: System.out.println("Setting Sql Service");
060: m_sqlService = service;
061: }
062:
063: /** Configuration: to run the ddl on init or not. */
064: // TODO: Set back to false
065: protected boolean m_autoDdl = true;
066:
067: /**
068: * Configuration: to run the ddl on init or not.
069: *
070: * @param value
071: * the auto ddl value.
072: */
073: public void setAutoDdl(String value) {
074: m_autoDdl = new Boolean(value).booleanValue();
075: }
076:
077: /**********************************************************************************************************************************************************************************************************************************************************
078: * Init and Destroy
079: *********************************************************************************************************************************************************************************************************************************************************/
080:
081: /**
082: * Final initialization, once all dependencies are set.
083: */
084: public void init() {
085: try {
086: M_log.info("init()");
087: } catch (Throwable t) {
088: System.out.println(this
089: + ".init() - failed attempting to log " + t);
090: M_log.warn(".init(): " + t);
091: }
092:
093: try {
094: // if we are auto-creating our schema, check and create
095: if (m_autoDdl && m_sqlService != null) {
096: m_sqlService.ddl(this .getClass().getClassLoader(),
097: "imsent_provider");
098: System.out.println("Back from autoddl");
099: }
100:
101: // Check to see if we are ready to run...
102: if (!isReady()) {
103: M_log.warn(".init(): Not properly initialized.");
104: }
105:
106: // Run our local unit tests
107: IMSEntProviderUnitTest.localUnitTests(this , null);
108: } catch (Throwable t) {
109: M_log.warn(".init(): ", t);
110: m_isReady = false;
111: }
112: // Check to see if we are ready to run...
113: if (!isReady()) {
114: M_log.warn(".init(): Not properly initialized.");
115: }
116:
117: } // init
118:
119: /**
120: * Returns to uninitialized state. You can use this method to release resources thet your Service allocated when Turbine shuts down.
121: */
122: public void destroy() {
123: M_log.info("destroy()");
124: } // destroy
125:
126: /**
127: * Determine if we are in a ready-to-go-state
128: */
129: private boolean m_isReady = true;
130:
131: private boolean m_firstCheck = true;
132:
133: private boolean isReady() {
134: // Only check things once
135: if (!m_firstCheck)
136: return m_isReady;
137: m_firstCheck = false;
138:
139: boolean retval = true;
140:
141: if (m_sqlService == null) {
142: M_log.warn("sqlService injection failed");
143: retval = false;
144: }
145:
146: // Check all other injections here
147:
148: // Return the value and set
149: m_isReady = retval;
150: return retval;
151: }
152:
153: /**********************************************************************************************************************************************************************************************************************************************************
154: * UserDirectoryProvider implementation
155: *********************************************************************************************************************************************************************************************************************************************************/
156:
157: public class SakaiIMSUser {
158: // From User
159: public String eMail = null;
160:
161: public String displayName = null;
162:
163: public String sortName = null;
164:
165: public String firstName = null;
166:
167: public String lastName = null;
168:
169: // From Resource
170: // public ResourceProperties getProperties;
171: public String id = null;
172:
173: // For use locally
174: public String password = null;
175:
176: // For debugging
177: public String toString() {
178: String rv = "SakaiIMSUser Email=" + eMail + " DisplayName="
179: + displayName + " SortName=" + sortName
180: + " FirstName=" + firstName + " LastName="
181: + lastName + " Id=" + id + " Password=" + password;
182: return rv;
183: }
184: }
185:
186: public SakaiIMSUser retrieveUser(final String userId,
187: boolean isEmail) {
188: String statement;
189:
190: if (userId == null)
191: return null;
192:
193: if (isEmail) {
194: // 1 2 3 4 5 6 7
195: statement = "select USERID,FN,SORT,PASSWORD,FAMILY,GIVEN,EMAIL from IMSENT_PERSON where EMAIL = ?";
196: } else {
197: statement = "select USERID,FN,SORT,PASSWORD,FAMILY,GIVEN,EMAIL from IMSENT_PERSON where USERID = ?";
198: }
199:
200: Object fields[] = new Object[1];
201: fields[0] = userId;
202:
203: System.out.println("SQL:" + statement);
204: List rv = m_sqlService.dbRead(statement, fields,
205: new SqlReader() {
206: public Object readSqlResultRecord(ResultSet result) {
207: try {
208: SakaiIMSUser rv = new SakaiIMSUser();
209: rv.id = result.getString(1);
210: rv.displayName = result.getString(2);
211: rv.sortName = result.getString(3);
212: if (rv.sortName == null)
213: rv.sortName = rv.displayName;
214: rv.password = result.getString(4);
215: rv.lastName = result.getString(5);
216: rv.firstName = result.getString(6);
217: rv.eMail = result.getString(7);
218: System.out.println("Inside reader " + rv);
219: return rv;
220: } catch (SQLException e) {
221: M_log.warn(this + ".authenticateUser: "
222: + userId + " : " + e);
223: return null;
224: }
225: }
226: });
227:
228: if ((rv != null) && (rv.size() > 0)) {
229: System.out.println("Returning ");
230: System.out.println(" " + (SakaiIMSUser) rv.get(0));
231: return (SakaiIMSUser) rv.get(0);
232: }
233: return null;
234: }
235:
236: /**
237: * Construct.
238: */
239: public IMSEntUserDirectoryProvider() {
240:
241: } // SampleUserDirectoryProvider
242:
243: /**
244: * See if a user by this id exists.
245: *
246: * @param userId
247: * The user id string.
248: * @return true if a user by this id exists, false if not.
249: */
250: public boolean userExists(String userId) {
251: if (!isReady())
252: return false;
253: if (userId == null)
254: return false;
255: System.out.println("userExists(" + userId + ")");
256: SakaiIMSUser rv = retrieveUser(userId, false);
257: return (rv != null);
258: } // userExists
259:
260: /**
261: * Copy the information from our internal structure into the Sakai User structure.
262: *
263: * @param edit
264: * @param imsUser
265: */
266: private void copyInfo(UserEdit edit, SakaiIMSUser imsUser) {
267: edit.setId(imsUser.id);
268: edit.setFirstName(imsUser.firstName);
269: edit.setLastName(imsUser.lastName);
270: edit.setEmail(imsUser.eMail);
271: edit.setPassword(imsUser.password);
272: // Sakai currently creates sortname from first and last name
273: edit.setType("imsent");
274: }
275:
276: /**
277: * Access a user object. Update the object with the information found.
278: *
279: * @param edit
280: * The user object (id is set) to fill in.
281: * @return true if the user object was found and information updated, false if not.
282: */
283: public boolean getUser(UserEdit edit) {
284: if (!isReady())
285: return false;
286: if (edit == null)
287: return false;
288: String userId = edit.getEid();
289:
290: System.out.println("getUser(" + userId + ")");
291: SakaiIMSUser rv = retrieveUser(userId, false);
292: if (rv == null)
293: return false;
294: copyInfo(edit, rv);
295: return true;
296: } // getUser
297:
298: /**
299: * Access a collection of UserEdit objects; if the user is found, update the information, otherwise remove the UserEdit object from the collection.
300: *
301: * @param users
302: * The UserEdit objects (with id set) to fill in or remove.
303: */
304: public void getUsers(Collection users) {
305: for (Iterator i = users.iterator(); i.hasNext();) {
306: UserEdit user = (UserEdit) i.next();
307: if (!getUser(user)) {
308: i.remove();
309: }
310: }
311: }
312:
313: /**
314: * Find a user object who has this email address. Update the object with the information found.
315: *
316: * @param email
317: * The email address string.
318: * @return true if the user object was found and information updated, false if not.
319: */
320: public boolean findUserByEmail(UserEdit edit, String email) {
321: if (!isReady())
322: return false;
323: if ((edit == null) || (email == null))
324: return false;
325:
326: System.out.println("findUserByEmail(" + email + ")");
327: SakaiIMSUser rv = retrieveUser(email, true);
328: if (rv == null)
329: return false;
330: copyInfo(edit, rv);
331: return true;
332:
333: } // findUserByEmail
334:
335: /**
336: * Authenticate a user / password. If the user edit exists it may be modified, and will be stored if...
337: *
338: * @param id
339: * The user id.
340: * @param edit
341: * The UserEdit matching the id to be authenticated (and updated) if we have one.
342: * @param password
343: * The password.
344: * @return true if authenticated, false if not.
345: */
346: public boolean authenticateUser(final String userId, UserEdit edit,
347: String password) {
348: if (!isReady())
349: return false;
350: if ((userId == null) || (password == null))
351: return false;
352: System.out.println("authenticateUser(" + userId + ")");
353: SakaiIMSUser rv = retrieveUser(userId, false);
354: if (rv == null)
355: return false;
356: return (password.compareTo(rv.password) == 0);
357: } // authenticateUser
358:
359: /**
360: * Will this provider update user records on successfull authentication? If so, the UserDirectoryService will cause these updates to be stored.
361: *
362: * @return true if the user record may be updated after successfull authentication, false if not.
363: */
364: public boolean updateUserAfterAuthentication() {
365: return false;
366: }
367:
368: /**
369: * {@inheritDoc}
370: */
371: public void destroyAuthentication() {
372: }
373:
374: /**
375: * {@inheritDoc}
376: */
377: public boolean authenticateWithProviderFirst(String id) {
378: return false;
379: }
380:
381: /**
382: * {@inheritDoc}
383: */
384: public boolean createUserRecord(String id) {
385: return false;
386: }
387:
388: } // SampleUserDirectoryProvider
|