Project

General

Profile

1 4080 daigle
/**
2
 *  '$RCSfile$'
3 4591 daigle
 *    Purpose: A Class that implements login methods
4 4080 daigle
 *  Copyright: 2008 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6 4591 daigle
 *    ors: Michael Daigle
7 4080 daigle
 *
8 4591 daigle
 *   '$or: daigle $'
9 4080 daigle
 *     '$Date$'
10
 * '$Revision$'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26
27
package edu.ucsb.nceas.metacat.admin;
28
29
import java.util.Vector;
30
31
import javax.servlet.http.HttpServletRequest;
32
import javax.servlet.http.HttpServletResponse;
33
34
import org.apache.log4j.Logger;
35
36 5015 daigle
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
37 4591 daigle
import edu.ucsb.nceas.metacat.util.AuthUtil;
38 4080 daigle
import edu.ucsb.nceas.metacat.util.RequestUtil;
39
40
/**
41 4591 daigle
 * Control the display of the login page
42 4080 daigle
 */
43 5027 daigle
public class LoginAdmin extends MetacatAdmin {
44 4080 daigle
45 4591 daigle
	private static LoginAdmin Admin = null;
46
	private static Logger logMetacat = Logger.getLogger(LoginAdmin.class);
47 4080 daigle
48
	/**
49
	 * private constructor since this is a singleton
50
	 */
51 4591 daigle
	private LoginAdmin() {}
52 4080 daigle
53
	/**
54 4591 daigle
	 * Get the single instance of LoginAdmin.
55 4080 daigle
	 *
56 4591 daigle
	 * @return the single instance of LoginAdmin
57 4080 daigle
	 */
58 4591 daigle
	public static LoginAdmin getInstance() {
59
		if (Admin == null) {
60
			Admin = new LoginAdmin();
61 4080 daigle
		}
62 4591 daigle
		return Admin;
63 4080 daigle
	}
64
65
	/**
66 4591 daigle
	 * Handle configuration of the Authentication properties
67 4080 daigle
	 *
68
	 * @param request
69
	 *            the http request information
70
	 * @param response
71
	 *            the http response to be sent back to the client
72
	 */
73
	public void authenticateUser(HttpServletRequest request,
74
			HttpServletResponse response) throws AdminException {
75
76
		String processForm = request.getParameter("processForm");
77
		String formErrors = (String) request.getAttribute("formErrors");
78
79
		if (processForm == null || !processForm.equals("true") || formErrors != null) {
80
			// The servlet configuration parameters have not been set, or there
81
			// were form errors on the last attempt to configure, so redirect to
82
			// the web form for configuring metacat
83
84
			try {
85 5010 daigle
				request.setAttribute("adminList", AuthUtil.getAdministrators());
86 4080 daigle
				// Forward the request to the JSP page
87
				RequestUtil.forwardRequest(request, response,
88 5027 daigle
						"/admin/admin-login.jsp", null);
89 5010 daigle
			} catch (MetacatUtilException mue) {
90 5030 daigle
				throw new AdminException("LoginAdmin.authenticateUser - Utility problem while " +
91
						"processing login page: " + mue.getMessage());
92 5076 daigle
			}
93 4080 daigle
		} else {
94
			// The configuration form is being submitted and needs to be
95
			// processed.
96
			Vector<String> processingSuccess = new Vector<String>();
97
			Vector<String> processingErrors = new Vector<String>();
98
			Vector<String> validationErrors = new Vector<String>();
99
100 4591 daigle
			String userName = "";
101 4080 daigle
102 4591 daigle
				userName = request.getParameter("username");
103 4080 daigle
				String password = request.getParameter("password");
104
105
				// Validate that the options provided are legitimate. Note that
106
				// we've allowed them to persist their entries. As of this point
107
				// there is no other easy way to go back to the configure form
108
				// and preserve their entries.
109
				validationErrors.addAll(validateOptions(request));
110
111
				if (validationErrors.size() == 0) {
112 4628 daigle
					try {
113 5076 daigle
						AuthUtil.logUserIn(request, userName, password);
114 4854 daigle
					} catch (MetacatUtilException ue) {
115 5030 daigle
						String errorMessage = "LoginAdmin.authenticateUser - Could not log in as: " + userName
116 4628 daigle
						+ " : " + ue.getMessage() + ". Please try again";
117
						processingErrors.add(errorMessage);
118
						logMetacat.error(errorMessage);
119
					}
120 4080 daigle
				}
121
122
			try {
123
				if (validationErrors.size() > 0 || processingErrors.size() > 0) {
124
					RequestUtil.clearRequestMessages(request);
125
					RequestUtil.setRequestFormErrors(request, validationErrors);
126
					RequestUtil.setRequestErrors(request, processingErrors);
127 5027 daigle
					RequestUtil.forwardRequest(request, response, "/admin", null);
128 4080 daigle
				} else {
129
					// Reload the main metacat configuration page
130 4591 daigle
					processingSuccess.add("User logged in as: " + userName);
131 4080 daigle
					RequestUtil.clearRequestMessages(request);
132 4591 daigle
					RequestUtil.setUserId(request, userName);
133 4080 daigle
					RequestUtil.setRequestSuccess(request, processingSuccess);
134
					RequestUtil.forwardRequest(request, response,
135 5027 daigle
							"/admin?configureType=configure&processForm=false", null);
136 4080 daigle
				}
137 5076 daigle
			} catch (MetacatUtilException mue) {
138 5030 daigle
				throw new AdminException("LoginAdmin.authenticateUser - IO problem while processing login page: "
139 5076 daigle
						+ mue.getMessage());
140
			}
141 4080 daigle
		}
142
	}
143
144
	/**
145
	 * Validate the most important configuration options submitted by the user.
146
	 *
147
	 * @return a vector holding error message for any fields that fail
148
	 *         validation.
149
	 */
150
	protected Vector<String> validateOptions(HttpServletRequest request) {
151
		Vector<String> errorVector = new Vector<String>();
152
153
		//TODO MCD validate options.
154
155
		return errorVector;
156
	}
157
}