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: 2009-08-04 14:32:58 -0700 (Tue, 04 Aug 2009) $'
10
 * '$Revision: 5015 $'
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.io.IOException;
30
import java.util.Vector;
31

    
32
import javax.servlet.ServletException;
33
import javax.servlet.http.HttpServletRequest;
34
import javax.servlet.http.HttpServletResponse;
35

    
36
import org.apache.log4j.Logger;
37

    
38
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
39
import edu.ucsb.nceas.metacat.util.AuthUtil;
40
import edu.ucsb.nceas.metacat.util.RequestUtil;
41

    
42
/**
43
 * Control the display of the login page 
44
 */
45
public class LoginAdmin extends MetaCatAdmin {
46

    
47
	private static LoginAdmin Admin = null;
48
	private static Logger logMetacat = Logger.getLogger(LoginAdmin.class);
49

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

    
55
	/**
56
	 * Get the single instance of LoginAdmin.
57
	 * 
58
	 * @return the single instance of LoginAdmin
59
	 */
60
	public static LoginAdmin getInstance() {
61
		if (Admin == null) {
62
			Admin = new LoginAdmin();
63
		}
64
		return Admin;
65
	}
66
	
67
	/**
68
	 * Handle configuration of the Authentication properties
69
	 * 
70
	 * @param request
71
	 *            the http request information
72
	 * @param response
73
	 *            the http response to be sent back to the client
74
	 */
75
	public void authenticateUser(HttpServletRequest request,
76
			HttpServletResponse response) throws AdminException {
77

    
78
		String processForm = request.getParameter("processForm");
79
		String formErrors = (String) request.getAttribute("formErrors");
80

    
81
		if (processForm == null || !processForm.equals("true") || formErrors != null) {
82
			// The servlet configuration parameters have not been set, or there
83
			// were form errors on the last attempt to configure, so redirect to
84
			// the web form for configuring metacat
85
			
86
			try {
87
				request.setAttribute("adminList", AuthUtil.getAdministrators());
88
				// Forward the request to the JSP page
89
				RequestUtil.forwardRequest(request, response,
90
						"/admin/admin-login.jsp");
91
			} catch (MetacatUtilException mue) {
92
				throw new AdminException("Utility problem while processing login page: " 
93
						+ mue.getMessage());
94
			} catch (IOException ioe) {
95
				throw new AdminException("IO problem while initializing "
96
						+ "user login page:" + ioe.getMessage());
97
			} catch (ServletException se) {
98
				throw new AdminException("problem forwarding request while "
99
						+ "initializing user login page: " + se.getMessage());
100
			}
101
		} else {
102
			// The configuration form is being submitted and needs to be
103
			// processed.
104
			Vector<String> processingSuccess = new Vector<String>();
105
			Vector<String> processingErrors = new Vector<String>();
106
			Vector<String> validationErrors = new Vector<String>();
107
			
108
			Boolean isLoggedIn = false;
109
			String userName = "";
110

    
111
				userName = request.getParameter("username");
112
				String password = request.getParameter("password");
113
				
114
				// Validate that the options provided are legitimate. Note that
115
				// we've allowed them to persist their entries. As of this point
116
				// there is no other easy way to go back to the configure form
117
				// and preserve their entries.
118
				validationErrors.addAll(validateOptions(request));
119
				
120
				if (validationErrors.size() == 0) {
121
					try {
122
						isLoggedIn = AuthUtil.logUserIn(request, userName, password);
123
					} catch (MetacatUtilException ue) {
124
						String errorMessage = "Could not log in as: " + userName
125
						+ " : " + ue.getMessage() + ". Please try again";
126
						processingErrors.add(errorMessage);
127
						logMetacat.error(errorMessage);
128
					} 
129
				}
130
			
131
			try {
132
				if (validationErrors.size() > 0 || processingErrors.size() > 0) {
133
					RequestUtil.clearRequestMessages(request);
134
					RequestUtil.setRequestFormErrors(request, validationErrors);
135
					RequestUtil.setRequestErrors(request, processingErrors);
136
					RequestUtil.forwardRequest(request, response, "/admin");
137
				} else {
138
					// Reload the main metacat configuration page
139
					processingSuccess.add("User logged in as: " + userName);
140
					RequestUtil.clearRequestMessages(request);
141
					RequestUtil.setUserId(request, userName);
142
					RequestUtil.setRequestSuccess(request, processingSuccess);
143
					RequestUtil.forwardRequest(request, response,
144
							"/admin?configureType=configure&processForm=false");
145
				}
146
			} catch (IOException ioe) {
147
				throw new AdminException("IO problem while processing login page: " 
148
						+ ioe.getMessage());
149
			} catch (ServletException se) {
150
				throw new AdminException("problem forwarding request while "
151
						+ "processoing login page: " + se.getMessage());
152
			}
153
		}
154
	}
155
	
156
	/**
157
	 * Validate the most important configuration options submitted by the user.
158
	 * 
159
	 * @return a vector holding error message for any fields that fail
160
	 *         validation.
161
	 */
162
	protected Vector<String> validateOptions(HttpServletRequest request) {
163
		Vector<String> errorVector = new Vector<String>();
164

    
165
		//TODO MCD validate options.
166

    
167
		return errorVector;
168
	}
169
}
(6-6/10)