View Javadoc
1   /******************************************************************************
2    * BSAccountManManImpl.java - Implement the API for BSAccountManMan
3    * 
4    * BSAccountMan - BuckoSoft Web Account Manager Manager 
5    * Copyright(c) 2007 - Dick Balaska and BuckoSoft, Corp.
6    * 
7    */
8   package com.buckosoft.BSAccountMan.business;
9   
10  import java.security.SecureRandom;
11  
12  import org.apache.commons.logging.Log;
13  import org.apache.commons.logging.LogFactory;
14  import org.springframework.beans.factory.annotation.Autowired;
15  
16  import com.buckosoft.BSAccount.BSAccountMan;
17  import com.buckosoft.BSAccount.domain.BSAccount;
18  import com.buckosoft.BSAccount.domain.BSAccountUser;
19  import com.buckosoft.BSAccount.domain.BSAccountUserWebSession;
20  import com.buckosoft.BSAccount.domain.UserFactory;
21  import com.buckosoft.BSAccountMan.db.Database;
22  import com.buckosoft.BSAccountMan.mail.MailSender;
23  
24  /** Implement the API for BSAccountManMan
25   * @author dick
26   * @since 2007-09-19
27   */
28  public class BSAccountManManImpl implements BSAccountManMan, UserFactory {
29  
30  	protected final Log logger = LogFactory.getLog(getClass());
31  	
32  	@Autowired
33  	private	BSAccountMan	bsAccountMan;
34  	@Autowired
35  	private	Database		db;
36  
37  	private	String			smtpHost	= "smtp.buckosoft.com";
38  	private	String			baseUrl		= "http:/probablyNotWhatYouWant";
39  	private	SecureRandom	random;
40  
41  	public BSAccountManManImpl() {
42  		random = new SecureRandom();
43  		random.nextInt();
44  	}
45  
46  	/**
47  	 * @return the bsAccountMan
48  	 */
49  	public BSAccountMan getBsAccountMan() {
50  		return bsAccountMan;
51  	}
52  
53  	/**
54  	 * @param bsAccountMan the bsAccountMan to set
55  	 */
56  	public void setBsAccountMan(BSAccountMan bsAccountMan) {
57  		this.bsAccountMan = bsAccountMan;
58  	}
59  
60  	/**
61  	 * @param smtpHost the smtpHost to set
62  	 */
63  	public void setSmtpHost(String smtpHost) {
64  		this.smtpHost = smtpHost;
65  	}
66  
67  	public void setBaseUrl(String baseUrl) {
68  		this.baseUrl = baseUrl;
69  	}
70  
71  	/**
72  	 * @return the db
73  	 */
74  	public Database getDb() {
75  		return db;
76  	}
77  
78  	/**
79  	 * @param db the db to set
80  	 */
81  	public void setDb(Database db) {
82  		this.db = db;
83  	}
84  
85  	/* (non-Javadoc)
86  	 * @see com.buckosoft.BSAccount.domain.UserFactory#getNewUser()
87  	 */
88  	@Override
89  	public BSAccountUser getNewUser() {
90  		logger.info("getNewUser()");
91  		return(new BSAccountUser(null));
92  	}
93  
94  	/* (non-Javadoc)
95  	 * @see com.buckosoft.BSAccount.domain.UserFactory#getUser(com.buckosoft.BSAccount.domain.BSAccount)
96  	 */
97  	@Override
98  	public BSAccountUser getUser(BSAccount account) {
99  		if (logger.isInfoEnabled())		// eliminate wacky dead code warning below
100 			logger.info("getUser: " + account == null ? "account = null" : "account userid=" + account.getUserId());
101 		if (account == null)
102 			return(null);
103 		return(new BSAccountUser(account));
104 	}
105 
106 	/* (non-Javadoc)
107 	 * @see com.buckosoft.BSAccount.domain.UserFactory#getNewUserWebSession(com.buckosoft.BSAccount.domain.BSAccountUser)
108 	 */
109 	@Override
110 	public BSAccountUserWebSession getNewUserWebSession(BSAccountUser user) {
111 		return(new BSAccountUserWebSession(user));
112 	}
113 
114 	/** Get the number of users in the system
115 	 * @return The number of users
116 	 */
117 	@Override
118 	public int getUserCount() {
119 		return(db.getUserCount());
120 	}
121 
122 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
123 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
124 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
125 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
126 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
127 	/* (non-Javadoc)
128 	 * @see com.buckosoft.BSAccount.BSAccount#insertAccount(com.buckosoft.BSAccount.domain.Account)
129 	 */
130 	public void insertAccount(BSAccount account) {
131 		throw new RuntimeException("insertAccount: Not implemented");
132 	}
133 
134 	/* (non-Javadoc)
135 	 * @see com.buckosoft.BSAccount.BSAccount#updateAccount(com.buckosoft.BSAccount.domain.Account)
136 	 */
137 	@Override
138 	public void updateAccount(BSAccount account) {
139 		getRandomInt();		// entropy in the random stream
140 		db.updateAccount(account);
141 	}
142 
143 	/* (non-Javadoc)
144 	 * @see com.buckosoft.BSAccount.BSAccountMan#updateAccountPassword(com.buckosoft.BSAccount.domain.BSAccount, java.lang.String)
145 	 */
146 	@Override
147 	public void updateAccountPassword(BSAccount account, String newPassword) {
148 		getRandomInt();		// entropy in the random stream
149 		db.updateAccountPassword(account, newPassword);
150 		account.setStatus(BSAccount.STATE_OK);
151 		account.setRegisterToken(0);
152 		db.updateAccount(account);
153 	}
154 
155 	/* (non-Javadoc)
156 	 * @see com.buckosoft.BSAccount.BSAccount#registerAccount(com.buckosoft.BSAccount.domain.Account)
157 	 */
158 	@Override
159 	public String registerAccount(String appName, BSAccount account) {
160 		getRandomInt();		// entropy in the random stream
161 		account.setRegisterToken(this.getRandomInt());
162 		logger.info("registerAccount: token = " + account.getRegisterToken());
163 		db.insertAccount(account);
164 		return(this.sendValidateEmail(appName, account.getUsername(), ""));
165 	}
166 
167 	/* (non-Javadoc)
168 	 * @see com.buckosoft.BSAccount.BSAccount#verifyUser(com.buckosoft.BSAccount.domain.Account)
169 	 */
170 	@Override
171 	public void verifyUser(BSAccount account) {
172 		throw new RuntimeException("Unimplemented");
173 	}
174 
175 	/* (non-Javadoc)
176 	 * @see com.buckosoft.BSAccount.BSAccountMan#validateUserName(java.lang.String)
177 	 */
178 	@Override
179 	public String validateUserName(String username) {
180 		getRandomInt();		// entropy in the random stream
181 		if (!username.matches("[A-Za-z][A-Za-z0-9_]*"))
182 			return("Not a valid username, no funny characters. Just letters, numbers, and _");
183 		if (username.length() > 20)
184 			return("Username too long. 20 character limit");
185 		return("");
186 	}
187 
188 	/* (non-Javadoc)
189 	 * @see com.buckosoft.BSAccount.BSAccount#validateEmailAddress(java.lang.String)
190 	 */
191 	@Override
192 	public String validateEmailAddress(String email) {
193 		getRandomInt();		// entropy in the random stream
194 		if (!email.matches("[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"))
195 			return("Not a valid looking email address");
196 		return("");
197 	}
198 
199 	/* (non-Javadoc)
200 	 * @see com.buckosoft.BSAccount.BSAccount#sendValidateEmail(java.lang.String)
201 	 */
202 	@Override
203 	public String sendValidateEmail(String appName, String username, String email) {
204 		getRandomInt();		// entropy in the random stream
205 		logger.info("sendValidateEmail to '" + username + "'");
206 		BSAccount account = db.getAccount(username);
207 		if (account == null) {
208 			if (email == null)
209 				account = db.getAccountByEmail(username);
210 			else
211 				account = db.getAccountByEmail(email);
212 		}
213 		if (account == null) {
214 			getRandomInt();		// entropy in the random stream
215 			if (username != null && username.length() > 0)
216 				return("Can't send validation email to unknown user");
217 			return("user not found");		// don't tell him anything for unknown email addresses
218 		}
219 		account.setRegisterToken(this.getRandomInt());
220 		db.updateAccount(account);
221 		
222 		MailSender ms = new MailSender();
223 		ms.setBaseUrl(this.baseUrl);
224 		ms.setSmtpHost(this.smtpHost);
225 		String s = ms.sendValidation(appName, account);
226 		logger.info("result: " + s);
227 		return(s);
228 	}
229 
230 	/* (non-Javadoc)
231 	 * @see com.buckosoft.BSAccount.BSAccount#validateAccount(java.lang.String, java.lang.String)
232 	 */
233 	@Override
234 	public String validateAccount(String userid, String registerToken) {
235 		getRandomInt();		// entropy in the random stream
236 		String s = "";
237 		String h = "Can't validate account: ";
238 		int i = -1;
239 		try {
240 			i = Integer.parseInt(userid);
241 		} catch (NumberFormatException e1) {}
242 		if (i == -1)
243 			return(h + "Invalid userid: " + userid);
244 		BSAccount a = db.getAccount(i);
245 		if (a == null) {
246 			s = h + "No such account '" + userid + "'";
247 			logger.info(s);
248 			return(s);
249 		}
250 		logger.info("Got account to validate");
251 		int r;
252 		try {
253 			r = Integer.parseInt(registerToken);
254 			if (r != a.getRegisterToken())
255 				s = h + "token does not match database";
256 		} catch (NumberFormatException e) {
257 			s = h + "Can't parse '" + registerToken + "' as an integer";
258 			logger.info(s);
259 			return(s);
260 		}
261 		if (a.getRegisterToken() != r) {
262 			s = h + "Register token mismatch";
263 			logger.info(s);
264 			return(s);
265 		}
266 		a.setRegisterToken(0);
267 		a.setStatus(BSAccount.STATE_NEEDPWD);
268 		db.updateAccount(a);
269 		return s;
270 	}
271 
272 	public	int	getRandomInt() {
273 		return(Math.abs(random.nextInt()));
274 	}
275 
276 	/* (non-Javadoc)
277 	 * @see com.buckosoft.BSAccount.BSAccount#getAccount(java.lang.String)
278 	 */
279 	@Override
280 	public BSAccount getAccount(String username) {
281 		return (db.getAccount(username));
282 	}
283 
284 
285 	/* (non-Javadoc)
286 	 * @see com.buckosoft.BSAccount.BSAccountMan#getAccount(int)
287 	 */
288 	@Override
289 	public BSAccount getAccount(int userid) {
290 		return(db.getAccount(userid));
291 	}
292 
293 	/* (non-Javadoc)
294 	 * @see com.buckosoft.BSAccountMan.business.BSAccountManMan#getAccountByToken(int)
295 	 */
296 	@Override
297 	public BSAccount getAccountByToken(int token) {
298 		return(db.getAccountByToken(token));
299 	}
300 
301 	@Override
302 	public void setLoginSuccessPageRedirect(String loginSuccessPageRedirect) {
303 		// TODO Auto-generated method stub
304 		
305 	}
306 
307 	@Override
308 	public String getLoginSuccessPageRedirect() {
309 		// TODO Auto-generated method stub
310 		return null;
311 	}
312 
313 	@Override
314 	public void setUserFactory(UserFactory userFactory) {
315 		// TODO Auto-generated method stub
316 		
317 	}
318 
319 	@Override
320 	public boolean isHelloForwards() {
321 		// TODO Auto-generated method stub
322 		return false;
323 	}
324 
325 	@Override
326 	public void setHelloForwards(boolean helloForwards) {
327 		// TODO Auto-generated method stub
328 		
329 	}
330 
331 	
332 }