Project

General

Profile

« Previous | Next » 

Revision 4591

Added by daigle about 16 years ago

This handles the configuration login form

View differences:

src/edu/ucsb/nceas/metacat/admin/LoginAdmin.java
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$'
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.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.util.AuthUtil;
39
import edu.ucsb.nceas.metacat.util.RequestUtil;
40
import edu.ucsb.nceas.metacat.util.UtilException;
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
				// Forward the request to the JSP page
88
				RequestUtil.forwardRequest(request, response,
89
						"/admin/admin-login.jsp");
90
			} catch (IOException ioe) {
91
				throw new AdminException("IO problem while initializing "
92
						+ "user login page:" + ioe.getMessage());
93
			} catch (ServletException se) {
94
				throw new AdminException("problem forwarding request while "
95
						+ "initializing user login page: " + se.getMessage());
96
			}
97
		} else {
98
			// The configuration form is being submitted and needs to be
99
			// processed.
100
			Vector<String> processingSuccess = new Vector<String>();
101
			Vector<String> processingErrors = new Vector<String>();
102
			Vector<String> validationErrors = new Vector<String>();
103
			
104
//			String loginString = null;
105
			Boolean isLoggedIn = false;
106
			String userName = "";
107

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

  
171
		//TODO MCD validate options.
172

  
173
		return errorVector;
174
	}
175
}
0 176

  

Also available in: Unified diff