Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements ldap configuration methods
4
 *  Copyright: 2008 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Michael Daigle
7
 *
8
 *   '$Author: daigle $'
9
 *     '$Date: 2008-07-07 13:49:18 -0700 (Mon, 07 Jul 2008) $'
10
 * '$Revision: 4084 $'
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.LDAPUtil;
39
import edu.ucsb.nceas.metacat.util.RequestUtil;
40
import edu.ucsb.nceas.metacat.util.UtilException;
41

    
42
/**
43
 * Control the display of the LDAP configuration page and the processing
44
 * of the configuration values.
45
 */
46
public class AuthAdmin extends MetaCatAdmin {
47

    
48
	private static AuthAdmin authAdmin = null;
49
	private static Logger logMetacat = Logger.getLogger(AuthAdmin.class);
50

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

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

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

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

    
108
			try {
109
				String 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 = LDAPUtil.getOrgDNs(organization);
121
					isLoggedIn = LDAPUtil.logUserIn(request, userName, organization, dnList, password);
122
					loginString = LDAPUtil.createLDAPString(userName, organization, dnList);
123
				}
124
				
125
				if (!isLoggedIn) {
126
					String errorMessage = "Could not log in as: " + loginString
127
							+ " .Please try again";
128
					processingErrors.add(errorMessage);
129
				}
130
			} catch (UtilException ue) {
131
				String errorMessage = "Problem in utility while "
132
					+ "processing authentication 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: " + loginString);
146
					RequestUtil.clearRequestMessages(request);
147
					RequestUtil.setUserId(request, loginString);
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 LDAP "
154
						+ "properties page: " + ioe.getMessage());
155
			} catch (ServletException se) {
156
				throw new AdminException("problem forwarding request while "
157
						+ "processoing LDAP properties 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
}
(2-2/8)