001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/providers/tags/sakai_2-4-1/sample/src/java/org/sakaiproject/provider/user/SampleUserDirectoryProvider.java $
003: * $Id: SampleUserDirectoryProvider.java 28732 2007-04-12 01:02:44Z ajpoland@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006, 2006, 2007 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.provider.user;
021:
022: import java.util.Collection;
023: import java.util.Hashtable;
024: import java.util.Iterator;
025: import java.util.Vector;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.sakaiproject.user.api.DisplayAdvisorUDP;
030: import org.sakaiproject.user.api.User;
031: import org.sakaiproject.user.api.UserDirectoryProvider;
032: import org.sakaiproject.user.api.UserEdit;
033: import org.sakaiproject.user.api.UserFactory;
034: import org.sakaiproject.user.api.UsersShareEmailUDP;
035:
036: /**
037: * <p>
038: * SampleUserDirectoryProvider is a samaple UserDirectoryProvider.
039: * </p>
040: */
041: public class SampleUserDirectoryProvider implements
042: UserDirectoryProvider, UsersShareEmailUDP, DisplayAdvisorUDP {
043: /** Our log (commons). */
044: private static Log M_log = LogFactory
045: .getLog(SampleUserDirectoryProvider.class);
046:
047: /**********************************************************************************************************************************************************************************************************************************************************
048: * Dependencies and their setter methods
049: *********************************************************************************************************************************************************************************************************************************************************/
050:
051: /** how many students to recognize (1.. this). */
052: protected int m_courseStudents = 500;
053:
054: /**
055: * Set how many students to recognize.
056: *
057: * @param count
058: * How many students to recognize.
059: */
060: public void setCourseStudents(String count) {
061: m_courseStudents = Integer.parseInt(count);
062: }
063:
064: /**********************************************************************************************************************************************************************************************************************************************************
065: * Init and Destroy
066: *********************************************************************************************************************************************************************************************************************************************************/
067:
068: /**
069: * Final initialization, once all dependencies are set.
070: */
071: public void init() {
072: try {
073: // fill a set of users
074: m_info = new Hashtable();
075: m_info.put("user1", new Info("user1", "One", "User",
076: "user1@local.host"));
077: m_info.put("user2", new Info("user2", "Two", "User",
078: "user2@local.host"));
079: m_info.put("user3", new Info("user3", "Three", "User",
080: "user3@local.host"));
081:
082: if (m_courseStudents > 0) {
083: for (int i = 1; i <= m_courseStudents; i++) {
084: m_info.put("student" + i, new Info("student" + i,
085: "" + i, "Student", "student" + i
086: + "@local.host"));
087: }
088: }
089:
090: m_info.put("instructor", new Info("instructor", "The",
091: "Instructor", "instructor@local.host"));
092: m_info.put("instructor1", new Info("instructor1", "The",
093: "Instructor1", "instructor1@local.host"));
094: m_info.put("instructor2", new Info("instructor2", "The",
095: "Instructor2", "instructor2@local.host"));
096: m_info.put("da1", new Info("da1", "Dept", "Admin",
097: "da1@local.host"));
098: m_info.put("ta", new Info("ta", "The",
099: "Teaching-Assistant", "ta@local.host"));
100:
101: M_log.info("init()");
102: } catch (Throwable t) {
103: M_log.warn(".init(): ", t);
104: }
105: }
106:
107: /**
108: * Returns to uninitialized state. You can use this method to release resources thet your Service allocated when Turbine shuts down.
109: */
110: public void destroy() {
111:
112: M_log.info("destroy()");
113:
114: } // destroy
115:
116: /**********************************************************************************************************************************************************************************************************************************************************
117: * UserDirectoryProvider implementation
118: *********************************************************************************************************************************************************************************************************************************************************/
119:
120: /** A collection of user ids/names. */
121: protected Hashtable m_info = null;
122:
123: protected class Info {
124: public String id;
125:
126: public String firstName;
127:
128: public String lastName;
129:
130: public String email;
131:
132: public Info(String id, String firstName, String lastName,
133: String email) {
134: this .id = id;
135: this .firstName = firstName;
136: this .lastName = lastName;
137: this .email = email;
138: }
139:
140: } // class info
141:
142: /**
143: * Construct.
144: */
145: public SampleUserDirectoryProvider() {
146: }
147:
148: /**
149: * See if a user by this id exists.
150: *
151: * @param userId
152: * The user id string.
153: * @return true if a user by this id exists, false if not.
154: */
155: public boolean userExists(String userId) {
156: if (userId == null)
157: return false;
158: if (userId.startsWith("test"))
159: return true;
160: if (m_info.containsKey(userId))
161: return true;
162:
163: return false;
164:
165: } // userExists
166:
167: /**
168: * Access a user object. Update the object with the information found.
169: *
170: * @param edit
171: * The user object (id is set) to fill in.
172: * @return true if the user object was found and information updated, false if not.
173: */
174: public boolean getUser(UserEdit edit) {
175: if (edit == null)
176: return false;
177: if (!userExists(edit.getEid()))
178: return false;
179:
180: Info info = (Info) m_info.get(edit.getEid());
181: if (info == null) {
182: edit.setFirstName(edit.getEid());
183: edit.setLastName(edit.getEid());
184: edit.setEmail(edit.getEid());
185: edit.setPassword(edit.getEid());
186: edit.setType("registered");
187: } else {
188: edit.setFirstName(info.firstName);
189: edit.setLastName(info.lastName);
190: edit.setEmail(info.email);
191: edit.setPassword("sakai");
192: edit.setType("registered");
193: }
194:
195: return true;
196:
197: } // getUser
198:
199: /**
200: * Access a collection of UserEdit objects; if the user is found, update the information, otherwise remove the UserEdit object from the collection.
201: *
202: * @param users
203: * The UserEdit objects (with id set) to fill in or remove.
204: */
205: public void getUsers(Collection users) {
206: for (Iterator i = users.iterator(); i.hasNext();) {
207: UserEdit user = (UserEdit) i.next();
208: if (!getUser(user)) {
209: i.remove();
210: }
211: }
212: }
213:
214: /**
215: * Find a user object who has this email address. Update the object with the information found. <br />
216: * Note: this method won't be used, because we are a UsersShareEmailUPD.<br />
217: * This is the sort of method to provide if your external source has only a single user for any email address.
218: *
219: * @param email
220: * The email address string.
221: * @return true if the user object was found and information updated, false if not.
222: */
223: public boolean findUserByEmail(UserEdit edit, String email) {
224: if ((edit == null) || (email == null))
225: return false;
226:
227: // assume a "@local.host"
228: int pos = email.indexOf("@local.host");
229: if (pos != -1) {
230: String id = email.substring(0, pos);
231: edit.setEid(id);
232: return getUser(edit);
233: }
234:
235: return false;
236:
237: } // findUserByEmail
238:
239: /**
240: * Find all user objects which have this email address.
241: *
242: * @param email
243: * The email address string.
244: * @param factory
245: * Use this factory's newUser() method to create all the UserEdit objects you populate and return in the return collection.
246: * @return Collection (UserEdit) of user objects that have this email address, or an empty Collection if there are none.
247: */
248: public Collection findUsersByEmail(String email, UserFactory factory) {
249: Collection rv = new Vector();
250:
251: // get a UserEdit to populate
252: UserEdit edit = factory.newUser();
253:
254: // assume a "@local.host"
255: int pos = email.indexOf("@local.host");
256: if (pos != -1) {
257: String id = email.substring(0, pos);
258: edit.setEid(id);
259: if (getUser(edit))
260: rv.add(edit);
261: }
262:
263: return rv;
264: }
265:
266: /**
267: * Authenticate a user / password. If the user edit exists it may be modified, and will be stored if...
268: *
269: * @param id
270: * The user id.
271: * @param edit
272: * The UserEdit matching the id to be authenticated (and updated) if we have one.
273: * @param password
274: * The password.
275: * @return true if authenticated, false if not.
276: */
277: public boolean authenticateUser(String userId, UserEdit edit,
278: String password) {
279: if ((userId == null) || (password == null))
280: return false;
281:
282: if (userId.startsWith("test"))
283: return userId.equals(password);
284: if (userExists(userId) && password.equals("sakai"))
285: return true;
286:
287: return false;
288:
289: } // authenticateUser
290:
291: /**
292: * Will this provider update user records on successfull authentication? If so, the UserDirectoryService will cause these updates to be stored.
293: *
294: * @return true if the user record may be updated after successfull authentication, false if not.
295: */
296: public boolean updateUserAfterAuthentication() {
297: return false;
298: }
299:
300: /**
301: * {@inheritDoc}
302: */
303: public void destroyAuthentication() {
304: }
305:
306: /**
307: * {@inheritDoc}
308: */
309: public boolean authenticateWithProviderFirst(String id) {
310: return false;
311: }
312:
313: /**
314: * {@inheritDoc}
315: */
316: public boolean createUserRecord(String id) {
317: return false;
318: }
319:
320: /**
321: * {@inheritDoc}
322: */
323: public String getDisplayId(User user) {
324: return user.getEid();
325: }
326:
327: /**
328: * {@inheritDoc}
329: */
330: public String getDisplayName(User user) {
331: // punt
332: return null;
333: }
334: }
|