| 637 |
rajveer |
1 |
/**
|
|
|
2 |
*
|
|
|
3 |
*/
|
|
|
4 |
package in.shop2020.serving.controllers;
|
|
|
5 |
|
|
|
6 |
import in.shop2020.model.v1.user.User;
|
| 822 |
vikas |
7 |
import in.shop2020.serving.interceptors.LoginInterceptor;
|
| 815 |
rajveer |
8 |
import in.shop2020.serving.utils.DesEncrypter;
|
| 637 |
rajveer |
9 |
import in.shop2020.thrift.clients.UserContextServiceClient;
|
|
|
10 |
|
|
|
11 |
import java.io.IOException;
|
|
|
12 |
import java.util.Date;
|
|
|
13 |
|
|
|
14 |
import org.apache.juli.logging.Log;
|
|
|
15 |
import org.apache.juli.logging.LogFactory;
|
|
|
16 |
import org.apache.struts2.convention.annotation.Result;
|
|
|
17 |
import org.apache.struts2.convention.annotation.Results;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
*
|
|
|
21 |
* @author rajveer
|
| 781 |
vikas |
22 |
*
|
| 637 |
rajveer |
23 |
*/
|
|
|
24 |
|
|
|
25 |
@Results({
|
| 781 |
vikas |
26 |
@Result(name = "failure", type = "redirectAction", params = {
|
|
|
27 |
"actionName", "login" }),
|
|
|
28 |
@Result(name = "redirect", location = "${url}", type = "redirect") })
|
|
|
29 |
public class LoginController extends BaseController {
|
| 650 |
rajveer |
30 |
|
| 781 |
vikas |
31 |
/**
|
|
|
32 |
*
|
|
|
33 |
*/
|
|
|
34 |
private static final long serialVersionUID = 5390035354379263121L;
|
| 650 |
rajveer |
35 |
|
| 637 |
rajveer |
36 |
private static Log log = LogFactory.getLog(LoginController.class);
|
| 815 |
rajveer |
37 |
private DesEncrypter desEncrypter = new DesEncrypter("saholic");
|
|
|
38 |
|
| 650 |
rajveer |
39 |
private String redirectUrl;
|
| 781 |
vikas |
40 |
|
|
|
41 |
public LoginController() {
|
| 637 |
rajveer |
42 |
super();
|
|
|
43 |
}
|
|
|
44 |
|
| 781 |
vikas |
45 |
public String index() throws SecurityException, IOException {
|
|
|
46 |
htmlSnippets.put("LOGIN_HEADER", pageLoader.getLoginHeaderHtml());
|
| 650 |
rajveer |
47 |
return "index";
|
| 781 |
vikas |
48 |
}
|
| 637 |
rajveer |
49 |
|
| 781 |
vikas |
50 |
public String create() throws SecurityException, Exception {
|
|
|
51 |
if (loginUser()) {
|
| 822 |
vikas |
52 |
redirectUrl = (String) this.session.getAttribute(LoginInterceptor.REDIRECT_URL);
|
| 781 |
vikas |
53 |
if (redirectUrl == null) {
|
|
|
54 |
redirectUrl = "";
|
|
|
55 |
}
|
|
|
56 |
log.debug(redirectUrl);
|
|
|
57 |
resetRedirectUrl();
|
|
|
58 |
return "redirect";
|
|
|
59 |
} else {
|
|
|
60 |
addActionError("Either email or password is wrong.");
|
|
|
61 |
return "failure";
|
|
|
62 |
}
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
private boolean loginUser() {
|
|
|
66 |
try {
|
|
|
67 |
String email, password;
|
|
|
68 |
|
|
|
69 |
email = this.request.getParameter("email");
|
|
|
70 |
password = this.request.getParameter("password");
|
|
|
71 |
|
|
|
72 |
if (email == null || password == null) {
|
|
|
73 |
return false;
|
|
|
74 |
}
|
| 815 |
rajveer |
75 |
|
|
|
76 |
String encryptedPassword = desEncrypter.encrypt(password);
|
|
|
77 |
|
| 781 |
vikas |
78 |
UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
|
|
|
79 |
in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient
|
|
|
80 |
.getClient();
|
| 815 |
rajveer |
81 |
User user = userClient.authenticateUser(email, encryptedPassword);
|
| 793 |
rajveer |
82 |
userClient.setUserAsLoggedIn(user.getUserId(),(new Date()).getTime());
|
|
|
83 |
String pincode = userClient.getDefaultPincode(user.getUserId());
|
| 637 |
rajveer |
84 |
userinfo.setUserId(user.getUserId());
|
|
|
85 |
userinfo.setNameOfUser(user.getName());
|
|
|
86 |
userinfo.setEmail(email);
|
|
|
87 |
userinfo.setLoggedIn(true);
|
| 793 |
rajveer |
88 |
userinfo.setPincode(pincode);
|
| 781 |
vikas |
89 |
|
|
|
90 |
// TODO: setTotalItems shouldn't be a method on userinfo. This
|
|
|
91 |
// allows
|
|
|
92 |
// for potentially updating the item count wrongly. The method
|
|
|
93 |
// setCartId
|
| 637 |
rajveer |
94 |
// should update the item count as well. Also, there can be a method
|
| 781 |
vikas |
95 |
// called refreshItemCount() that automatically updates the number
|
|
|
96 |
// of
|
| 637 |
rajveer |
97 |
// items currently in the cart.
|
|
|
98 |
userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
|
|
|
99 |
userinfo.setCartId(user.getActiveCartId());
|
| 781 |
vikas |
100 |
int totalItems = userClient.getCart(user.getActiveCartId())
|
|
|
101 |
.getLinesSize();
|
| 762 |
rajveer |
102 |
userinfo.setTotalItems(totalItems);
|
|
|
103 |
|
| 637 |
rajveer |
104 |
return true;
|
| 781 |
vikas |
105 |
} catch (Exception e) {
|
|
|
106 |
log.error("Wrong username or password.");
|
|
|
107 |
return false;
|
|
|
108 |
}
|
|
|
109 |
}
|
| 637 |
rajveer |
110 |
|
| 781 |
vikas |
111 |
String getUrl() {
|
|
|
112 |
return this.redirectUrl;
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
public String getLoginHeaderSnippet() {
|
| 637 |
rajveer |
116 |
return htmlSnippets.get("LOGIN_HEADER");
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
}
|