KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > actions > Register


1 package org.tigris.scarab.actions;
2
3 /* ================================================================
4  * Copyright (c) 2000-2002 CollabNet. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by Collab.Net <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of Collab.Net.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of Collab.Net.
47  */

48
49 // Turbine Stuff
50
import java.util.Locale JavaDoc;
51
52 import org.apache.turbine.RunData;
53 import org.apache.turbine.TemplateContext;
54 import org.apache.turbine.Turbine;
55 import org.apache.turbine.modules.ContextAdapter;
56 import org.apache.turbine.tool.IntakeTool;
57
58 import org.apache.fulcrum.intake.model.Field;
59 import org.apache.fulcrum.intake.model.Group;
60 import org.apache.fulcrum.security.TurbineSecurity;
61 import org.apache.fulcrum.security.util.TurbineSecurityException;
62
63 // Scarab Stuff
64
import org.tigris.scarab.om.ScarabUser;
65 import org.tigris.scarab.tools.ScarabRequestTool;
66 import org.tigris.scarab.tools.ScarabLocalizationTool;
67 import org.tigris.scarab.tools.localization.L10NKeySet;
68 import org.tigris.scarab.tools.localization.L10NMessage;
69 import org.tigris.scarab.tools.localization.Localizable;
70 import org.tigris.scarab.util.AnonymousUserUtil;
71 import org.tigris.scarab.util.Email;
72 import org.tigris.scarab.util.ScarabConstants;
73 import org.tigris.scarab.util.Log;
74 import org.tigris.scarab.util.ScarabRuntimeException;
75 import org.tigris.scarab.actions.base.ScarabTemplateAction;
76
77 // FIXME: remove the methods that reference this
78
import org.tigris.scarab.om.ScarabUserImpl;
79 import org.tigris.scarab.om.ScarabUserImplPeer;
80
81 import org.xbill.DNS.Record;
82 import org.xbill.DNS.dns;
83 import org.xbill.DNS.Type;
84
85 /**
86  * This class is responsible for dealing with the Register
87  * Action.
88  *
89  * @author <a HREF="mailto:jon@collab.net">Jon S. Stevens</a>
90  * @version $Id: Register.java 9360 2005-01-04 01:31:06Z dabbous $
91  */

92 public class Register extends ScarabTemplateAction
93 {
94
95     private boolean checkRFC2505(String JavaDoc email)
96     {
97         // try just the end portion of the domain
98
String JavaDoc domain = parseDomain(email);
99         if (domain != null)
100         {
101             // try to find any A records for the domain
102
Record[] records = dns.getRecords(domain, Type.A);
103             if (records != null || records.length > 0)
104             {
105                 return true;
106             }
107             // now try just the domain after the @
108
// this is for domains like foo.co.uk
109
String JavaDoc fullDomain = email.substring(email.indexOf('@')+1);
110             records = dns.getRecords(fullDomain, Type.A);
111             if (records != null || records.length > 0)
112             {
113                 return true;
114             }
115             // now try to find any MX records for the domain
116
records = dns.getRecords(domain, Type.MX);
117             if (records != null || records.length > 0)
118             {
119                 return true;
120             }
121             // now try to find any MX records for the fullDomain
122
records = dns.getRecords(fullDomain, Type.MX);
123             if (records != null || records.length > 0)
124             {
125                 return true;
126             }
127         }
128         return false;
129     }
130
131     /**
132      * This manages clicking the "Register" button in the Register.vm
133      * template. As a result, the user will go to the
134      * RegisterConfirm.vm screen.
135      */

136     public void doRegister(RunData data, TemplateContext context)
137         throws Exception JavaDoc
138     {
139         String JavaDoc template = getCurrentTemplate(data, null);
140         String JavaDoc nextTemplate = getNextTemplate(data, template);
141
142         IntakeTool intake = getIntakeTool(context);
143         if (intake.isAllValid())
144         {
145             ScarabRequestTool scarabR = getScarabRequestTool(context);
146             ScarabLocalizationTool l10n = getLocalizationTool(context);
147             Object JavaDoc user = data
148                             .getUser()
149                             .getTemp(ScarabConstants.SESSION_REGISTER);
150             Group register = null;
151             if (user != null && user instanceof ScarabUser)
152             {
153                 register = intake.get("Register",
154                     ((ScarabUser)user).getQueryKey(), false);
155             }
156             else
157             {
158                 register = intake.get("Register",
159                     IntakeTool.DEFAULT_KEY, false);
160             }
161
162             // not quite sure why this happens, but it does, so case
163
// for it and deal with it.
164
if (register == null)
165             {
166                 setTarget(data,"Register.vm");
167                 scarabR.setAlertMessage(L10NKeySet.RegisterSessionError);
168                 return;
169             }
170
171             String JavaDoc password = register.get("Password").toString();
172             String JavaDoc passwordConfirm = register.get("PasswordConfirm").toString();
173
174             // check to make sure the passwords match
175
if (!password.equals(passwordConfirm))
176             {
177                 setTarget(data, template);
178                 scarabR.setAlertMessage(L10NKeySet.PasswordsDoNotMatch);
179                 return;
180             }
181
182             // get an anonymous user
183
ScarabUser su = (ScarabUser) (ScarabUser) AnonymousUserUtil.getAnonymousUser();
184             try
185             {
186                 register.setProperties(su);
187             }
188             catch (Exception JavaDoc e)
189             {
190                 setTarget(data, template);
191                 Localizable msg = new L10NMessage(L10NKeySet.ExceptionGeneric,e);
192                 scarabR.setAlertMessage(msg);
193                 return;
194             }
195
196             String JavaDoc email = su.getEmail();
197             if (email == null)
198             {
199                 setTarget(data,"Register.vm");
200                 scarabR.setAlertMessage(L10NKeySet.EnterValidEmailAddress);
201                 return;
202             }
203
204             // check to see if the email is a valid domain (has A records)
205
if (Turbine.getConfiguration()
206                     .getBoolean("scarab.register.email.checkRFC2505", false))
207             {
208                 if (!checkRFC2505(email))
209                 {
210                     setTarget(data, template);
211                     Localizable msg = new L10NMessage(L10NKeySet.EmailHasBadDNS,email);
212                     scarabR.setAlertMessage(msg);
213                     return;
214                 }
215             }
216             String JavaDoc[] badEmails = Turbine
217                                 .getConfiguration()
218                                 .getStringArray("scarab.register.email.badEmails");
219             if (badEmails != null && badEmails.length > 0)
220             {
221                 for (int i=0;i<badEmails.length;i++)
222                 {
223                     if (email.equalsIgnoreCase(badEmails[i]))
224                     {
225                         setTarget(data, template);
226                         Localizable msg = new L10NMessage(L10NKeySet.InvalidEmailAddress,email);
227                         scarabR.setAlertMessage(msg);
228                         return;
229                     }
230                 }
231             }
232             
233             // check to see if the user already exists
234
if (ScarabUserImplPeer.checkExists(su))
235             {
236                 setTarget(data, template);
237                 scarabR.setAlertMessage(L10NKeySet.UsernameExistsAlready);
238                 return;
239             }
240
241             // put the user object into the context so that it can be
242
// used on the nextTemplate
243
data.getUser().setTemp(ScarabConstants.SESSION_REGISTER, su);
244             setTarget(data, nextTemplate);
245         }
246     }
247
248     public void doConfirmregistration(RunData data, TemplateContext context)
249         throws Exception JavaDoc
250     {
251         String JavaDoc template = getCurrentTemplate(data);
252         String JavaDoc nextTemplate = getNextTemplate(data);
253         ScarabRequestTool scarabR = getScarabRequestTool(context);
254
255         try
256         {
257             // pull the user object from the session
258
ScarabUser su = (ScarabUser) data.getUser()
259                 .getTemp(ScarabConstants.SESSION_REGISTER);
260             if (su == null)
261             {
262                 // assign the template to the cancel template, not the
263
// current template
264
template = getCancelTemplate(data, "Register.vm");
265                 throw new ScarabRuntimeException(L10NKeySet.UserObjectNotInSession);
266             }
267
268             try
269             {
270                 // attempt to create a new user!
271
su.createNewUser();
272             }
273             catch (org.apache.fulcrum.security.util.EntityExistsException e)
274             {
275                 Localizable msg = new L10NMessage(L10NKeySet.ExceptionGeneric,e);
276                 scarabR.setAlertMessage(msg);
277                 setTarget(data, "Confirm.vm");
278                 return;
279             }
280
281             // grab the ScarabRequestTool object so that we can populate the
282
// User object for redisplay of the form data on the screen
283
if (scarabR != null)
284             {
285                 scarabR.setUser(su);
286             }
287             
288             // send an email that is for confirming the registration
289
sendConfirmationEmail(su, context);
290
291             // set the next template on success
292
setTarget(data, nextTemplate);
293         }
294         catch (Exception JavaDoc e)
295         {
296             setTarget(data, template);
297             Localizable msg = new L10NMessage(L10NKeySet.ExceptionGeneric,e);
298             scarabR.setAlertMessage(msg);
299             Log.get().error(e);
300             return;
301         }
302     }
303
304     /**
305      * returns you to Register.vm
306      */

307     public void doBack(RunData data, TemplateContext context)
308         throws Exception JavaDoc
309     {
310         // set the template to the template that we should be going back to
311
setTarget(data, data.getParameters().getString(
312                 ScarabConstants.CANCEL_TEMPLATE, "Register.vm"));
313     }
314
315     /**
316      * calls doRegisterConfirm()
317      */

318     public void doPerform(RunData data, TemplateContext context)
319         throws Exception JavaDoc
320     {
321         doConfirmregistration(data, context);
322     }
323
324     /**
325      * This manages clicking the Confirm button in the Confirm.vm
326      * template. As a result, this will end up sending
327      * the user to the Confirm screen.
328      */

329     public void doConfirm(RunData data, TemplateContext context)
330         throws Exception JavaDoc
331     {
332         String JavaDoc template = getCurrentTemplate(data, null);
333         String JavaDoc nextTemplate = getNextTemplate(data, template);
334
335         IntakeTool intake = getIntakeTool(context);
336         if (intake.isAllValid())
337         {
338             ScarabLocalizationTool l10n = getLocalizationTool(context);
339             ScarabRequestTool scarabR = getScarabRequestTool(context);
340             Object JavaDoc user = data
341                             .getUser()
342                             .getTemp(ScarabConstants.SESSION_REGISTER);
343             Group register = null;
344             if (user != null && user instanceof ScarabUser)
345             {
346                 register = intake.get("Register",
347                     ((ScarabUser)user).getQueryKey(), false);
348             }
349             else
350             {
351                 register = intake.get("Register",
352                     IntakeTool.DEFAULT_KEY, false);
353             }
354
355             if (register == null)
356             {
357                 // This is often triggered by self-host issue SCB825.
358
scarabR.setAlertMessage(L10NKeySet.RegisterGroupIsNullError);
359                 String JavaDoc msg = "Register group is null: user="
360                     + (user != null && user instanceof ScarabUser ?
361                        ((ScarabUser) user).getQueryKey() : "[none]")
362                     + " IntakeTool.DEFAULT_KEY=" + IntakeTool.DEFAULT_KEY;
363                 Log.get().warn(msg);
364                 return;
365             }
366             String JavaDoc username = null;
367             String JavaDoc confirm = null;
368             Field usernameField = register.get("UserName");
369             Field confirmField = register.get("Confirm");
370             if (usernameField == null)
371             {
372                 scarabR.setAlertMessage(L10NKeySet.UsernameGroupIsNullError);
373                 return;
374             }
375             else if (confirmField == null)
376             {
377                 scarabR.setAlertMessage(L10NKeySet.ConfirmFieldIsNullError);
378                 return;
379             }
380             username = usernameField.toString();
381             confirm = confirmField.toString();
382
383             // This reference to ScarabUserImpl is ok because this action
384
// is specific to use with that implementation.
385
if (ScarabUserImpl.checkConfirmationCode(username, confirm))
386             {
387                 // update the database to confirm the user
388
if(ScarabUserImpl.confirmUser(username))
389                 {
390                     // NO PROBLEMS! :-)
391
ScarabUser confirmedUser = (ScarabUser)
392                                     TurbineSecurity.getUser(username);
393                     // we set this to false and make people login again
394
// because of this issue:
395
// http://scarab.tigris.org/issues/show_bug.cgi?id=115
396
// there may be a better way, but given that on the confirm
397
// screen, we aren't asking for a password and checkConfirmationCode
398
// will return true if someone is already confirmed,
399
// we need to do this for security purposes.
400
confirmedUser.setHasLoggedIn(Boolean.FALSE);
401                     data.setUser(confirmedUser);
402                     data.save();
403     
404                     scarabR.setConfirmMessage(L10NKeySet.AccountConfirmedSuccess);
405                     setTarget(data, nextTemplate);
406                 }
407                 else
408                 {
409                     scarabR.setAlertMessage(L10NKeySet.AccountConfirmedFailure);
410                     setTarget(data, template);
411                 }
412             }
413             else // we don't have confirmation! :-(
414
{
415                 scarabR.setAlertMessage(L10NKeySet.InvalidConfirmationCode);
416                 setTarget(data, template);
417             }
418         }
419     }
420
421     /**
422      * This manages clicking the "Resend code" button
423      * in the Confirm.vm template.
424      */

425     public void doResendconfirmationcode(RunData data, TemplateContext context)
426         throws Exception JavaDoc
427     {
428         String JavaDoc template = getCurrentTemplate(data, null);
429         ScarabRequestTool scarabR = getScarabRequestTool(context);
430
431         try
432         {
433             Object JavaDoc user = data
434                             .getUser()
435                             .getTemp(ScarabConstants.SESSION_REGISTER);
436  
437             ScarabLocalizationTool l10n = getLocalizationTool(context);
438             IntakeTool intake = getIntakeTool(context);
439             Group register = null;
440             if (user != null && user instanceof ScarabUser)
441             {
442                 register = intake.get("Register",
443                     ((ScarabUser)user).getQueryKey(), false);
444             }
445             else
446             {
447                 register = intake.get("Register",
448                     IntakeTool.DEFAULT_KEY, false);
449             }
450         
451             if (register == null)
452             {
453                 scarabR.setAlertMessage(L10NKeySet.RegisterGroupIsNullError);
454                 return;
455             }
456             String JavaDoc username = register.get("UserName").toString();
457             try
458             {
459                 // Authenticate the user and get the object.
460
user = TurbineSecurity.getUser(username);
461
462                 // grab the ScarabRequestTool object so that we can
463
// populate the User object for redisplay of the form
464
// data on the screen
465
if (scarabR != null)
466                 {
467                     scarabR.setUser((ScarabUser) user);
468                 }
469             }
470             catch (TurbineSecurityException e)
471             {
472                 scarabR.setAlertMessage(L10NKeySet.InvalidUsername);
473                 Log.get().error ("RegisterConfirm: ", e);
474                 return;
475             }
476         
477             // send an email that is for confirming the registration
478
sendConfirmationEmail((ScarabUser) user, context);
479             scarabR.setConfirmMessage(L10NKeySet.ConfirmationCodeSent);
480
481             // set the next template on success
482
data.getUser().setTemp(ScarabConstants.SESSION_REGISTER, user);
483             intake.remove(register);
484
485             setTarget(data, "Confirm.vm");
486         }
487         catch (Exception JavaDoc e)
488         {
489             setTarget(data, template);
490             Localizable msg = new L10NMessage(L10NKeySet.ExceptionGeneric,e);
491             scarabR.setAlertMessage(msg);
492             Log.get().error(e);
493             return;
494         }
495     }
496
497     /**
498      * For an email address like <code>jon@foo.bar.com</code>, parse
499      * and return the TLD <code>bar.com</code>.
500      */

501     String JavaDoc parseDomain(String JavaDoc email)
502     {
503         String JavaDoc result = null;
504         char[] emailChars = email.toCharArray();
505         int dotCount = 0;
506         for (int i = emailChars.length - 1; i >= 0; i--)
507         {
508             if (emailChars[i] == '.')
509             {
510                 dotCount++;
511             }
512             if (dotCount == 2 || emailChars[i] == '@')
513             {
514                 result = new String JavaDoc(emailChars, i + 1,
515                                     emailChars.length - (i + 1));
516                 break;
517             }
518         }
519         return result;
520     }
521     
522     /**
523      * Send the confirmation code to the given user.
524      */

525     private void sendConfirmationEmail(ScarabUser su, TemplateContext context)
526         throws Exception JavaDoc
527     {
528         Email te = new Email();
529
530         // Retrieve the charset to be used for the Email.
531
ScarabLocalizationTool l10n = getLocalizationTool(context);
532         Locale JavaDoc locale = l10n.getPrimaryLocale();
533         String JavaDoc charset = Email.getCharset(locale);
534         te.setCharset(charset);
535
536         te.setContext(new ContextAdapter(context));
537         te.setTo(su.getFirstName() + " " + su.getLastName(), su.getEmail());
538         te.setFrom(
539             Turbine.getConfiguration()
540                 .getString("scarab.email.register.fromName",
541                            "Scarab System"),
542             Turbine.getConfiguration()
543                 .getString("scarab.email.register.fromAddress",
544                            "register@localhost"));
545         te.setSubject(L10NKeySet.ConfirmationSubject.getMessage(l10n));
546         te.setTemplate(
547             Turbine.getConfiguration()
548                 .getString("scarab.email.register.template",
549                            "email/Confirmation.vm"));
550         te.send();
551     }
552 }
553
Popular Tags