View Javadoc

1   /*
2    * Copyright (C) 2007 Alf Mikula
3    * 
4    * This file is part of PromoteGo.
5    *
6    * PromoteGo is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * PromoteGo is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with PromoteGo.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.promotego.logic;
20  
21  import java.util.Date;
22  import java.util.Random;
23  
24  import org.promotego.beans.User;
25  import org.promotego.beans.UserHolder;
26  import org.promotego.beans.ValidationRecord;
27  import org.promotego.dao.interfaces.UserDao;
28  import org.promotego.dao.interfaces.ValidationRecordDao;
29  import org.promotego.interfaces.PasswordHashTool;
30  import org.promotego.viewbeans.UserInfoBean;
31  import org.springframework.beans.factory.annotation.Required;
32  import org.springframework.mail.MailSender;
33  import org.springframework.mail.SimpleMailMessage;
34  import org.springframework.transaction.annotation.Transactional;
35  
36  public class UserRegisterer
37  {
38      private static char [] s_validatorChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_!~*".toCharArray();
39      
40      private UserDao m_userDao;
41      private PasswordHashTool m_passwordHashTool;
42      private ValidationRecordDao m_validationRecordDao;
43      private MailSender m_mailSender;
44      private UserHolder m_userHolder;
45      private String m_baseUrl;
46      
47      @Transactional
48      public ValidationRecord registerUser(UserInfoBean userInfo)
49      {
50          User theUser = new User();
51          
52          // Force username to lowercase.
53          theUser.setUsername(userInfo.getUsername().toLowerCase());
54          theUser.setCryptedPassword(m_passwordHashTool.hash(userInfo.getPassword()));
55          theUser.setEmailAddress(userInfo.getEmailAddress());
56          theUser.setValidated(false);
57          
58          m_userDao.create(theUser);
59          
60          m_userHolder.setUser(theUser);
61          
62          String validationString = createValidationString(32);
63          
64          ValidationRecord theRecord = new ValidationRecord();
65          theRecord.setUser(theUser);
66          theRecord.setValidationKey(validationString);
67          theRecord.setCreationDate(new Date());
68  
69          m_validationRecordDao.create(theRecord);
70          
71          SimpleMailMessage msg = new SimpleMailMessage();
72          
73          // TODO Make sender configurable
74          msg.setFrom("noreply@promotego.org");
75          msg.setTo(theUser.getEmailAddress());
76          msg.setSubject("[PromoteGo] Please validate your email address");
77  
78          msg.setText("\nYou have registered the username " + theUser.getUsername() + ".\n\n"
79                  + "To complete registration, please follow this link: "
80                  + m_baseUrl + "/validateuser.do?validationKey="
81          		+ theRecord.getValidationKey());
82  
83          m_mailSender.send(msg);
84          
85          // TODO provide re-send validation logic
86  
87          return theRecord;
88      }
89      
90      private String createValidationString(int nChars)
91      {
92          StringBuilder retval = new StringBuilder();
93          
94          Random random = new Random();
95          
96          for (int i=0; i<nChars; i++)
97          {
98              retval.append(s_validatorChars[random.nextInt(s_validatorChars.length)]);
99          }
100         
101         return retval.toString();
102     }
103 
104     @Required
105     public void setUserDao(UserDao userDao)
106     {
107         m_userDao = userDao;
108     }
109     
110     @Required
111     public void setValidationRecordDao(ValidationRecordDao validationRecordDao)
112     {
113         m_validationRecordDao = validationRecordDao;
114     }
115 
116     @Required
117 	public void setPasswordHashTool(PasswordHashTool passwordHashTool)
118     {
119 		m_passwordHashTool = passwordHashTool;
120 	}
121 
122     @Required
123 	public void setMailSender(MailSender mailSender)
124     {
125 		m_mailSender = mailSender;
126 	}
127 
128     @Required
129 	public void setUserHolder(UserHolder userHolder)
130 	{
131 		m_userHolder = userHolder;
132 	}
133     
134     @Required
135     public void setBaseUrl(String baseUrl)
136     {
137     	m_baseUrl = baseUrl;
138     }
139 }