Subversion Repositories SmartDukaan

Rev

Rev 5510 | Rev 7007 | 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
 
2263 vikas 6
import in.shop2020.datalogger.EventType;
3830 chandransh 7
import in.shop2020.model.v1.user.Cart;
637 rajveer 8
import in.shop2020.model.v1.user.User;
815 rajveer 9
import in.shop2020.serving.utils.DesEncrypter;
1175 varun.gupt 10
import in.shop2020.serving.utils.UserMessage;
3126 rajveer 11
import in.shop2020.thrift.clients.UserClient;
2511 vikas 12
import in.shop2020.utils.DataLogger;
637 rajveer 13
 
14
import java.io.IOException;
15
import java.util.Date;
1623 rajveer 16
import java.util.List;
637 rajveer 17
 
832 rajveer 18
import org.apache.log4j.Logger;
4222 varun.gupt 19
import org.apache.struts2.convention.annotation.Action;
20
import org.apache.struts2.convention.annotation.Actions;
21
import org.apache.struts2.convention.annotation.InterceptorRef;
637 rajveer 22
import org.apache.struts2.convention.annotation.Result;
925 rajveer 23
import org.apache.struts2.convention.annotation.Results;
637 rajveer 24
 
25
/**
26
 * 
27
 * @author rajveer
781 vikas 28
 * 
637 rajveer 29
 */
925 rajveer 30
@Results({
31
	@Result(name="success", type="redirectAction", params = {"actionName" , "home"}),
32
	@Result(name = "redirect", location = "${redirectUrl}", type = "redirect")
33
})
637 rajveer 34
 
781 vikas 35
public class LoginController extends BaseController {
650 rajveer 36
 
781 vikas 37
	/**
38
	 * 
39
	 */
40
	private static final long serialVersionUID = 5390035354379263121L;
650 rajveer 41
 
832 rajveer 42
	private static Logger log = Logger.getLogger(Class.class);
815 rajveer 43
	private DesEncrypter desEncrypter = new DesEncrypter("saholic");
5510 rajveer 44
	private String loginResult = "0";
2933 vikas 45
	private String redirectUrl = "/";
781 vikas 46
 
47
	public LoginController() {
637 rajveer 48
		super();
49
	}
4222 varun.gupt 50
	@Actions({
51
		@Action(value="login", interceptorRefs={@InterceptorRef("myDefault")}),
52
		@Action(value="login-mini", interceptorRefs={@InterceptorRef("myDefault")})
53
	})
781 vikas 54
	public String index() throws SecurityException, IOException {
925 rajveer 55
		if(userinfo.isLoggedIn()){
56
    		return "success";
57
    	}
650 rajveer 58
		return "index";
781 vikas 59
	}
637 rajveer 60
 
781 vikas 61
	public String create() throws SecurityException, Exception {
62
		if (loginUser()) {
2959 chandransh 63
			log.info("Will redirect the user to:" + redirectUrl);
2637 vikas 64
            return "redirect";
781 vikas 65
		} else {
1175 varun.gupt 66
			addActionError(UserMessage.USER_AUTHENTICATION_FAILURE);
3185 vikas 67
            DataLogger.logData(EventType.LOGIN_FAILED, getSessionId(), userinfo.getUserId(), this.request.getParameter("email"));
830 vikas 68
			return "login";
781 vikas 69
		}
70
	}
71
 
72
	private boolean loginUser() {
73
		try {
74
			String email, password;
75
 
76
			email = this.request.getParameter("email");
77
			password = this.request.getParameter("password");
78
 
79
			if (email == null || password == null) {
80
				return false;
81
			}
815 rajveer 82
 
1776 varun.gupt 83
			String encryptedPassword = desEncrypter.encrypt(password);
815 rajveer 84
 
3126 rajveer 85
			UserClient userContextServiceClient = new UserClient();
1747 varun.gupt 86
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
815 rajveer 87
			User user = userClient.authenticateUser(email, encryptedPassword);
793 rajveer 88
			userClient.setUserAsLoggedIn(user.getUserId(),(new Date()).getTime());
89
			String pincode = userClient.getDefaultPincode(user.getUserId());
2637 vikas 90
 
1175 varun.gupt 91
			// TODO: setTotalItems shouldn't be a method on userinfo. This allows
92
			// for potentially updating the item count wrongly. The method setCartId
637 rajveer 93
			// should update the item count as well. Also, there can be a method
1175 varun.gupt 94
			// called refreshItemCount() that automatically updates the number of
637 rajveer 95
			// items currently in the cart.
1625 rajveer 96
			if(userinfo.getUserId() != -1){
1623 rajveer 97
				userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
1625 rajveer 98
 
2982 rajveer 99
				List<Long> items = userClient.getBrowseHistoryItems(userinfo.getUserId());
100
				if(items != null){
101
					for(Long itemId: items){
102
						userClient.updateBrowseHistory(user.getUserId(), itemId);
1623 rajveer 103
					}
104
				}
4453 varun.gupt 105
 
2982 rajveer 106
				items = userClient.getMyResearchItems(userinfo.getUserId());
107
				if(items != null){
108
					for(Long itemId: items){
109
						userClient.updateMyResearch(user.getUserId(), itemId);
1625 rajveer 110
					}
111
				}
1623 rajveer 112
			}
113
 
1625 rajveer 114
			userinfo.setUserId(user.getUserId());
115
			userinfo.setEmail(email);
116
			userinfo.setLoggedIn(true);
117
			userinfo.setPincode(pincode);
637 rajveer 118
			userinfo.setCartId(user.getActiveCartId());
3830 chandransh 119
			Cart cart = userClient.getCart(user.getActiveCartId());
120
			userinfo.setTotalItems(cart.getLinesSize());
121
			userinfo.setTotalAmount(cart.getTotalPrice());
2959 chandransh 122
			log.info(userinfo);
2996 vikas 123
			String src = user.getSource();
124
			if (src == null) {
125
			    src = "";
126
			}
3185 vikas 127
			DataLogger.logData(EventType.LOGIN_SUCCESS, getSessionId(), userinfo.getUserId(),
2996 vikas 128
                    email, src);
2637 vikas 129
    		return true;
781 vikas 130
		} catch (Exception e) {
2959 chandransh 131
			log.error(UserMessage.USER_AUTHENTICATION_FAILURE, e);
781 vikas 132
			return false;
133
		}
134
	}
637 rajveer 135
 
5510 rajveer 136
	public String authenticateUser() {
137
		String email, password;
138
 
139
		email = this.request.getParameter("email");
140
		password = this.request.getParameter("password");
141
 
142
		if (email == null || password == null) {
143
			loginResult = "0";
144
			return "result";
145
		}
146
 
147
		String encryptedPassword = desEncrypter.encrypt(password);
148
		try{
149
			UserClient userContextServiceClient = new UserClient();
150
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
151
			userClient.authenticateUser(email, encryptedPassword);
152
		}catch (Exception e) {
153
			loginResult = "0";
154
			return "result";
155
		}
156
		loginResult = "1";
157
		return "result";
158
	}
159
 
160
	public String getLoginResult() {
161
		return loginResult;
162
	}
163
 
924 vikas 164
	public String getRedirectUrl() {
165
		return redirectUrl;
781 vikas 166
	}
167
 
924 vikas 168
	public void setRedirectUrl(String redirectUrl) {
169
		this.redirectUrl = redirectUrl;
170
	}
6903 anupam.sin 171
 
172
	public static void main(String[] args) {
173
        DesEncrypter des = new DesEncrypter("saholic");
174
        System.out.println(des.decrypt("XvrWIvmYrUAdXqxhovZMSw"));
175
    }
637 rajveer 176
}