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-24 13:52:51 -0700 (Thu, 24 Jul 2008) $'
10
 * '$Revision: 4159 $'
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.OrganizationUtil;
40
import edu.ucsb.nceas.metacat.util.RequestUtil;
41
import edu.ucsb.nceas.metacat.util.UtilException;
42

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

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

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

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

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

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

    
109
			try {
110
				String userName = request.getParameter("username");
111
				String organization = request.getParameter("organization");
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
					Vector<String> dnList = OrganizationUtil.getOrgDNs(organization);
122
					isLoggedIn = LDAPUtil.logUserIn(request, userName, organization, dnList, password);
123
					loginString = LDAPUtil.createLDAPString(userName, organization, dnList);
124
				}
125
				
126
				if (!isLoggedIn) {
127
					String errorMessage = "Could not log in as: " + loginString
128
							+ " .Please try again";
129
					processingErrors.add(errorMessage);
130
				}
131
			} catch (UtilException ue) {
132
				String errorMessage = "Problem in utility while "
133
					+ "processing authentication page: " + ue.getMessage();
134
				processingErrors.add(errorMessage);
135
				logMetacat.error(errorMessage);
136
			} 
137
			
138
			try {
139
				if (validationErrors.size() > 0 || processingErrors.size() > 0) {
140
					RequestUtil.clearRequestMessages(request);
141
					RequestUtil.setRequestFormErrors(request, validationErrors);
142
					RequestUtil.setRequestErrors(request, processingErrors);
143
					RequestUtil.forwardRequest(request, response, "/admin");
144
				} else {
145
					// Reload the main metacat configuration page
146
					processingSuccess.add("User logged in as: " + loginString);
147
					RequestUtil.clearRequestMessages(request);
148
					RequestUtil.setUserId(request, loginString);
149
					RequestUtil.setRequestSuccess(request, processingSuccess);
150
					RequestUtil.forwardRequest(request, response,
151
							"/admin?configureType=configure&processForm=false");
152
				}
153
			} catch (IOException ioe) {
154
				throw new AdminException("IO problem while processing LDAP "
155
						+ "properties page: " + ioe.getMessage());
156
			} catch (ServletException se) {
157
				throw new AdminException("problem forwarding request while "
158
						+ "processoing LDAP properties page: " + se.getMessage());
159
			}
160
		}
161
	}
162
	
163
	/**
164
	 * Validate the most important configuration options submitted by the user.
165
	 * 
166
	 * @return a vector holding error message for any fields that fail
167
	 *         validation.
168
	 */
169
	protected Vector<String> validateOptions(HttpServletRequest request) {
170
		Vector<String> errorVector = new Vector<String>();
171

    
172
		//TODO MCD validate options.
173

    
174
		return errorVector;
175
	}
176
}
(2-2/9)