Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements login methods
4
 *  Copyright: 2008 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    ors: Michael Daigle
7
 *
8
 *   '$or: daigle $'
9
 *     '$Date: 2011-06-07 09:53:46 -0700 (Tue, 07 Jun 2011) $'
10
 * '$Revision: 6124 $'
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
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
37
import edu.ucsb.nceas.metacat.util.AuthUtil;
38
import edu.ucsb.nceas.metacat.util.RequestUtil;
39

    
40
/**
41
 * Control the display of the login page 
42
 */
43
public class LoginAdmin extends MetacatAdmin {
44

    
45
	private static LoginAdmin Admin = null;
46
	private static Logger logMetacat = Logger.getLogger(LoginAdmin.class);
47

    
48
	/**
49
	 * private constructor since this is a singleton
50
	 */
51
	private LoginAdmin() {}
52

    
53
	/**
54
	 * Get the single instance of LoginAdmin.
55
	 * 
56
	 * @return the single instance of LoginAdmin
57
	 */
58
	public static LoginAdmin getInstance() {
59
		if (Admin == null) {
60
			Admin = new LoginAdmin();
61
		}
62
		return Admin;
63
	}
64
	
65
	/**
66
	 * Handle configuration of the Authentication properties
67
	 * 
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
				request.setAttribute("adminList", AuthUtil.getAdministrators());
86
				// Forward the request to the JSP page
87
				RequestUtil.forwardRequest(request, response,
88
						"/admin/admin-login.jsp", null);
89
			} catch (MetacatUtilException mue) {
90
				throw new AdminException("LoginAdmin.authenticateUser - Utility problem while " + 
91
						"processing login page: " + mue.getMessage());
92
			} 
93
		} 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
			String userName = "";
101

    
102
				userName = request.getParameter("username");
103
				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
					try {
113
						AuthUtil.logUserIn(request, userName, password);
114
					} catch (MetacatUtilException ue) {
115
						String errorMessage = "LoginAdmin.authenticateUser - Could not log in as: " + userName
116
						+ " : " + ue.getMessage() + ". Please try again";
117
						processingErrors.add(errorMessage);
118
						logMetacat.error(errorMessage);
119
					} 
120
				}
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
					RequestUtil.forwardRequest(request, response, "/admin", null);
128
				} else {
129
					// Reload the main metacat configuration page
130
					processingSuccess.add("User logged in as: " + userName);
131
					RequestUtil.clearRequestMessages(request);
132
					RequestUtil.setUserId(request, userName);
133
					RequestUtil.setRequestSuccess(request, processingSuccess);
134
					RequestUtil.forwardRequest(request, response,
135
							"/admin?configureType=configure&processForm=false", null);
136
				}
137
			} catch (MetacatUtilException mue) {
138
				throw new AdminException("LoginAdmin.authenticateUser - IO problem while processing login page: " 
139
						+ mue.getMessage());
140
			} 
141
		}
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
}
(6-6/10)