Subversion Repositories SmartDukaan

Rev

Rev 3126 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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");
49
	    	if(emailId != null){
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
	    	}
627 rajveer 76
	    	return "failure";
569 rajveer 77
	    }
894 rajveer 78
 
79
		private boolean mailNewPassword(String emailId, String newPassword) {
80
			List<String> toList = new ArrayList<String>();
81
			toList.add(emailId);
82
 
3126 rajveer 83
			HelperClient helperServiceClient = null;
894 rajveer 84
			try {
3126 rajveer 85
				helperServiceClient = new HelperClient();
894 rajveer 86
				in.shop2020.utils.HelperService.Client client = helperServiceClient.getClient();
8181 rajveer 87
				client.saveUserEmailForSending(toList, "help@saholic.com", "Password reset request", "Your new password is: " + newPassword, "", "ForgotPassword", new ArrayList<String>(), new ArrayList<String>(), 1);
894 rajveer 88
			} catch (Exception e) {
2949 chandransh 89
				log.error("Unexpected error while mailing the new password");
904 rajveer 90
				return false;
894 rajveer 91
			}
92
			return true;
93
		}
94
 
95
		private static String generateNewPassword() {
96
		    char[] buf = new char[LENGTH];
97
	        for (int i = 0; i < buf.length; i++) {
98
	            buf[i] = chars.charAt(random.nextInt(chars.length()));
99
	        }
100
	        return new String(buf);
101
	    }
102
 
569 rajveer 103
}