1
2
3
4
5
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
25
26
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
48
49 public BSAccountMan getBsAccountMan() {
50 return bsAccountMan;
51 }
52
53
54
55
56 public void setBsAccountMan(BSAccountMan bsAccountMan) {
57 this.bsAccountMan = bsAccountMan;
58 }
59
60
61
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
73
74 public Database getDb() {
75 return db;
76 }
77
78
79
80
81 public void setDb(Database db) {
82 this.db = db;
83 }
84
85
86
87
88 @Override
89 public BSAccountUser getNewUser() {
90 logger.info("getNewUser()");
91 return(new BSAccountUser(null));
92 }
93
94
95
96
97 @Override
98 public BSAccountUser getUser(BSAccount account) {
99 if (logger.isInfoEnabled())
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
107
108
109 @Override
110 public BSAccountUserWebSession getNewUserWebSession(BSAccountUser user) {
111 return(new BSAccountUserWebSession(user));
112 }
113
114
115
116
117 @Override
118 public int getUserCount() {
119 return(db.getUserCount());
120 }
121
122
123
124
125
126
127
128
129
130 public void insertAccount(BSAccount account) {
131 throw new RuntimeException("insertAccount: Not implemented");
132 }
133
134
135
136
137 @Override
138 public void updateAccount(BSAccount account) {
139 getRandomInt();
140 db.updateAccount(account);
141 }
142
143
144
145
146 @Override
147 public void updateAccountPassword(BSAccount account, String newPassword) {
148 getRandomInt();
149 db.updateAccountPassword(account, newPassword);
150 account.setStatus(BSAccount.STATE_OK);
151 account.setRegisterToken(0);
152 db.updateAccount(account);
153 }
154
155
156
157
158 @Override
159 public String registerAccount(String appName, BSAccount account) {
160 getRandomInt();
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
168
169
170 @Override
171 public void verifyUser(BSAccount account) {
172 throw new RuntimeException("Unimplemented");
173 }
174
175
176
177
178 @Override
179 public String validateUserName(String username) {
180 getRandomInt();
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
189
190
191 @Override
192 public String validateEmailAddress(String email) {
193 getRandomInt();
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
200
201
202 @Override
203 public String sendValidateEmail(String appName, String username, String email) {
204 getRandomInt();
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();
215 if (username != null && username.length() > 0)
216 return("Can't send validation email to unknown user");
217 return("user not found");
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
231
232
233 @Override
234 public String validateAccount(String userid, String registerToken) {
235 getRandomInt();
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
277
278
279 @Override
280 public BSAccount getAccount(String username) {
281 return (db.getAccount(username));
282 }
283
284
285
286
287
288 @Override
289 public BSAccount getAccount(int userid) {
290 return(db.getAccount(userid));
291 }
292
293
294
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
304
305 }
306
307 @Override
308 public String getLoginSuccessPageRedirect() {
309
310 return null;
311 }
312
313 @Override
314 public void setUserFactory(UserFactory userFactory) {
315
316
317 }
318
319 @Override
320 public boolean isHelloForwards() {
321
322 return false;
323 }
324
325 @Override
326 public void setHelloForwards(boolean helloForwards) {
327
328
329 }
330
331
332 }