Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
637 rajveer 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.controllers;
5
 
6
import in.shop2020.model.v1.user.User;
815 rajveer 7
import in.shop2020.serving.utils.DesEncrypter;
637 rajveer 8
import in.shop2020.thrift.clients.UserContextServiceClient;
9
 
10
import java.io.IOException;
11
import java.util.Date;
12
 
832 rajveer 13
import org.apache.log4j.Logger;
637 rajveer 14
import org.apache.struts2.convention.annotation.Result;
925 rajveer 15
import org.apache.struts2.convention.annotation.Results;
637 rajveer 16
 
17
/**
18
 * 
19
 * @author rajveer
781 vikas 20
 * 
637 rajveer 21
 */
925 rajveer 22
@Results({
23
	@Result(name="success", type="redirectAction", params = {"actionName" , "home"}),
24
	@Result(name = "redirect", location = "${redirectUrl}", type = "redirect")
25
})
637 rajveer 26
 
781 vikas 27
public class LoginController extends BaseController {
650 rajveer 28
 
781 vikas 29
	/**
30
	 * 
31
	 */
32
	private static final long serialVersionUID = 5390035354379263121L;
650 rajveer 33
 
832 rajveer 34
	private static Logger log = Logger.getLogger(Class.class);
815 rajveer 35
	private DesEncrypter desEncrypter = new DesEncrypter("saholic");
36
 
924 vikas 37
	private String redirectUrl = null;
781 vikas 38
 
39
	public LoginController() {
637 rajveer 40
		super();
41
	}
42
 
781 vikas 43
	public String index() throws SecurityException, IOException {
925 rajveer 44
		if(userinfo.isLoggedIn()){
45
    		return "success";
46
    	}
781 vikas 47
		htmlSnippets.put("LOGIN_HEADER", pageLoader.getLoginHeaderHtml());
650 rajveer 48
		return "index";
781 vikas 49
	}
637 rajveer 50
 
781 vikas 51
	public String create() throws SecurityException, Exception {
52
		if (loginUser()) {
831 vikas 53
			log.info(redirectUrl);
781 vikas 54
			return "redirect";
55
		} else {
56
			addActionError("Either email or password is wrong.");
830 vikas 57
			return "login";
781 vikas 58
		}
59
	}
60
 
61
	private boolean loginUser() {
62
		try {
63
			String email, password;
64
 
65
			email = this.request.getParameter("email");
66
			password = this.request.getParameter("password");
67
 
68
			if (email == null || password == null) {
69
				return false;
70
			}
815 rajveer 71
 
72
			String encryptedPassword =   desEncrypter.encrypt(password);
73
 
781 vikas 74
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
75
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient
76
					.getClient();
815 rajveer 77
			User user = userClient.authenticateUser(email, encryptedPassword);
793 rajveer 78
			userClient.setUserAsLoggedIn(user.getUserId(),(new Date()).getTime());
79
			String pincode = userClient.getDefaultPincode(user.getUserId());
637 rajveer 80
			userinfo.setUserId(user.getUserId());
81
			userinfo.setNameOfUser(user.getName());
82
			userinfo.setEmail(email);
83
			userinfo.setLoggedIn(true);
793 rajveer 84
			userinfo.setPincode(pincode);
781 vikas 85
 
86
			// TODO: setTotalItems shouldn't be a method on userinfo. This
87
			// allows
88
			// for potentially updating the item count wrongly. The method
89
			// setCartId
637 rajveer 90
			// should update the item count as well. Also, there can be a method
781 vikas 91
			// called refreshItemCount() that automatically updates the number
92
			// of
637 rajveer 93
			// items currently in the cart.
94
			userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
95
			userinfo.setCartId(user.getActiveCartId());
781 vikas 96
			int totalItems = userClient.getCart(user.getActiveCartId())
97
					.getLinesSize();
762 rajveer 98
			userinfo.setTotalItems(totalItems);
99
 
637 rajveer 100
			return true;
781 vikas 101
		} catch (Exception e) {
102
			log.error("Wrong username or password.");
103
			return false;
104
		}
105
	}
637 rajveer 106
 
924 vikas 107
	public String getRedirectUrl() {
108
		return redirectUrl;
781 vikas 109
	}
110
 
924 vikas 111
	public void setRedirectUrl(String redirectUrl) {
112
		this.redirectUrl = redirectUrl;
113
	}
114
 
781 vikas 115
	public String getLoginHeaderSnippet() {
637 rajveer 116
		return htmlSnippets.get("LOGIN_HEADER");
117
	}
118
 
119
}