| 569 |
rajveer |
1 |
package in.shop2020.serving.controllers;
|
|
|
2 |
|
|
|
3 |
|
| 894 |
rajveer |
4 |
import java.util.ArrayList;
|
|
|
5 |
import java.util.List;
|
|
|
6 |
import java.util.Random;
|
|
|
7 |
|
| 569 |
rajveer |
8 |
import in.shop2020.model.v1.user.UserContextService.Client;
|
|
|
9 |
import in.shop2020.serving.controllers.BaseController;
|
| 894 |
rajveer |
10 |
import in.shop2020.serving.utils.DesEncrypter;
|
|
|
11 |
import in.shop2020.thrift.clients.HelperServiceClient;
|
| 569 |
rajveer |
12 |
import in.shop2020.thrift.clients.UserContextServiceClient;
|
| 894 |
rajveer |
13 |
import in.shop2020.utils.HelperService;
|
|
|
14 |
import in.shop2020.utils.Mail;
|
| 569 |
rajveer |
15 |
|
|
|
16 |
import org.apache.juli.logging.Log;
|
|
|
17 |
import org.apache.juli.logging.LogFactory;
|
| 832 |
rajveer |
18 |
import org.apache.log4j.Logger;
|
| 627 |
rajveer |
19 |
import org.apache.struts2.convention.annotation.Result;
|
|
|
20 |
import org.apache.struts2.convention.annotation.Results;
|
| 569 |
rajveer |
21 |
import org.apache.struts2.rest.DefaultHttpHeaders;
|
|
|
22 |
import org.apache.struts2.rest.HttpHeaders;
|
|
|
23 |
|
| 627 |
rajveer |
24 |
@Results({
|
|
|
25 |
@Result(name="failure", type="redirectAction",
|
|
|
26 |
params = {"actionName" , "forgot-password"})
|
|
|
27 |
})
|
| 569 |
rajveer |
28 |
public class ForgotPasswordController extends BaseController{
|
|
|
29 |
|
|
|
30 |
private static final long serialVersionUID = 1L;
|
| 894 |
rajveer |
31 |
private static final String chars = "abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
|
32 |
private static final Random random = new Random();
|
|
|
33 |
private static final int LENGTH = 10;
|
|
|
34 |
|
| 832 |
rajveer |
35 |
private static Logger log = Logger.getLogger(Class.class);
|
| 894 |
rajveer |
36 |
private DesEncrypter desEncrypter = new DesEncrypter("saholic");
|
| 569 |
rajveer |
37 |
|
| 894 |
rajveer |
38 |
|
| 569 |
rajveer |
39 |
public ForgotPasswordController(){
|
|
|
40 |
super();
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
// GET /Forgot password page
|
|
|
44 |
public HttpHeaders index() {
|
|
|
45 |
return new DefaultHttpHeaders("index").disableCaching();
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
// POST /Forgot password
|
|
|
49 |
public String create() {
|
|
|
50 |
log.info("ForgotPasswordController.create");
|
|
|
51 |
String emailId = request.getParameter("emailId");
|
|
|
52 |
if(emailId != null){
|
|
|
53 |
UserContextServiceClient userContextServiceClient;
|
|
|
54 |
try {
|
|
|
55 |
userContextServiceClient = new UserContextServiceClient();
|
|
|
56 |
Client client = userContextServiceClient.getClient();
|
| 894 |
rajveer |
57 |
if(!client.userExists(emailId)){
|
|
|
58 |
addActionError("Email address is not registered with us.");
|
|
|
59 |
return "failure";
|
|
|
60 |
}
|
|
|
61 |
String newPassword = generateNewPassword();
|
|
|
62 |
String encryptedPassword = desEncrypter.encrypt(newPassword);
|
|
|
63 |
if(client.forgotPassword(emailId, encryptedPassword)){
|
|
|
64 |
if(mailNewPassword(emailId, newPassword)){
|
|
|
65 |
return "success";
|
|
|
66 |
}else{
|
|
|
67 |
return "failure";
|
|
|
68 |
}
|
|
|
69 |
|
| 627 |
rajveer |
70 |
}else{
|
|
|
71 |
addActionError("Email address is not registered with us.");
|
|
|
72 |
return "failure";
|
|
|
73 |
}
|
| 569 |
rajveer |
74 |
} catch (Exception e) {
|
|
|
75 |
e.printStackTrace();
|
| 627 |
rajveer |
76 |
addActionError("Something went wrong. Try again.");
|
| 569 |
rajveer |
77 |
}
|
|
|
78 |
}
|
| 627 |
rajveer |
79 |
return "failure";
|
| 569 |
rajveer |
80 |
}
|
| 894 |
rajveer |
81 |
|
|
|
82 |
private boolean mailNewPassword(String emailId, String newPassword) {
|
|
|
83 |
List<String> toList = new ArrayList<String>();
|
|
|
84 |
toList.add(emailId);
|
|
|
85 |
|
|
|
86 |
HelperServiceClient helperServiceClient = null;
|
|
|
87 |
try {
|
|
|
88 |
helperServiceClient = new HelperServiceClient();
|
|
|
89 |
in.shop2020.utils.HelperService.Client client = helperServiceClient.getClient();
|
|
|
90 |
Mail mail = new Mail();
|
| 902 |
rajveer |
91 |
mail.setSubject("Password reset request");
|
| 894 |
rajveer |
92 |
mail.setTo(toList);
|
|
|
93 |
mail.setData("Your new password is: " + newPassword);
|
|
|
94 |
client.sendMail(mail);
|
|
|
95 |
} catch (Exception e) {
|
|
|
96 |
// TODO Auto-generated catch block
|
|
|
97 |
e.printStackTrace();
|
| 904 |
rajveer |
98 |
return false;
|
| 894 |
rajveer |
99 |
}
|
|
|
100 |
return true;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
private static String generateNewPassword() {
|
|
|
104 |
char[] buf = new char[LENGTH];
|
|
|
105 |
for (int i = 0; i < buf.length; i++) {
|
|
|
106 |
buf[i] = chars.charAt(random.nextInt(chars.length()));
|
|
|
107 |
}
|
|
|
108 |
return new String(buf);
|
|
|
109 |
}
|
|
|
110 |
|
| 569 |
rajveer |
111 |
}
|