001: /*
002: * FlowEPersonUtils.java
003: *
004: * Version: $Revision: 1.3 $
005: *
006: * Date: $Date: 2006/07/13 23:20:54 $
007: *
008: * Copyright (c) 2002, Hewlett-Packard Company and Massachusetts
009: * Institute of Technology. All rights reserved.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions are
013: * met:
014: *
015: * - Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * - Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in the
020: * documentation and/or other materials provided with the distribution.
021: *
022: * - Neither the name of the Hewlett-Packard Company nor the name of the
023: * Massachusetts Institute of Technology nor the names of their
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
030: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
032: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
033: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
034: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
036: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
037: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
038: * DAMAGE.
039: */
040:
041: package org.dspace.app.xmlui.aspect.administrative;
042:
043: import java.io.IOException;
044: import java.sql.SQLException;
045: import java.util.Map;
046:
047: import javax.mail.MessagingException;
048:
049: import org.apache.cocoon.environment.Request;
050: import org.dspace.app.xmlui.utils.AuthenticationUtil;
051: import org.dspace.app.xmlui.wing.Message;
052: import org.dspace.authorize.AuthorizeException;
053: import org.dspace.core.Context;
054: import org.dspace.eperson.AccountManager;
055: import org.dspace.eperson.EPerson;
056: import org.dspace.eperson.EPersonDeletionException;
057:
058: /**
059: * Utility methods to processes actions on EPeople. These methods are used
060: * exclusivly from the administrative flow scripts.
061: *
062: * @author scott phillips
063: */
064: public class FlowEPersonUtils {
065:
066: /** Language Strings */
067: private static final Message T_add_eperson_success_notice = new Message(
068: "default",
069: "xmlui.administrative.FlowEPersonUtils.add_eperson_success_notice");
070:
071: private static final Message T_edit_eperson_success_notice = new Message(
072: "default",
073: "xmlui.administrative.FlowEPersonUtils.edit_eperson_success_notice");
074:
075: private static final Message T_reset_password_success_notice = new Message(
076: "default",
077: "xmlui.administrative.FlowEPersonUtils.reset_password_success_notice");
078:
079: private static final Message t_delete_eperson_success_notice = new Message(
080: "default",
081: "xmlui.administrative.FlowEPersonUtils.delete_eperson_success_notice");
082:
083: /**
084: * Add a new eperson. This method will check that the email address,
085: * first name, and last name are non empty. Also a check is preformed
086: * to see if the requested email address is allready in use by another
087: * user.
088: *
089: * @param context The current DSpace context
090: * @param request The HTTP request parameters
091: * @param objectModel Cocoon's object model
092: * @return A process result's object.
093: */
094: public static FlowResult processAddEPerson(Context context,
095: Request request, Map objectModel) throws SQLException,
096: AuthorizeException {
097: FlowResult result = new FlowResult();
098: result.setContinue(false); // default to no continue
099:
100: // Get all our request parameters
101: String email = request.getParameter("email_address");
102: String first = request.getParameter("first_name");
103: String last = request.getParameter("last_name");
104: String phone = request.getParameter("phone");
105: boolean login = (request.getParameter("can_log_in") != null) ? true
106: : false;
107: boolean certificate = (request.getParameter("certificate") != null) ? true
108: : false;
109:
110: // If we have errors, the form needs to be resubmitted to fix those problems
111: if (email.length() == 0)
112: result.addError("email_address");
113: if (first.length() == 0)
114: result.addError("first_name");
115: if (last.length() == 0)
116: result.addError("last_name");
117:
118: // Check if the email address is all ready being used.
119: EPerson potentialDupicate = EPerson.findByEmail(context, email);
120: if (potentialDupicate != null) {
121: // special error that the front end knows about.
122: result.addError("eperson_email_key");
123: }
124:
125: // No errors, so we try to create the EPerson from the data provided
126: if (result.getErrors() == null) {
127: EPerson newPerson = AuthenticationUtil.createNewEperson(
128: objectModel, email);
129:
130: newPerson.setFirstName(first);
131: newPerson.setLastName(last);
132: newPerson.setMetadata("phone", phone);
133: newPerson.setCanLogIn(login);
134: newPerson.setRequireCertificate(certificate);
135: newPerson.setSelfRegistered(false);
136:
137: newPerson.update();
138: context.commit();
139: // success
140: result.setContinue(true);
141: result.setOutcome(true);
142: result.setMessage(T_add_eperson_success_notice);
143: result.setParameter("epersonID", newPerson.getID());
144: }
145:
146: return result;
147: }
148:
149: /**
150: * Edit an eperson's metadata, the email address, first name, and last name are all
151: * required. The user's email address can be updated but it must remain unique, if
152: * the email address allready exists then the an error is produced.
153: *
154: * @param context The current DSpace context
155: * @param request The HTTP request parameters
156: * @param objectModel Cocoon's object model
157: * @param epersonID The unique id of the eperson being edited.
158: * @return A process result's object.
159: */
160: public static FlowResult processEditEPerson(Context context,
161: Request request, Map ObjectModel, int epersonID)
162: throws SQLException, AuthorizeException {
163:
164: FlowResult result = new FlowResult();
165: result.setContinue(false); // default to failure
166:
167: // Get all our request parameters
168: String email = request.getParameter("email_address");
169: String first = request.getParameter("first_name");
170: String last = request.getParameter("last_name");
171: String phone = request.getParameter("phone");
172: boolean login = (request.getParameter("can_log_in") != null) ? true
173: : false;
174: boolean certificate = (request.getParameter("certificate") != null) ? true
175: : false;
176:
177: // If we have errors, the form needs to be resubmitted to fix those problems
178: if (email.length() == 0)
179: result.addError("email_address");
180: if (first.length() == 0)
181: result.addError("first_name");
182: if (last.length() == 0)
183: result.addError("last_name");
184:
185: // No errors, so we edit the EPerson with the data provided
186: if (result.getErrors() == null) {
187: // Grab the person in question
188: EPerson personModified = EPerson.find(context, epersonID);
189:
190: // Make sure the email address we are changing to is unique
191: if (personModified.getEmail() != email) {
192: EPerson potentialDupicate = EPerson.findByEmail(
193: context, email);
194:
195: if (potentialDupicate == null) {
196: personModified.setEmail(email);
197: } else if (potentialDupicate != personModified) {
198: // set a special field in error so that the transformer can display a pretty error.
199: result.addError("eperson_email_key");
200: return result;
201: }
202: }
203: if (personModified.getFirstName() != first) {
204: personModified.setFirstName(first);
205: }
206: if (personModified.getLastName() != last) {
207: personModified.setLastName(last);
208: }
209: if (personModified.getMetadata("phone") != phone) {
210: personModified.setMetadata("phone", phone);
211: }
212: personModified.setCanLogIn(login);
213: personModified.setRequireCertificate(certificate);
214:
215: personModified.update();
216: context.commit();
217:
218: result.setContinue(true);
219: result.setOutcome(true);
220: // FIXME: rename this message
221: result.setMessage(T_edit_eperson_success_notice);
222: }
223:
224: // Everything was fine
225: return result;
226: }
227:
228: /**
229: * Send the user a forgot password email message. The message will
230: * contain a token that the user can use to login and pick a new password.
231: *
232: * @param context The current DSpace context
233: * @param epersonID The unique id of the eperson being edited.
234: * @return A process result's object.
235: */
236: public static FlowResult processResetPassword(Context context,
237: int epersonID) throws IOException, MessagingException,
238: SQLException, AuthorizeException {
239: EPerson eperson = EPerson.find(context, epersonID);
240:
241: // Note, this may throw an error is the email is bad.
242: AccountManager.sendForgotPasswordInfo(context, eperson
243: .getEmail());
244:
245: FlowResult result = new FlowResult();
246: result.setContinue(true);
247: result.setOutcome(true);
248: result.setMessage(T_reset_password_success_notice);
249: return result;
250: }
251:
252: /**
253: * Delete the epeople specified by the epeopleIDs parameter. This assumes that the
254: * detetion has been confirmed.
255: *
256: * @param context The current DSpace context
257: * @param epeopleIDs The unique id of the eperson being edited.
258: * @return A process result's object.
259: */
260: public static FlowResult processDeleteEPeople(Context context,
261: String[] epeopleIDs) throws NumberFormatException,
262: SQLException, AuthorizeException, EPersonDeletionException {
263: FlowResult result = new FlowResult();
264:
265: for (String id : epeopleIDs) {
266: EPerson personDeleted = EPerson.find(context, Integer
267: .valueOf(id));
268: personDeleted.delete();
269: }
270:
271: result.setOutcome(true);
272: result.setMessage(t_delete_eperson_success_notice);
273:
274: return result;
275: }
276: }
|