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