| 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;
|
| 3126 |
rajveer |
11 |
import in.shop2020.thrift.clients.HelperClient;
|
|
|
12 |
import in.shop2020.thrift.clients.UserClient;
|
| 894 |
rajveer |
13 |
import in.shop2020.utils.Mail;
|
| 569 |
rajveer |
14 |
|
| 832 |
rajveer |
15 |
import org.apache.log4j.Logger;
|
| 627 |
rajveer |
16 |
import org.apache.struts2.convention.annotation.Result;
|
|
|
17 |
import org.apache.struts2.convention.annotation.Results;
|
| 569 |
rajveer |
18 |
import org.apache.struts2.rest.DefaultHttpHeaders;
|
|
|
19 |
import org.apache.struts2.rest.HttpHeaders;
|
|
|
20 |
|
| 627 |
rajveer |
21 |
@Results({
|
|
|
22 |
@Result(name="failure", type="redirectAction",
|
|
|
23 |
params = {"actionName" , "forgot-password"})
|
|
|
24 |
})
|
| 569 |
rajveer |
25 |
public class ForgotPasswordController extends BaseController{
|
|
|
26 |
|
|
|
27 |
private static final long serialVersionUID = 1L;
|
| 894 |
rajveer |
28 |
private static final String chars = "abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
|
29 |
private static final Random random = new Random();
|
|
|
30 |
private static final int LENGTH = 10;
|
|
|
31 |
|
| 832 |
rajveer |
32 |
private static Logger log = Logger.getLogger(Class.class);
|
| 894 |
rajveer |
33 |
private DesEncrypter desEncrypter = new DesEncrypter("saholic");
|
| 569 |
rajveer |
34 |
|
| 894 |
rajveer |
35 |
|
| 569 |
rajveer |
36 |
public ForgotPasswordController(){
|
|
|
37 |
super();
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
// GET /Forgot password page
|
|
|
41 |
public HttpHeaders index() {
|
|
|
42 |
return new DefaultHttpHeaders("index").disableCaching();
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
// POST /Forgot password
|
|
|
46 |
public String create() {
|
|
|
47 |
log.info("ForgotPasswordController.create");
|
|
|
48 |
String emailId = request.getParameter("emailId");
|
| 8745 |
manish.sha |
49 |
if(emailId != null && !emailId.isEmpty() && !emailId.contains(" ")){
|
| 3126 |
rajveer |
50 |
UserClient userContextServiceClient;
|
| 569 |
rajveer |
51 |
try {
|
| 3126 |
rajveer |
52 |
userContextServiceClient = new UserClient();
|
| 569 |
rajveer |
53 |
Client client = userContextServiceClient.getClient();
|
| 894 |
rajveer |
54 |
if(!client.userExists(emailId)){
|
|
|
55 |
addActionError("Email address is not registered with us.");
|
|
|
56 |
return "failure";
|
|
|
57 |
}
|
|
|
58 |
String newPassword = generateNewPassword();
|
|
|
59 |
String encryptedPassword = desEncrypter.encrypt(newPassword);
|
|
|
60 |
if(client.forgotPassword(emailId, encryptedPassword)){
|
|
|
61 |
if(mailNewPassword(emailId, newPassword)){
|
|
|
62 |
return "success";
|
|
|
63 |
}else{
|
|
|
64 |
return "failure";
|
|
|
65 |
}
|
|
|
66 |
|
| 627 |
rajveer |
67 |
}else{
|
|
|
68 |
addActionError("Email address is not registered with us.");
|
|
|
69 |
return "failure";
|
|
|
70 |
}
|
| 569 |
rajveer |
71 |
} catch (Exception e) {
|
| 2949 |
chandransh |
72 |
log.error("Unexpected error while processing forgot password request", e);
|
| 627 |
rajveer |
73 |
addActionError("Something went wrong. Try again.");
|
| 569 |
rajveer |
74 |
}
|
|
|
75 |
}
|
| 8745 |
manish.sha |
76 |
else{
|
|
|
77 |
addActionError("Please enter valid email Id...");
|
|
|
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 |
|
| 3126 |
rajveer |
86 |
HelperClient helperServiceClient = null;
|
| 894 |
rajveer |
87 |
try {
|
| 3126 |
rajveer |
88 |
helperServiceClient = new HelperClient();
|
| 894 |
rajveer |
89 |
in.shop2020.utils.HelperService.Client client = helperServiceClient.getClient();
|
| 8181 |
rajveer |
90 |
client.saveUserEmailForSending(toList, "help@saholic.com", "Password reset request", "Your new password is: " + newPassword, "", "ForgotPassword", new ArrayList<String>(), new ArrayList<String>(), 1);
|
| 894 |
rajveer |
91 |
} catch (Exception e) {
|
| 2949 |
chandransh |
92 |
log.error("Unexpected error while mailing the new password");
|
| 904 |
rajveer |
93 |
return false;
|
| 894 |
rajveer |
94 |
}
|
|
|
95 |
return true;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
private static String generateNewPassword() {
|
|
|
99 |
char[] buf = new char[LENGTH];
|
|
|
100 |
for (int i = 0; i < buf.length; i++) {
|
|
|
101 |
buf[i] = chars.charAt(random.nextInt(chars.length()));
|
|
|
102 |
}
|
|
|
103 |
return new String(buf);
|
|
|
104 |
}
|
|
|
105 |
|
| 569 |
rajveer |
106 |
}
|