| 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 |
|
|
|
35 |
@InterceptorRefs({
|
|
|
36 |
@InterceptorRef("defaultStack"),
|
|
|
37 |
@InterceptorRef("restDefaultStack")
|
|
|
38 |
})
|
| 637 |
rajveer |
39 |
public class LoginController extends BaseController{
|
|
|
40 |
|
| 650 |
rajveer |
41 |
|
| 637 |
rajveer |
42 |
private static Log log = LogFactory.getLog(LoginController.class);
|
|
|
43 |
|
| 650 |
rajveer |
44 |
private String redirectUrl;
|
| 637 |
rajveer |
45 |
|
|
|
46 |
public LoginController(){
|
|
|
47 |
super();
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
|
| 650 |
rajveer |
51 |
public String index() throws SecurityException, IOException {
|
| 637 |
rajveer |
52 |
htmlSnippets.put("LOGIN_HEADER", pageLoader.getLoginHeaderHtml());
|
|
|
53 |
htmlSnippets.put("LOGIN_FORM", pageLoader.getLoginFormHtml());
|
| 650 |
rajveer |
54 |
return "index";
|
| 637 |
rajveer |
55 |
}
|
|
|
56 |
|
|
|
57 |
public String create() throws SecurityException, Exception {
|
|
|
58 |
|
|
|
59 |
if(loginUser()){
|
| 650 |
rajveer |
60 |
if(getUrl()!=null){
|
|
|
61 |
redirectUrl = getUrl();
|
|
|
62 |
resetRedirectUrl();
|
|
|
63 |
return "redirect";
|
|
|
64 |
}
|
| 637 |
rajveer |
65 |
return "success";
|
|
|
66 |
}
|
|
|
67 |
else
|
|
|
68 |
return "failure";
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
public boolean loginUser(){
|
|
|
72 |
try{
|
|
|
73 |
String email, password;
|
|
|
74 |
|
|
|
75 |
email = this.request.getParameter("email");
|
|
|
76 |
password = this.request.getParameter("password");
|
|
|
77 |
|
|
|
78 |
UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
|
|
|
79 |
in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
|
|
|
80 |
User user = userClient.authenticateUser(email, password);
|
|
|
81 |
userClient.setUserAsLoggedIn(user.getUserId(), (new Date()).getTime());
|
|
|
82 |
|
|
|
83 |
userinfo.setUserId(user.getUserId());
|
|
|
84 |
userinfo.setNameOfUser(user.getName());
|
|
|
85 |
userinfo.setEmail(email);
|
|
|
86 |
userinfo.setLoggedIn(true);
|
|
|
87 |
|
|
|
88 |
// TODO: setTotalItems shouldn't be a method on userinfo. This allows
|
|
|
89 |
// for potentially updating the item count wrongly. The method setCartId
|
|
|
90 |
// should update the item count as well. Also, there can be a method
|
|
|
91 |
// called refreshItemCount() that automatically updates the number of
|
|
|
92 |
// items currently in the cart.
|
|
|
93 |
userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
|
|
|
94 |
userinfo.setCartId(user.getActiveCartId());
|
|
|
95 |
userinfo.setTotalItems(Utils.getNumberOfItemsInCart(userinfo.getCartId()));
|
|
|
96 |
return true;
|
|
|
97 |
}catch(Exception e){
|
|
|
98 |
log.error("Wrong username or password.");
|
|
|
99 |
return false;
|
|
|
100 |
}
|
|
|
101 |
}
|
|
|
102 |
|
| 650 |
rajveer |
103 |
String getUrl(){
|
|
|
104 |
return this.redirectUrl;
|
|
|
105 |
}
|
|
|
106 |
|
| 637 |
rajveer |
107 |
public String getLoginHeaderSnippet(){
|
|
|
108 |
return htmlSnippets.get("LOGIN_HEADER");
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
public String getLoginFormSnippet(){
|
|
|
112 |
return htmlSnippets.get("LOGIN_FORM");
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
}
|