Subversion Repositories SmartDukaan

Rev

Rev 832 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 832 Rev 894
Line 1... Line 1...
1
package in.shop2020.serving.controllers;
1
package in.shop2020.serving.controllers;
2
 
2
 
3
 
3
 
-
 
4
import java.util.ArrayList;
-
 
5
import java.util.List;
-
 
6
import java.util.Random;
-
 
7
 
4
import in.shop2020.model.v1.user.UserContextService.Client;
8
import in.shop2020.model.v1.user.UserContextService.Client;
5
import in.shop2020.serving.controllers.BaseController;
9
import in.shop2020.serving.controllers.BaseController;
-
 
10
import in.shop2020.serving.utils.DesEncrypter;
-
 
11
import in.shop2020.thrift.clients.HelperServiceClient;
6
import in.shop2020.thrift.clients.UserContextServiceClient;
12
import in.shop2020.thrift.clients.UserContextServiceClient;
-
 
13
import in.shop2020.utils.HelperService;
-
 
14
import in.shop2020.utils.Mail;
7
 
15
 
8
import org.apache.juli.logging.Log;
16
import org.apache.juli.logging.Log;
9
import org.apache.juli.logging.LogFactory;
17
import org.apache.juli.logging.LogFactory;
10
import org.apache.log4j.Logger;
18
import org.apache.log4j.Logger;
11
import org.apache.struts2.convention.annotation.Result;
19
import org.apache.struts2.convention.annotation.Result;
Line 18... Line 26...
18
    		params = {"actionName" , "forgot-password"})
26
    		params = {"actionName" , "forgot-password"})
19
})
27
})
20
public class ForgotPasswordController extends BaseController{
28
public class ForgotPasswordController extends BaseController{
21
	
29
	
22
	private static final long serialVersionUID = 1L;
30
	private static final long serialVersionUID = 1L;
-
 
31
	private static final String chars = "abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
-
 
32
    private static final Random random = new Random();
-
 
33
    private static final int LENGTH = 10;
-
 
34
    
23
	private static Logger log = Logger.getLogger(Class.class);
35
	private static Logger log = Logger.getLogger(Class.class);
-
 
36
	private DesEncrypter desEncrypter = new DesEncrypter("saholic");
24
	
37
	
-
 
38
    
25
		public ForgotPasswordController(){
39
		public ForgotPasswordController(){
26
			super();	
40
			super();	
27
		}
41
		}
28
		
42
		
29
		 // GET /Forgot password page
43
		 // GET /Forgot password page
Line 38... Line 52...
38
	    	if(emailId != null){
52
	    	if(emailId != null){
39
	    		UserContextServiceClient userContextServiceClient;
53
	    		UserContextServiceClient userContextServiceClient;
40
				try {
54
				try {
41
					userContextServiceClient = new UserContextServiceClient();
55
					userContextServiceClient = new UserContextServiceClient();
42
		    		Client client = userContextServiceClient.getClient();
56
		    		Client client = userContextServiceClient.getClient();
43
		    		if(client.forgotPassword(emailId)){
57
		    		if(!client.userExists(emailId)){
44
		    			addActionMessage("Password sent to your email address.");
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)){
45
		    			return "success";
65
		    				return "success";
-
 
66
		    			}else{
-
 
67
		    				return "failure";
-
 
68
		    			}
-
 
69
		    			
46
		    		}else{
70
		    		}else{
47
		    			addActionError("Email address is not registered with us.");
71
		    			addActionError("Email address is not registered with us.");
48
		    			return "failure";
72
		    			return "failure";
49
		    		}
73
		    		}
50
				} catch (Exception e) {
74
				} catch (Exception e) {
Line 52... Line 76...
52
					addActionError("Something went wrong. Try again.");
76
					addActionError("Something went wrong. Try again.");
53
				}
77
				}
54
	    	}
78
	    	}
55
	    	return "failure";
79
	    	return "failure";
56
	    }
80
	    }
57
 
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();
-
 
91
				mail.setSubject("Reset password request");
-
 
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();
-
 
98
			}
-
 
99
			return true;
-
 
100
		}
-
 
101
		
-
 
102
		private static String generateNewPassword() {
-
 
103
		    char[] buf = new char[LENGTH];
-
 
104
	        for (int i = 0; i < buf.length; i++) {
-
 
105
	            buf[i] = chars.charAt(random.nextInt(chars.length()));
-
 
106
	        }
-
 
107
	        return new String(buf);
-
 
108
	    }
-
 
109
		 
58
}
110
}
59
111