Project

General

Profile

« Previous | Next » 

Revision 4590

Added by daigle about 16 years ago

Rename LDAPAdmin to AuthAdmin

View differences:

src/edu/ucsb/nceas/metacat/admin/LDAPAdmin.java
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$'
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.net.ConnectException;
31
import java.util.HashMap;
32
import java.util.Set;
33
import java.util.SortedMap;
34
import java.util.Vector;
35

  
36
import javax.servlet.ServletException;
37
import javax.servlet.http.HttpServletRequest;
38
import javax.servlet.http.HttpServletResponse;
39

  
40
import org.apache.log4j.Logger;
41

  
42
import edu.ucsb.nceas.metacat.AuthLdap;
43
import edu.ucsb.nceas.metacat.service.PropertyService;
44
import edu.ucsb.nceas.metacat.util.RequestUtil;
45
import edu.ucsb.nceas.utilities.FileUtil;
46
import edu.ucsb.nceas.utilities.GeneralPropertyException;
47
import edu.ucsb.nceas.utilities.MetaDataProperty;
48
import edu.ucsb.nceas.utilities.PropertiesMetaData;
49
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
50
import edu.ucsb.nceas.utilities.SortedProperties;
51
import edu.ucsb.nceas.utilities.StringUtil;
52

  
53
/**
54
 * Control the display of the LDAP configuration page and the processing
55
 * of the configuration values.
56
 */
57
public class LDAPAdmin extends MetaCatAdmin {
58

  
59
	private static LDAPAdmin ldapAdmin = null;
60
	private static Logger logMetacat = Logger.getLogger(LDAPAdmin.class);
61

  
62
	/**
63
	 * private constructor since this is a singleton
64
	 */
65
	private LDAPAdmin() {}
66

  
67
	/**
68
	 * Get the single instance of the MetaCatConfig.
69
	 * 
70
	 * @return the single instance of MetaCatConfig
71
	 */
72
	public static LDAPAdmin getInstance() {
73
		if (ldapAdmin == null) {
74
			ldapAdmin = new LDAPAdmin();
75
		}
76
		return ldapAdmin;
77
	}
78
	
79
	/**
80
	 * Handle configuration of the LDAP properties
81
	 * 
82
	 * @param request
83
	 *            the http request information
84
	 * @param response
85
	 *            the http response to be sent back to the client
86
	 */
87
	public void configureLDAP(HttpServletRequest request,
88
			HttpServletResponse response) throws AdminException {
89

  
90
		String processForm = request.getParameter("processForm");
91
		String formErrors = (String) request.getAttribute("formErrors");
92

  
93
		if (processForm == null || !processForm.equals("true") || formErrors != null) {
94
			// The servlet configuration parameters have not been set, or there
95
			// were form errors on the last attempt to configure, so redirect to
96
			// the web form for configuring metacat
97
			
98
			try {
99
				// Load the properties metadata file so that the JSP page can
100
				// use the metadata to construct the editing form
101
				PropertiesMetaData metadata = PropertyService.getLDAPMetaData();
102
				request.setAttribute("metadata", metadata);
103
				request.setAttribute("groupMap", metadata.getGroups());
104

  
105
				// add the list of ldap options and their values to the request
106
				Vector<String> propertyNames = PropertyService.getPropertyNamesByGroup("ldap");
107
				for (String name : propertyNames) {
108
					request.setAttribute(name, PropertyService.getProperty(name));
109
				} 
110

  
111
				// Check for any backup properties and apply them to the request.
112
				// These are properties from previous configurations. They keep
113
				// the user from having to re-enter all values when upgrading.
114
				// If this is a first time install, getBackupProperties will return
115
				// null.
116
				SortedProperties backupProperties = PropertyService.getLDAPBackupProperties();
117
				if (backupProperties != null) {
118
					Vector<String> backupKeys = backupProperties.getPropertyNames();
119
					for (String key : backupKeys) {
120
						String value = backupProperties.getProperty(key);
121
						if (value != null) {
122
							request.setAttribute(key, value);
123
						}
124
					}
125
				}
126
				// Forward the request to the JSP page
127
				RequestUtil.forwardRequest(request, response,
128
						"/admin/ldap-configuration.jsp");
129
			} catch (PropertyNotFoundException pnfe) {
130
				throw new AdminException("Problem getting property while initializing "
131
						+ "LDAP properties page: " + pnfe.getMessage());
132
			} catch (IOException ioe) {
133
				throw new AdminException("IO problem while initializing "
134
						+ "LDAP properties page:" + ioe.getMessage());
135
			} catch (ServletException se) {
136
				throw new AdminException("problem forwarding request while " 
137
						+ "initializing LDAP properties page: " + se.getMessage());
138
			}
139
		} else {
140
			// The configuration form is being submitted and needs to be
141
			// processed.
142
			Vector<String> processingSuccess = new Vector<String>();
143
			Vector<String> processingErrors = new Vector<String>();
144
			Vector<String> validationErrors = new Vector<String>();
145

  
146
			try {
147
				// For each property, check if it is changed and save it
148
				PropertiesMetaData ldapMetaData = PropertyService
149
						.getLDAPMetaData();
150

  
151
				// process the fields for the global options (group 1)
152
				SortedMap<Integer, MetaDataProperty> globalPropertyMap = ldapMetaData
153
						.getPropertiesInGroup(1);
154
				Set<Integer> globalPropertyIndexes = globalPropertyMap.keySet();
155
				for (Integer globalPropertyIndex : globalPropertyIndexes) {
156
					String globalPropertyKey = globalPropertyMap.get(
157
							globalPropertyIndex).getKey();
158
					PropertyService.checkAndSetProperty(request,
159
							globalPropertyKey);
160
				}
161

  
162
				// we need to write the options from memory to the properties
163
				// file
164
				PropertyService.persistProperties();
165

  
166
				// Validate that the options provided are legitimate. Note that
167
				// we've allowed them to persist their entries. As of this point
168
				// there is no other easy way to go back to the configure form
169
				// and preserve their entries.
170
				validationErrors.addAll(validateOptions(request));
171

  
172
				// Try to create data file and backup directories if
173
				// necessary.
174
				String backupDir = PropertyService.getBackupDir();
175
				try {
176
					FileUtil.createDirectory(backupDir);
177
				} catch (IOException ioe) {
178
					String errorString = "Could not create directory: " + backupDir
179
							+ " : " + ioe.getMessage();
180
					logMetacat.error(errorString);
181
					validationErrors.add(errorString);
182
				}
183

  
184
				// Write out the configurable properties to a backup file
185
				// outside the install directory.  Note that we allow them to
186
				// do this even if they have validation errors.  They will
187
				// need to go back and fix the errors before they can run metacat.
188
				PropertyService.persistLDAPBackupProperties(request.getSession()
189
						.getServletContext());
190
			
191
			} catch (GeneralPropertyException gpe) {
192
				String errorMessage = "Problem getting or setting property while "
193
					+ "processing LDAP properties page: " + gpe.getMessage();
194
				logMetacat.error(errorMessage);
195
				processingErrors.add(errorMessage);
196
			} 
197
			
198
			try {
199
				if (validationErrors.size() > 0 || processingErrors.size() > 0) {
200
					RequestUtil.clearRequestMessages(request);
201
					RequestUtil.setRequestFormErrors(request, validationErrors);
202
					RequestUtil.setRequestErrors(request, processingErrors);
203
					RequestUtil.forwardRequest(request, response, "/admin");
204
				} else {
205
					// Now that the options have been set, change the
206
					// 'ldapConfigured' option to 'true'
207
					PropertyService.setProperty("configutil.ldapConfigured",
208
							PropertyService.CONFIGURED);
209
					
210
					// Reload the main metacat configuration page
211
					processingSuccess.add("LDAP successfully configured");
212
					RequestUtil.clearRequestMessages(request);
213
					RequestUtil.setRequestSuccess(request, processingSuccess);
214
					RequestUtil.forwardRequest(request, response,
215
							"/admin?configureType=configure&processForm=false");
216
				}
217
			} catch (ServletException se) {
218
				throw new AdminException("problem forwarding request while "
219
						+ "processing LDAP properties page: " + se.getMessage());
220
			} catch (IOException ioe) {
221
				throw new AdminException("IO problem while processing LDAP "
222
						+ "properties page: " + ioe.getMessage());
223
			} catch (GeneralPropertyException gpe) {
224
				String errorMessage = "Problem getting or setting property while "
225
					+ "processing LDAP properties page: " + gpe.getMessage();
226
				logMetacat.error(errorMessage);
227
				processingErrors.add(errorMessage);
228
			}
229
		}
230
	}
231
	
232
	/**
233
	 * Validate the most important configuration options submitted by the user.
234
	 * 
235
	 * @return a vector holding error message for any fields that fail
236
	 *         validation.
237
	 */
238
	protected Vector<String> validateOptions(HttpServletRequest request) {
239
		Vector<String> errorVector = new Vector<String>();
240

  
241
		String adminUsers = request.getParameter("ldap.administrators");
242
		Vector<String> adminUserList = StringUtil.toVector(adminUsers, ':');
243

  
244
		try {
245
			AuthLdap authLdap = new AuthLdap();
246
			for (String adminUser : adminUserList) {
247
				try {
248
					authLdap.getAttributes(adminUser);
249
				} catch (ConnectException ce) {
250
					if (ce.getMessage() != null
251
							&& ce.getMessage().contains("NameNotFoundException")) {
252
						errorVector.add("User : " + adminUser + " is not in LDAP.");
253
					} else {
254
						errorVector.add("Connection error while verifying Metacat " + 
255
								"Administrators : " + ce.getMessage());
256
					}
257
				}
258
			}
259
		} catch (InstantiationException ie) {
260
			errorVector
261
					.add("Instantiation error while verifying Metacat Administrators : "
262
							+ ie.getMessage());
263
		}
264

  
265
		return errorVector;
266
	}
267
}
268 0

  
src/edu/ucsb/nceas/metacat/admin/AuthAdmin.java
27 27
package edu.ucsb.nceas.metacat.admin;
28 28

  
29 29
import java.io.IOException;
30
import java.net.ConnectException;
31
import java.util.Set;
32
import java.util.SortedMap;
30 33
import java.util.Vector;
31 34

  
32 35
import javax.servlet.ServletException;
......
35 38

  
36 39
import org.apache.log4j.Logger;
37 40

  
38
import edu.ucsb.nceas.metacat.util.LDAPUtil;
39
import edu.ucsb.nceas.metacat.util.OrganizationUtil;
41
import edu.ucsb.nceas.metacat.AuthSession;
42
import edu.ucsb.nceas.metacat.service.PropertyService;
40 43
import edu.ucsb.nceas.metacat.util.RequestUtil;
41
import edu.ucsb.nceas.metacat.util.UtilException;
44
import edu.ucsb.nceas.utilities.FileUtil;
45
import edu.ucsb.nceas.utilities.GeneralPropertyException;
46
import edu.ucsb.nceas.utilities.MetaDataProperty;
47
import edu.ucsb.nceas.utilities.PropertiesMetaData;
48
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
49
import edu.ucsb.nceas.utilities.SortedProperties;
50
import edu.ucsb.nceas.utilities.StringUtil;
42 51

  
43 52
/**
44
 * Control the display of the LDAP configuration page and the processing
53
 * Control the display of the Authentication configuration page and the processing
45 54
 * of the configuration values.
46 55
 */
47 56
public class AuthAdmin extends MetaCatAdmin {
......
55 64
	private AuthAdmin() {}
56 65

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

  
80 89
		String processForm = request.getParameter("processForm");
......
86 95
			// the web form for configuring metacat
87 96
			
88 97
			try {
98
				// Load the properties metadata file so that the JSP page can
99
				// use the metadata to construct the editing form
100
				PropertiesMetaData metadata = PropertyService.getAuthMetaData();
101
				request.setAttribute("metadata", metadata);
102
				request.setAttribute("groupMap", metadata.getGroups());
103

  
104
				// add the list of auth options and their values to the request
105
				Vector<String> propertyNames = PropertyService.getPropertyNamesByGroup("auth");
106
				for (String name : propertyNames) {
107
					request.setAttribute(name, PropertyService.getProperty(name));
108
				} 
109

  
110
				// Check for any backup properties and apply them to the request.
111
				// These are properties from previous configurations. They keep
112
				// the user from having to re-enter all values when upgrading.
113
				// If this is a first time install, getBackupProperties will return
114
				// null.
115
				SortedProperties backupProperties = PropertyService.getAuthBackupProperties();
116
				if (backupProperties != null) {
117
					Vector<String> backupKeys = backupProperties.getPropertyNames();
118
					for (String key : backupKeys) {
119
						String value = backupProperties.getProperty(key);
120
						if (value != null) {
121
							request.setAttribute(key, value);
122
						}
123
					}
124
				}
89 125
				// Forward the request to the JSP page
90 126
				RequestUtil.forwardRequest(request, response,
91
						"/admin/admin-login.jsp");
127
						"/admin/auth-configuration.jsp");
128
			} catch (PropertyNotFoundException pnfe) {
129
				throw new AdminException("Problem getting property while initializing "
130
						+ "LDAP properties page: " + pnfe.getMessage());
92 131
			} catch (IOException ioe) {
93 132
				throw new AdminException("IO problem while initializing "
94
						+ "user login page:" + ioe.getMessage());
133
						+ "LDAP properties page:" + ioe.getMessage());
95 134
			} catch (ServletException se) {
96
				throw new AdminException("problem forwarding request while "
97
						+ "initializing user login page: " + se.getMessage());
135
				throw new AdminException("problem forwarding request while " 
136
						+ "initializing LDAP properties page: " + se.getMessage());
98 137
			}
99 138
		} else {
100 139
			// The configuration form is being submitted and needs to be
......
102 141
			Vector<String> processingSuccess = new Vector<String>();
103 142
			Vector<String> processingErrors = new Vector<String>();
104 143
			Vector<String> validationErrors = new Vector<String>();
105
			
106
			String loginString = null;
107
			Boolean isLoggedIn = false;
108 144

  
109 145
			try {
110
				String userName = request.getParameter("username");
111
				String organization = request.getParameter("organization");
112
				String password = request.getParameter("password");
113
				
146
				// For each property, check if it is changed and save it
147
				PropertiesMetaData authMetaData = PropertyService
148
						.getAuthMetaData();
149

  
150
				// process the fields for the global options (group 1)
151
				SortedMap<Integer, MetaDataProperty> globalPropertyMap = authMetaData
152
						.getPropertiesInGroup(1);
153
				Set<Integer> globalPropertyIndexes = globalPropertyMap.keySet();
154
				for (Integer globalPropertyIndex : globalPropertyIndexes) {
155
					String globalPropertyKey = globalPropertyMap.get(
156
							globalPropertyIndex).getKey();
157
					PropertyService.checkAndSetProperty(request,
158
							globalPropertyKey);
159
				}
160

  
161
				// we need to write the options from memory to the properties
162
				// file
163
				PropertyService.persistProperties();
164

  
114 165
				// Validate that the options provided are legitimate. Note that
115 166
				// we've allowed them to persist their entries. As of this point
116 167
				// there is no other easy way to go back to the configure form
117 168
				// and preserve their entries.
118 169
				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);
170

  
171
				// Try to create data file and backup directories if
172
				// necessary.
173
				String backupDir = PropertyService.getBackupDir();
174
				try {
175
					FileUtil.createDirectory(backupDir);
176
				} catch (IOException ioe) {
177
					String errorString = "Could not create directory: " + backupDir
178
							+ " : " + ioe.getMessage();
179
					logMetacat.error(errorString);
180
					validationErrors.add(errorString);
124 181
				}
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();
182

  
183
				// Write out the configurable properties to a backup file
184
				// outside the install directory.  Note that we allow them to
185
				// do this even if they have validation errors.  They will
186
				// need to go back and fix the errors before they can run metacat.
187
				PropertyService.persistAuthBackupProperties(request.getSession()
188
						.getServletContext());
189
			
190
			} catch (GeneralPropertyException gpe) {
191
				String errorMessage = "Problem getting or setting property while "
192
					+ "processing LDAP properties page: " + gpe.getMessage();
193
				logMetacat.error(errorMessage);
134 194
				processingErrors.add(errorMessage);
135
				logMetacat.error(errorMessage);
136 195
			} 
137 196
			
138 197
			try {
......
142 201
					RequestUtil.setRequestErrors(request, processingErrors);
143 202
					RequestUtil.forwardRequest(request, response, "/admin");
144 203
				} else {
204
					// Now that the options have been set, change the
205
					// 'authConfigured' option to 'true'
206
					PropertyService.setProperty("configutil.authConfigured",
207
							PropertyService.CONFIGURED);
208
					
145 209
					// Reload the main metacat configuration page
146
					processingSuccess.add("User logged in as: " + loginString);
210
					processingSuccess.add("Authentication successfully configured");
147 211
					RequestUtil.clearRequestMessages(request);
148
					RequestUtil.setUserId(request, loginString);
149 212
					RequestUtil.setRequestSuccess(request, processingSuccess);
150 213
					RequestUtil.forwardRequest(request, response,
151 214
							"/admin?configureType=configure&processForm=false");
152 215
				}
216
			} catch (ServletException se) {
217
				throw new AdminException("problem forwarding request while "
218
						+ "processing LDAP properties page: " + se.getMessage());
153 219
			} catch (IOException ioe) {
154
				throw new AdminException("IO problem while processing LDAP "
220
				throw new AdminException("IO problem while processing Authentication "
155 221
						+ "properties page: " + ioe.getMessage());
156
			} catch (ServletException se) {
157
				throw new AdminException("problem forwarding request while "
158
						+ "processoing LDAP properties page: " + se.getMessage());
222
			} catch (GeneralPropertyException gpe) {
223
				String errorMessage = "Problem getting or setting property while "
224
					+ "processing Authentication properties page: " + gpe.getMessage();
225
				logMetacat.error(errorMessage);
226
				processingErrors.add(errorMessage);
159 227
			}
160 228
		}
161 229
	}
......
169 237
	protected Vector<String> validateOptions(HttpServletRequest request) {
170 238
		Vector<String> errorVector = new Vector<String>();
171 239

  
172
		//TODO MCD validate options.
240
		String adminUsers = request.getParameter("auth.administrators");
241
		Vector<String> adminUserList = StringUtil.toVector(adminUsers, ':');
173 242

  
243
		try {
244
			AuthSession authSession = new AuthSession();
245
			for (String adminUser : adminUserList) {
246
				try {
247
					authSession.getAttributes(adminUser);
248
				} catch (ConnectException ce) {
249
					if (ce.getMessage() != null
250
							&& ce.getMessage().contains("NameNotFoundException")) {
251
						errorVector.add("User : " + adminUser + " is not in LDAP.");
252
					} else {
253
						errorVector.add("Connection error while verifying Metacat " + 
254
								"Administrators : " + ce.getMessage());
255
					}
256
				}
257
			}
258
		} catch (InstantiationException ie) {
259
			errorVector.add("Instantiation error while verifying Metacat Administrators : "
260
							+ ie.getMessage());
261
		} catch (Exception e) {
262
			errorVector.add("Error while verifying Metacat Administrators : "
263
					+ e.getMessage());
264
		}
265

  
174 266
		return errorVector;
175 267
	}
176 268
}
177 269

  

Also available in: Unified diff