Subversion Repositories SmartDukaan

Rev

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