Subversion Repositories SmartDukaan

Rev

Rev 7023 | Rev 7825 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7023 Rev 7039
Line 3... Line 3...
3
 */
3
 */
4
package in.shop2020.serving.controllers;
4
package in.shop2020.serving.controllers;
5
 
5
 
6
import in.shop2020.datalogger.EventType;
6
import in.shop2020.datalogger.EventType;
7
import in.shop2020.model.v1.user.Cart;
7
import in.shop2020.model.v1.user.Cart;
8
import in.shop2020.model.v1.user.Sex;
-
 
9
import in.shop2020.model.v1.user.ShoppingCartException;
-
 
10
import in.shop2020.model.v1.user.TrackLogType;
-
 
11
import in.shop2020.model.v1.user.User;
8
import in.shop2020.model.v1.user.User;
12
import in.shop2020.model.v1.user.UserAffiliateException;
-
 
13
import in.shop2020.model.v1.user.UserContextException;
-
 
14
import in.shop2020.model.v1.user.WidgetException;
-
 
15
import in.shop2020.serving.interceptors.TrackingInterceptor;
-
 
16
import in.shop2020.serving.interceptors.UserInterceptor;
-
 
17
import in.shop2020.serving.utils.DesEncrypter;
9
import in.shop2020.serving.utils.DesEncrypter;
18
import in.shop2020.serving.utils.UserMessage;
10
import in.shop2020.serving.utils.UserMessage;
19
import in.shop2020.serving.utils.Utils;
-
 
20
import in.shop2020.thrift.clients.UserClient;
11
import in.shop2020.thrift.clients.UserClient;
21
import in.shop2020.utils.DataLogger;
12
import in.shop2020.utils.DataLogger;
22
 
13
 
23
import java.io.IOException;
14
import java.io.IOException;
24
import java.util.Date;
15
import java.util.Date;
25
import java.util.List;
16
import java.util.List;
26
 
17
 
27
import javax.servlet.http.Cookie;
-
 
28
import javax.servlet.http.HttpServletResponse;
-
 
29
 
-
 
30
import org.apache.commons.lang.RandomStringUtils;
-
 
31
import org.apache.log4j.Logger;
18
import org.apache.log4j.Logger;
32
import org.apache.struts2.ServletActionContext;
-
 
33
import org.apache.struts2.convention.annotation.Action;
19
import org.apache.struts2.convention.annotation.Action;
34
import org.apache.struts2.convention.annotation.Actions;
20
import org.apache.struts2.convention.annotation.Actions;
35
import org.apache.struts2.convention.annotation.InterceptorRef;
21
import org.apache.struts2.convention.annotation.InterceptorRef;
36
import org.apache.struts2.convention.annotation.Result;
22
import org.apache.struts2.convention.annotation.Result;
37
import org.apache.struts2.convention.annotation.Results;
23
import org.apache.struts2.convention.annotation.Results;
38
import org.apache.thrift.TException;
-
 
39
import org.apache.thrift.transport.TTransportException;
-
 
40
 
24
 
41
/**
25
/**
42
 * 
26
 * 
43
 * @author rajveer
27
 * @author rajveer
44
 * 
28
 * 
Line 57... Line 41...
57
 
41
 
58
	private static Logger log = Logger.getLogger(Class.class);
42
	private static Logger log = Logger.getLogger(Class.class);
59
	private DesEncrypter desEncrypter = new DesEncrypter("saholic");
43
	private DesEncrypter desEncrypter = new DesEncrypter("saholic");
60
	private String loginResult = "0";
44
	private String loginResult = "0";
61
	private String redirectUrl = "/";
45
	private String redirectUrl = "/";
62
	private Long userId = -1L;
-
 
63
	
46
 
64
	public LoginController() {
47
	public LoginController() {
65
		super();
48
		super();
66
	}
49
	}
67
	@Actions({
50
	@Actions({
68
		@Action(value="login", interceptorRefs={@InterceptorRef("myDefault")}),
51
		@Action(value="login", interceptorRefs={@InterceptorRef("myDefault")}),
Line 86... Line 69...
86
		}
69
		}
87
	}
70
	}
88
 
71
 
89
	private boolean loginUser() {
72
	private boolean loginUser() {
90
		try {
73
		try {
91
			String email, password, isFacebookUser;
74
			String email, password;
92
 
75
 
93
			email = this.request.getParameter("email");
76
			email = this.request.getParameter("email");
94
			password = this.request.getParameter("password");
77
			password = this.request.getParameter("password");
95
			isFacebookUser = this.request.getParameter("isFacebookUser");
-
 
96
			
78
 
97
			if (email == null || (password == null && (isFacebookUser==null || !isFacebookUser.equals("True")))) {
79
			if (email == null || password == null) {
98
				return false;
80
				return false;
99
			}
81
			}
100
			
82
 
101
			String encryptedPassword = null;
-
 
102
			if(!(password==null)){
-
 
103
				encryptedPassword = desEncrypter.encrypt(password);
83
			String encryptedPassword = desEncrypter.encrypt(password);
104
			}
-
 
105
			
84
			
106
			UserClient userContextServiceClient = new UserClient();
85
			UserClient userContextServiceClient = new UserClient();
107
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
86
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
108
 
-
 
109
			User user = null;
-
 
110
			
-
 
111
			boolean shouldChangeCookieExpiry = false;
-
 
112
			boolean isNewFacebookUser = false;
-
 
113
			if (isFacebookUser!=null && isFacebookUser.equals("True") && !email.equals("undefined")) {
-
 
114
				String accessToken = this.request.getParameter("accessToken");
-
 
115
				String facebookId = this.request.getParameter("facebookId");
-
 
116
				if (userClient.userExists(email)){
-
 
117
					shouldChangeCookieExpiry = true;
-
 
118
					user = userClient.getUserByEmail(email);
-
 
119
					userId = user.getUserId();
-
 
120
					if(!user.isIsFacebookUser()) {
-
 
121
						user.setIsFacebookUser(true);						
-
 
122
						user.setFacebookAccessToken(accessToken);
-
 
123
						user.setFacebookId(facebookId);
-
 
124
						userClient.updateUser(user);
-
 
125
					} else if(user.getFacebookAccessToken() == null || (accessToken!=null && !accessToken.equals(user.getFacebookAccessToken()))){
-
 
126
						user.setFacebookAccessToken(accessToken);
-
 
127
						if(facebookId!=null) {
-
 
128
							user.setFacebookId(facebookId);
-
 
129
						}
-
 
130
						userClient.updateUser(user);
-
 
131
					}
-
 
132
				} else {
-
 
133
					isNewFacebookUser = createNewUserForFacebook(email);
-
 
134
				}
-
 
135
				if(shouldChangeCookieExpiry || isNewFacebookUser) {
-
 
136
					Cookie uidCookie = (Cookie) cookiesMap.get(UserInterceptor.USER_ID_COOKIE_NAME);
-
 
137
					Cookie uicCookie = (Cookie) cookiesMap.get(UserInterceptor.USER_INFO_COOKIE_NAME);
-
 
138
					HttpServletResponse response = ServletActionContext.getResponse();
-
 
139
					if (uidCookie == null) {
-
 
140
						DesEncrypter cookieEncryter = new DesEncrypter(UserInterceptor.COOKIE_DECRYPTION_STRING);
-
 
141
						uidCookie = new Cookie(UserInterceptor.USER_ID_COOKIE_NAME, cookieEncryter.encrypt(userId + ""));
-
 
142
						uidCookie.setPath("/");
-
 
143
						if(!domainName.isEmpty()) {
-
 
144
							uidCookie.setDomain(domainName);
-
 
145
						}
-
 
146
					}
-
 
147
					if (uicCookie != null) {
-
 
148
						uicCookie.setMaxAge(UserInterceptor.FACEBOOK_USER_COOKIE_EXPIRY_TIME);
-
 
149
						cookiesMap.put(UserInterceptor.USER_INFO_COOKIE_NAME, uicCookie);
-
 
150
				        response.addCookie(uicCookie);
-
 
151
					}
-
 
152
					uidCookie.setMaxAge(UserInterceptor.FACEBOOK_USER_COOKIE_EXPIRY_TIME);
-
 
153
					cookiesMap.put(UserInterceptor.USER_ID_COOKIE_NAME, uidCookie);
-
 
154
			        response.addCookie(uidCookie);
-
 
155
			        
-
 
156
			        if(isNewFacebookUser) {
-
 
157
			        	return true;
-
 
158
			        }
-
 
159
				}
-
 
160
			} else {
-
 
161
				user = userClient.authenticateUser(email, encryptedPassword);
87
			User user = userClient.authenticateUser(email, encryptedPassword);
162
			}
-
 
163
			userClient.setUserAsLoggedIn(user.getUserId(),(new Date()).getTime());
88
			userClient.setUserAsLoggedIn(user.getUserId(),(new Date()).getTime());
164
			String pincode = userClient.getDefaultPincode(user.getUserId());
89
			String pincode = userClient.getDefaultPincode(user.getUserId());
165
			
90
			
166
			// TODO: setTotalItems shouldn't be a method on userinfo. This allows
91
			// TODO: setTotalItems shouldn't be a method on userinfo. This allows
167
			// for potentially updating the item count wrongly. The method setCartId
92
			// for potentially updating the item count wrongly. The method setCartId
Line 206... Line 131...
206
			log.error(UserMessage.USER_AUTHENTICATION_FAILURE, e);
131
			log.error(UserMessage.USER_AUTHENTICATION_FAILURE, e);
207
			return false;
132
			return false;
208
		}
133
		}
209
	}
134
	}
210
 
135
 
211
	
-
 
212
	
-
 
213
	
-
 
214
	private boolean createNewUserForFacebook(String email) {
-
 
215
    	String password;
-
 
216
    	password = RandomStringUtils.randomAlphabetic(8);
-
 
217
    	
-
 
218
    	Sex sex = null;
-
 
219
    	String name = this.request.getParameter("name");
-
 
220
    	String gender = this.request.getParameter("gender");
-
 
221
    	if (gender.equals("male")) {
-
 
222
    		sex = Sex.MALE;
-
 
223
    	} else if (gender.equals("female")) {
-
 
224
    		sex = Sex.FEMALE;
-
 
225
    	} else {
-
 
226
    		sex = Sex.WONT_SAY;
-
 
227
    	}
-
 
228
    	String accessToken = this.request.getParameter("accessToken");
-
 
229
    	String facebookId = this.request.getParameter("facebookId");
-
 
230
		User user = new User();
-
 
231
		user.setEmail(email);
-
 
232
		String encryptedPassword = desEncrypter.encrypt(password);
-
 
233
		user.setPassword(encryptedPassword);
-
 
234
		user.setCommunicationEmail(email);
-
 
235
		user.setName(name);
-
 
236
		user.setSex(sex);
-
 
237
		user.setFacebookAccessToken(accessToken);
-
 
238
		if(user.getFacebookId()!=null && user.getFacebookId().isEmpty()) {
-
 
239
			user.setFacebookId(facebookId);
-
 
240
		}
-
 
241
		Cookie sourceCookie = (Cookie) cookiesMap.get(TrackingInterceptor.SRC_COOKIE);
-
 
242
        if (sourceCookie != null) {
-
 
243
            DesEncrypter des = new DesEncrypter(TrackingInterceptor.ENCRIPTION_STRING);
-
 
244
            String sourceCookieVal = des.decrypt(sourceCookie.getValue());
-
 
245
            user.setSource(sourceCookieVal);
-
 
246
        }
-
 
247
        
-
 
248
        Cookie sourceTimeCookie = (Cookie) cookiesMap.get(TrackingInterceptor.SRC_TIME_COOKIE);
-
 
249
        long sourceTime = 0;
-
 
250
        if (sourceTimeCookie != null) {
-
 
251
            try {
-
 
252
                sourceTime = Long.parseLong(sourceTimeCookie.getValue());
-
 
253
            }
-
 
254
            catch (Exception e) {
-
 
255
                log.warn("Unable to parse session src time cookie.");
-
 
256
            }
-
 
257
            user.setSourceStartTime(sourceTime);
-
 
258
        }
-
 
259
		
-
 
260
		user.setSex(Sex.WONT_SAY);
-
 
261
		user.setIsFacebookUser(true);
-
 
262
		
-
 
263
		UserClient userContextServiceClient;
-
 
264
		try {
-
 
265
			userContextServiceClient = new UserClient();
-
 
266
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
-
 
267
			try{
-
 
268
				user = userClient.createUser(user);
-
 
269
			}catch (UserContextException ux){
-
 
270
				addActionError("User already exists with this email id.");
-
 
271
				DataLogger.logData(EventType.REGISTER_FAILED_USER_EXISTS, getSessionId(), userinfo.getUserId(), email, "", email);
-
 
272
				return false;				
-
 
273
			}
-
 
274
			this.userId = user.getUserId();
-
 
275
			long userId = user.getUserId();
-
 
276
			userClient.setUserAsLoggedIn(userId, (new Date()).getTime());
-
 
277
			String pincode = userClient.getDefaultPincode(user.getUserId());
-
 
278
		
-
 
279
			// TODO: setTotalItems shouldn't be a method on userinfo. This allows
-
 
280
			// for potentially updating the item count wrongly. The method setCartId
-
 
281
			// should update the item count as well. Also, there can be a method
-
 
282
			// called refreshItemCount() that automatically updates the number of
-
 
283
			// items currently in the cart.
-
 
284
			if(userinfo.getUserId() != -1){
-
 
285
				userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
-
 
286
			
-
 
287
				List<Long> items = userClient.getBrowseHistoryItems(userinfo.getUserId());
-
 
288
				if(items != null){
-
 
289
					for(Long itemId: items){
-
 
290
						userClient.updateBrowseHistory(user.getUserId(), itemId);
-
 
291
					}
-
 
292
				}
-
 
293
			
-
 
294
				items = userClient.getMyResearchItems(userinfo.getUserId());
-
 
295
				if(items != null){
-
 
296
					for(Long itemId: items){
-
 
297
						userClient.updateMyResearch(user.getUserId(), itemId);
-
 
298
					}
-
 
299
				}
-
 
300
			}
-
 
301
			
-
 
302
			userinfo.setUserId(userId);
-
 
303
			userinfo.setEmail(email);
-
 
304
			userinfo.setLoggedIn(true);
-
 
305
			userinfo.setPincode(pincode);
-
 
306
			userinfo.setCartId(user.getActiveCartId());
-
 
307
			Cart cart = userClient.getCart(userinfo.getCartId());
-
 
308
			userinfo.setTotalItems(cart.getLinesSize());
-
 
309
			userinfo.setTotalAmount(cart.getTotalPrice());
-
 
310
			
-
 
311
	        if (cookiesMap.containsKey(TrackingInterceptor.AFF_COOKIE)) {
-
 
312
	            long affId = Long.parseLong(cookiesMap.get(TrackingInterceptor.AFF_COOKIE).getValue());
-
 
313
	            userClient.addTrackLog(affId, userId, TrackLogType.NEW_REGISTRATION, "",email, (new Date()).getTime());
-
 
314
	        }
-
 
315
	        DataLogger.logData(EventType.REGISTER_SUCCESS, getSessionId(), userinfo.getUserId(), email, "", email);
-
 
316
	
-
 
317
			return true;
-
 
318
		} catch (TTransportException e) {
-
 
319
			log.error("Unable to register user." + e);
-
 
320
		} catch (UserContextException e) {
-
 
321
			log.error("Unable to register user." + e);
-
 
322
		} catch (TException e) {
-
 
323
			log.error("Unable to register user." + e);
-
 
324
		} catch (UserAffiliateException e) {
-
 
325
			log.error("Unable to register user." + e);
-
 
326
		} catch (ShoppingCartException e) {
-
 
327
			log.error("Unable to register user." + e);
-
 
328
		} catch (WidgetException e) {
-
 
329
			log.error("Unable to register user." + e);
-
 
330
		}
-
 
331
		return false;
-
 
332
	}
-
 
333
	
-
 
334
	
-
 
335
	
-
 
336
	public String authenticateUser() {
136
	public String authenticateUser() {
337
		String email, password;
137
		String email, password;
338
 
138
 
339
		email = this.request.getParameter("email");
139
		email = this.request.getParameter("email");
340
		password = this.request.getParameter("password");
140
		password = this.request.getParameter("password");