1
2
3
4
5
6
7
8 package com.buckosoft.BSAccountMan.web;
9
10 import java.io.IOException;
11 import java.io.PrintWriter;
12
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.springframework.beans.factory.annotation.Autowired;
19 import org.springframework.http.MediaType;
20 import org.springframework.stereotype.Controller;
21 import org.springframework.ui.Model;
22 import org.springframework.web.bind.annotation.RequestMapping;
23 import org.springframework.web.bind.annotation.RequestMethod;
24 import org.springframework.web.bind.annotation.RequestParam;
25
26 import com.buckosoft.BSAccount.BSAccountMan;
27 import com.buckosoft.BSAccount.domain.BSAccount;
28 import com.buckosoft.BSAccount.domain.BSAccountUser;
29 import com.buckosoft.BSAccount.domain.BSAccountUserWebSession;
30 import com.buckosoft.BSAccountMan.business.BSAccountManMan;
31 import com.buckosoft.BSAccountMan.domain.BSAccountPlusApp;
32 import com.buckosoft.BSAccountMan.util.ConfigManager;
33
34
35
36
37
38 @Controller
39 public class MaintController {
40 private final Log log = LogFactory.getLog(getClass());
41
42 @Autowired
43 private BSAccountMan bsAccountMan;
44
45 @Autowired
46 private BSAccountManMan bsAccountManMan;
47
48 private boolean compressJS;
49 private boolean debugJS;
50
51 public MaintController() {
52 compressJS = ConfigManager.getBoolean("BSAccountMan.compressJS", true);
53 debugJS = ConfigManager.getBoolean("BSAccountMan.debugJS", false);
54 }
55
56 @RequestMapping(value="/resetPassword", method=RequestMethod.POST, produces=MediaType.TEXT_PLAIN_VALUE)
57 public void resetPassword(HttpServletRequest request, HttpServletResponse response,
58 String appName, String userid) throws IOException {
59
60 BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
61 log.debug("userWebSession = " + userWebSession);
62 log.debug("Resetting password for " + userid);
63 String s = this.bsAccountManMan.sendValidateEmail(appName, userid, null);
64 PrintWriter pw = response.getWriter();
65 pw.print(s);
66 }
67
68 @RequestMapping("/validateUser")
69 public String validateUser(HttpServletRequest request, Model model,
70 @RequestParam("a") String appName,
71 @RequestParam("t") String token) {
72 BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
73 log.debug("userWebSession = " + userWebSession);
74 model.addAttribute("userWebSession", userWebSession);
75 model.addAttribute("compressJS", compressJS);
76 model.addAttribute("debugJS", debugJS);
77 model.addAttribute("appName", appName);
78
79 int t;
80 try {
81 t = Integer.parseInt(token);
82 } catch (NumberFormatException e) {
83 return("validateUserBad");
84 }
85 BSAccount account = this.bsAccountManMan.getAccountByToken(t);
86 if (account != null) {
87 BSAccountUser user = this.bsAccountMan.getUser(account);
88 userWebSession.setUser(user);
89 }
90 if (account == null)
91 return("validateUserBad");
92 return("validateUser");
93 }
94
95 @RequestMapping(value="/changePassword", method=RequestMethod.POST, produces=MediaType.TEXT_PLAIN_VALUE)
96 public void changePassword(HttpServletRequest request, HttpServletResponse response,
97 String appName, String password) throws IOException {
98
99 PrintWriter pw = response.getWriter();
100 BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
101 BSAccount account = userWebSession.getUser().accessAccount();
102 if (account == null) {
103 pw.print("Can't update null account");
104 log.warn("Can't update null account");
105 }
106 this.bsAccountManMan.updateAccountPassword(account, password);
107 log.debug("userWebSession = " + userWebSession);
108 log.debug("Changed password for " + account.getUsername());
109 String s = "OK";
110 pw.print(s);
111 }
112
113 @RequestMapping(value="/createUser", method=RequestMethod.POST, produces=MediaType.TEXT_PLAIN_VALUE)
114 public void createUser(HttpServletRequest request, HttpServletResponse response,
115 BSAccountPlusApp newaccount) throws IOException {
116
117 log.info("creating account for name = " + newaccount.getUsername() + " email=" + newaccount.getEmail());
118 PrintWriter pw;
119 pw = response.getWriter();
120 String s = this.bsAccountManMan.validateUserName(newaccount.getUsername());
121 if (!s.isEmpty()) {
122 pw.print(s);
123 return;
124 }
125 s = this.bsAccountManMan.validateEmailAddress(newaccount.getEmail());
126 if (!s.isEmpty()) {
127 pw.print(s);
128 return;
129 }
130 BSAccount account = this.bsAccountManMan.getAccount(newaccount.getUsername());
131 if (account != null) {
132 pw.print("user already exists.");
133 return;
134 }
135 account = new BSAccount();
136 account.setUsername(newaccount.getUsername());
137 account.setEmail(newaccount.getEmail());
138 account.setFirstName(newaccount.getFirstName());
139 account.setLastName(newaccount.getLastName());
140 account.setAddress1(newaccount.getAddress1());
141 account.setAddress2(newaccount.getAddress2());
142 account.setCity(newaccount.getCity());
143 account.setState(newaccount.getState());
144 account.setZip(newaccount.getZip());
145 account.setCountry(newaccount.getCountry());
146 account.setPhone(newaccount.getPhone());
147 s = this.bsAccountManMan.registerAccount(newaccount.getAppName(), account);
148 if (s.isEmpty())
149 pw.print("OK");
150 else
151 pw.print(s);
152 }
153
154 @RequestMapping(value="/saveUser", method=RequestMethod.POST, produces=MediaType.TEXT_PLAIN_VALUE)
155 public void saveUser(HttpServletRequest request, HttpServletResponse response,
156 BSAccountPlusApp updatedAccount) {
157 BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
158 BSAccount account = userWebSession.getUser().accessAccount();
159
160 PrintWriter pw;
161 try {
162 pw = response.getWriter();
163 } catch (IOException e) {
164 log.warn("Can't get PrintWriter trying to update " + updatedAccount.getUsername());
165 return;
166 }
167 String s = "";
168 account = this.bsAccountManMan.getAccount(updatedAccount.getUsername());
169 if (account == null) {
170 pw.print("user doesn't exist.");
171 return;
172 }
173 if (updatedAccount.getNewPassword() != null && !updatedAccount.getNewPassword().isEmpty()) {
174 BSAccount testAccount = this.bsAccountMan.getAccount(updatedAccount.getUsername(), updatedAccount.getOldPassword());
175 if (testAccount == null) {
176 s = "current password doesn't match what you typed";
177 log.info("Updating user " + updatedAccount.getUsername() + ": " + s);
178 pw.print(s);
179 return;
180 }
181 log.info("Updating password for " + updatedAccount.getUsername());
182 this.bsAccountManMan.updateAccountPassword(account, updatedAccount.getNewPassword());
183 }
184 account.setFirstName(updatedAccount.getFirstName());
185 account.setLastName(updatedAccount.getLastName());
186 account.setAddress1(updatedAccount.getAddress1());
187 account.setAddress2(updatedAccount.getAddress2());
188 account.setCity(updatedAccount.getCity());
189 account.setState(updatedAccount.getState());
190 account.setZip(updatedAccount.getZip());
191 account.setCountry(updatedAccount.getCountry());
192 account.setPhone(updatedAccount.getPhone());
193 this.bsAccountManMan.updateAccount(account);
194 userWebSession.getUser().setAccount(account);
195 if (s.isEmpty())
196 pw.print("OK");
197 else
198 pw.print(s);
199 }
200 }
201