KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > install > forms > ConfigureSuperUserForm


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.install.forms;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Calendar JavaDoc;
24 import java.util.List JavaDoc;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.struts.Globals;
31 import org.apache.struts.action.ActionErrors;
32 import org.apache.struts.action.ActionMapping;
33 import org.apache.struts.action.ActionMessage;
34
35 import com.sslexplorer.core.CoreServlet;
36 import com.sslexplorer.core.UserDatabaseManager;
37 import com.sslexplorer.jdbc.JDBCUserDatabase;
38 import com.sslexplorer.properties.Property;
39 import com.sslexplorer.properties.impl.realms.RealmKey;
40 import com.sslexplorer.realms.DefaultRealm;
41 import com.sslexplorer.realms.Realm;
42 import com.sslexplorer.security.User;
43 import com.sslexplorer.security.UserDatabase;
44 import com.sslexplorer.wizard.AbstractWizardSequence;
45 import com.sslexplorer.wizard.forms.DefaultWizardForm;
46
47
48 /**
49  * Wizard for for selecting and configuring the super user account to
50  * use.
51  *
52  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
53  */

54 public class ConfigureSuperUserForm extends DefaultWizardForm {
55
56     final static Log log = LogFactory.getLog(ConfigureSuperUserForm.class);
57
58     /**
59      * Super user password
60      */

61     public final static String JavaDoc ATTR_SUPER_USER_PASSWORD = "superUserPassword";
62     
63     /**
64      * Super user password
65      */

66     public final static String JavaDoc ATTR_SUPER_USER = "account";
67     
68     /**
69      * Super user email
70      */

71     public final static String JavaDoc ATTR_SUPER_USER_EMAIL = "email";
72
73     // Statics
74
final static String JavaDoc DUMMY_PASSWORD = "**********";
75
76     // Private instance variables
77
private String JavaDoc confirmSuperUserPassword;
78     private String JavaDoc superUserPassword;
79     private String JavaDoc account;
80     private String JavaDoc email;
81     private List JavaDoc<String JavaDoc> usernames;
82     private String JavaDoc databaseType;
83     private UserDatabase userDatabase;
84
85     /**
86      * Constructor.
87      *
88      */

89     public ConfigureSuperUserForm() {
90         super(true, true, "/WEB-INF/jsp/content/install/configureSuperUser.jspf", "account", false, false, "configureSuperUser",
91             "install", "installation.configureSuperUser", 3);
92     }
93
94     /*
95      * (non-Javadoc)
96      *
97      * @see com.sslexplorer.wizard.forms.AbstractWizardForm#init(com.sslexplorer.wizard.AbstractWizardSequence)
98      */

99     public void init(AbstractWizardSequence sequence, HttpServletRequest JavaDoc request) throws Exception JavaDoc {
100         databaseType = (String JavaDoc) sequence.getAttribute(SelectUserDatabaseForm.ATTR_USER_DATABASE, JDBCUserDatabase.DATABASE_TYPE);
101         userDatabase = (UserDatabase) sequence.getAttribute(SelectUserDatabaseForm.ATTR_USER_DATABASE_INSTANCE, null);
102         
103         // Temporarily load the selected user database
104
try {
105             if(!userDatabase.isOpen()) {
106                 log.info("Opening user databse");
107                 Calendar JavaDoc now = Calendar.getInstance();
108                 Realm realm = new DefaultRealm((String JavaDoc)sequence.getAttribute(SelectUserDatabaseForm.ATTR_USER_DATABASE, null), 1, UserDatabaseManager.DEFAULT_REALM_NAME, UserDatabaseManager.DEFAULT_REALM_DESCRIPTION, now, now);
109                 userDatabase.open(CoreServlet.getServlet(), realm);
110             }
111             
112             String JavaDoc bestGuess = null;
113             String JavaDoc actualEmail = "";
114             String JavaDoc firstEmail = null;
115             boolean found = false;
116             account = Property.getProperty(new RealmKey("security.administrators", userDatabase.getRealm().getResourceId()));
117             email = "";
118             User[] users = userDatabase.listAllUsers("*");
119             usernames = new ArrayList JavaDoc<String JavaDoc>();
120             if (users != null) {
121                 for (int i = 0; i < users.length; i++) {
122                     String JavaDoc un = users[i].getPrincipalName();
123                     usernames.add(un);
124                     if(i == 0) {
125                         firstEmail = users[i].getEmail();
126                     }
127                     if(un.equals(account)) {
128                         actualEmail = users[i].getEmail();
129                         found = true;
130                     }
131                     else if (un.toLowerCase().startsWith("admin") || un.toLowerCase().startsWith("root")) {
132                         bestGuess = un;
133                         actualEmail = users[i].getEmail();
134                     }
135                 }
136             }
137             if(!found) {
138                 if(bestGuess != null) {
139                     account = bestGuess;
140                     email = actualEmail;
141                 }
142                 else if (usernames.size() > 0) {
143                     account = (String JavaDoc) usernames.get(0);
144                     email = firstEmail;
145                 }
146                 else {
147                     account = "";
148                     email = "";
149                 }
150             }
151             else {
152                 email = actualEmail;
153             }
154         } catch (Exception JavaDoc e) {
155             log.error("Failed to load user database.", e);
156         }
157
158         /* DUMMY_PASSWORD is sent back because we don't want password
159          * values sent back in HTML
160          */

161         String JavaDoc password = (String JavaDoc) sequence.getAttribute(ATTR_SUPER_USER_PASSWORD, "");
162         if (!password.equals("")) {
163             superUserPassword = DUMMY_PASSWORD;
164             confirmSuperUserPassword = DUMMY_PASSWORD;
165         } else {
166             superUserPassword = "";
167             confirmSuperUserPassword = "";
168         }
169         account = (String JavaDoc) sequence.getAttribute(ATTR_SUPER_USER, account);
170         email = (String JavaDoc) sequence.getAttribute(ATTR_SUPER_USER_EMAIL, email);
171     }
172
173     /*
174      * (non-Javadoc)
175      *
176      * @see com.sslexplorer.wizard.forms.AbstractWizardForm#apply(com.sslexplorer.wizard.AbstractWizardSequence)
177      */

178     public void apply(AbstractWizardSequence sequence) throws Exception JavaDoc {
179         String JavaDoc password = getSuperUserPassword(sequence);
180         sequence.putAttribute(ATTR_SUPER_USER_PASSWORD, password);
181         sequence.putAttribute(ATTR_SUPER_USER, account);
182         sequence.putAttribute(ATTR_SUPER_USER_EMAIL, email);
183     }
184     
185     private String JavaDoc getSuperUserPassword(AbstractWizardSequence sequence) {
186         if(!getSuperUserCreationSupported()) {
187             return "";
188         }
189         
190         if (DUMMY_PASSWORD.equals(superUserPassword)) {
191             return (String JavaDoc) sequence.getAttribute(ATTR_SUPER_USER_PASSWORD, "");
192         } else {
193             return superUserPassword;
194         }
195     }
196
197     /**
198      * Set the super user password
199      *
200      * @param superUserPassword super user password
201      */

202     public void setSuperUserPassword(String JavaDoc superUserPassword) {
203         this.superUserPassword = superUserPassword;
204     }
205
206     /**
207      * Get the super user password
208      *
209      * @return super user password
210      */

211     public String JavaDoc getSuperUserPassword() {
212         return superUserPassword;
213     }
214
215     /**
216      * Set the confirmed super user password
217      *
218      * @param confirmSuperUserPassword confirmed super user password
219      */

220     public void setConfirmSuperUserPassword(String JavaDoc confirmSuperUserPassword) {
221         this.confirmSuperUserPassword = confirmSuperUserPassword;
222     }
223
224     /**
225      * Get the confirmed super user password
226      *
227      * @return confirmed super user password
228      */

229     public String JavaDoc getConfirmSuperUserPassword() {
230         return confirmSuperUserPassword;
231     }
232
233     /*
234      * (non-Javadoc)
235      *
236      * @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping,
237      * javax.servlet.http.HttpServletRequest)
238      */

239     public ActionErrors validate(ActionMapping mapping, HttpServletRequest JavaDoc request) {
240         if (isCommiting() && superUserPassword != null) {
241             try {
242                 // keypasswords do not match
243
if (!( superUserPassword.equals(DUMMY_PASSWORD) && confirmSuperUserPassword.equals(DUMMY_PASSWORD)) && !superUserPassword.equals(confirmSuperUserPassword)) {
244                     throw new Exception JavaDoc("passwordsDoNotMatch");
245                 }
246                 if (account.equals("")) {
247                     throw new Exception JavaDoc("noSuperUserSpecified");
248                 }
249                 try {
250                     userDatabase.getAccount(account);
251                 } catch (Exception JavaDoc e) {
252                     log.error("Failure to getAccount", e);
253                     
254                     if (getSuperUserCreationSupported()) {
255                         if (superUserPassword.equals("")) {
256                             throw new Exception JavaDoc("noPassword");
257                         }
258                     }
259                     else {
260                         throw new Exception JavaDoc("superUserDoesntExist");
261                     }
262                 }
263             } catch (Exception JavaDoc e) {
264                 // Always report to user when an error is encountered
265
ActionErrors errs = new ActionErrors();
266                 errs.add(Globals.ERROR_KEY, new ActionMessage("installation.configureSuperUser.error." + e.getMessage()));
267                 return errs;
268             }
269         }
270         return null;
271     }
272
273     /**
274      * Get the super user account name
275      *
276      * @return super user account name
277      */

278     public String JavaDoc getAccount() {
279         return account;
280     }
281
282     /**
283      * Get the super user account email address
284      *
285      * @return super user account email address
286      */

287     public String JavaDoc getEmail() {
288         return email;
289     }
290
291     /**
292      * Set the super user account name
293      *
294      * @param account super user account name
295      */

296     public void setAccount(String JavaDoc account) {
297         this.account = account;
298     }
299
300     /**
301      * Set the super user account email address
302      *
303      * @param email super user account email address
304      */

305     public void setEmail(String JavaDoc email) {
306         this.email = email;
307     }
308
309     /**
310      * @return Returns the userDatabase.
311      */

312     public UserDatabase getUserDatabase() {
313         return userDatabase;
314     }
315
316     /**
317      * Get if account creation is supported by the current user
318      * database. For this the underlying user database implementation
319      * must support both account creation and password change
320      *
321      * @return account creation supported by current user database
322      */

323     public boolean getSuperUserCreationSupported() {
324         return JDBCUserDatabase.DATABASE_TYPE.equals(databaseType);
325     }
326 }
327
Popular Tags