Subversion Repositories SmartDukaan

Rev

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

Rev 20184 Rev 20202
Line 34... Line 34...
34
import in.shop2020.serving.interceptors.TrackingInterceptor;
34
import in.shop2020.serving.interceptors.TrackingInterceptor;
35
import in.shop2020.serving.interceptors.UserInterceptor;
35
import in.shop2020.serving.interceptors.UserInterceptor;
36
import in.shop2020.serving.utils.DesEncrypter;
36
import in.shop2020.serving.utils.DesEncrypter;
37
import in.shop2020.serving.utils.FacebookUtility;
37
import in.shop2020.serving.utils.FacebookUtility;
38
import in.shop2020.serving.utils.UserMessage;
38
import in.shop2020.serving.utils.UserMessage;
-
 
39
import in.shop2020.serving.utils.Utils;
39
import in.shop2020.thrift.clients.UserClient;
40
import in.shop2020.thrift.clients.UserClient;
40
 
41
 
41
/**
42
/**
42
 * 
43
 * 
43
 * @author rajveer
44
 * @author rajveer
Line 76... Line 77...
76
    	}
77
    	}
77
		return "index";
78
		return "index";
78
	}
79
	}
79
 
80
 
80
	public String create() throws SecurityException, Exception {
81
	public String create() throws SecurityException, Exception {
81
		if (loginUser()) {
82
		loginUser();
-
 
83
		return "login-response";
82
			log.info("Will redirect the user to:" + redirectUrl);
84
		//	log.info("Will redirect the user to:" + redirectUrl);
83
/*			if(userinfo.isPrivateDealUser()){
85
/*			if(userinfo.isPrivateDealUser()){
84
				addActionMessage("PrivateDealUser");
86
				addActionMessage("PrivateDealUser");
85
			}*/
87
			}*/
86
            return "redirect";
88
           // return "redirect";
87
		} else {
89
		//} else {
88
			log.info("in else of loginuser");
90
		//	log.info("in else of loginuser");
89
			addActionError(UserMessage.USER_AUTHENTICATION_FAILURE);
91
		//	addActionError(UserMessage.USER_AUTHENTICATION_FAILURE);
90
//            DataLogger.logData(EventType.LOGIN_FAILED, getSessionId(), userinfo.getUserId(), this.request.getParameter("email"));
92
//            DataLogger.logData(EventType.LOGIN_FAILED, getSessionId(), userinfo.getUserId(), this.request.getParameter("email"));
91
			return "login";
93
		//	return "login";
92
		}
94
		//}
93
	}
95
	}
94
 
96
 
95
	public String verifyFbUser(){
97
	public String verifyFbUser(){
96
		JSONObject json = new JSONObject();
98
		JSONObject json = new JSONObject();
97
		try{
99
		try{
Line 199... Line 201...
199
			e.printStackTrace();
201
			e.printStackTrace();
200
		}
202
		}
201
	}
203
	}
202
	
204
	
203
	
205
	
-
 
206
	public String signupFacebookUser(){
-
 
207
		try{
-
 
208
			JSONObject json = new JSONObject();
-
 
209
			String email  =  this.request.getParameter("email");
-
 
210
			String accessToken = this.request.getParameter("accessToken");
-
 
211
			UserClient userContextServiceClient = new UserClient();
-
 
212
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
-
 
213
			User user = userClient.getUserByEmail(email);
-
 
214
			if(user==null ){
-
 
215
				//if user do not exists
-
 
216
				//verify its accessToken
-
 
217
			JSONObject result=	new FacebookUtility().verifyFbToken(accessToken,email,user.isIsFacebookUser(),user.getFacebookId());
-
 
218
			if("true".equalsIgnoreCase(result.getString("status"))){
-
 
219
				//if token verified
-
 
220
				//create facebook user/register user
-
 
221
				boolean res = createNewUserForFacebook(email);
-
 
222
				if(res){
-
 
223
					json.put("status", "fail");
-
 
224
					json.put("message", getActionErrors().iterator().next());
-
 
225
				}else{
-
 
226
				json.put("status", "success");
-
 
227
				}
-
 
228
				setFbLoginCookie(userClient, user);
-
 
229
			}else{
-
 
230
				json.put("status", "fail");
-
 
231
				json.put("message", "Some error occurred. Please try again.");
-
 
232
			}
-
 
233
			}else{
-
 
234
				//no such user exists
-
 
235
				//ask him to signup
-
 
236
				json.put("status", "fail");
-
 
237
				json.put("message", "You are already a member. Please login.");
-
 
238
			}
-
 
239
			setLoginJson(json.toString());
-
 
240
		}catch(Exception e){
-
 
241
			e.printStackTrace();
-
 
242
		}
-
 
243
		return "register-response";
-
 
244
	}
-
 
245
	
204
	
246
	
205
	private boolean loginUser() {
247
	private boolean loginUser() {
-
 
248
		JSONObject json = new JSONObject();
206
		try {
249
		try {
207
			log.info("in loginuser");
250
			log.info("in loginuser");
208
			String email, password = null, isFacebookUser;
251
			String email, password = null;
209
 
-
 
210
			isFacebookUser = (String)this.request.getParameter("isFacebookUser");
-
 
211
			if (isFacebookUser!=null && isFacebookUser.equals("true")) {
-
 
212
				email = (String)this.request.getParameter("email");
-
 
213
			} else {
-
 
214
				email = this.request.getParameter("email");
252
				email = this.request.getParameter("email");
215
				password = this.request.getParameter("password");
253
				password = this.request.getParameter("password");
-
 
254
				boolean isValid = true;
-
 
255
				/*if(!Utils.isValidEmail(email))	{
-
 
256
		    		//addActionError("Please enter valid email address.");
-
 
257
		    		json.put("message", "Please enter valid email address.");
-
 
258
		    		json.put("status", "error");
-
 
259
		    		isValid = false;
216
			}
260
		        }*/
-
 
261
		    	if(password == null )	{
-
 
262
		    		//addActionError("Please enter password.");
217
			log.info("here email "+email+" password "+password+" isfbuser "+isFacebookUser);
263
		    		json.put("message", "Please enter password.");
-
 
264
		    		json.put("status", "error");
-
 
265
		    		isValid = false;
-
 
266
		        }
218
			
267
		    	
-
 
268
		    	if(!isValid){
219
			if (email == null || (password == null && (isFacebookUser==null || !isFacebookUser.equals("true")))) {
269
		    		setLoginJson(json.toString());
220
				log.info("email "+email+" password "+password+" isfbuser "+isFacebookUser);
270
//		    	    DataLogger.logData(EventType.REGISTER_DATA_INCOMPLETE, getSessionId(), userinfo.getUserId(), email, "", email);
221
				return false;
271
		    		return isValid;
222
			}
272
		    	}
223
			
-
 
224
			String encryptedPassword = null;
273
			String encryptedPassword = null;
225
			if(!(password==null)){
274
			if(password!=null){
226
				encryptedPassword = desEncrypter.encrypt(password);
275
				encryptedPassword = desEncrypter.encrypt(password);
227
			}
276
			}
228
			
-
 
229
			UserClient userContextServiceClient = new UserClient();
277
			UserClient userContextServiceClient = new UserClient();
230
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
278
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
231
 
-
 
232
			User user = null;
279
			User user = null;
233
			
-
 
234
			boolean shouldChangeCookieExpiry = false;
280
			boolean shouldChangeCookieExpiry = false;
235
			boolean isNewFacebookUser = false;
-
 
236
			if (isFacebookUser!=null && isFacebookUser.equals("true") && !email.equals("undefined")) {
-
 
237
				String accessToken = (String)this.request.getParameter("accessToken");
-
 
238
				String facebookId = (String)this.request.getParameter("facebookId");
-
 
239
				if (userClient.userExists(email)){
281
				if (userClient.userExists(email)){
240
					shouldChangeCookieExpiry = true;
282
					shouldChangeCookieExpiry = true;
241
					user = userClient.getUserByEmail(email);
-
 
242
					userId = user.getUserId();
-
 
243
					if(!user.isIsFacebookUser()) {
-
 
244
						user.setIsFacebookUser(true);						
-
 
245
						user.setFacebookAccessToken(accessToken);
-
 
246
						user.setFacebookId(facebookId);
-
 
247
						userClient.updateUser(user);
-
 
248
					} else if(user.getFacebookAccessToken() == null || (accessToken!=null && !accessToken.equals(user.getFacebookAccessToken()))){
-
 
249
						user.setFacebookAccessToken(accessToken);
-
 
250
						if(facebookId!=null) {
-
 
251
							user.setFacebookId(facebookId);
-
 
252
						}
-
 
253
						userClient.updateUser(user);
-
 
254
					}
-
 
255
				} else {
-
 
256
					isNewFacebookUser = createNewUserForFacebook(email);
-
 
257
				}
283
				
258
				if(shouldChangeCookieExpiry || isNewFacebookUser) {
284
				if(shouldChangeCookieExpiry) {
259
					Cookie uidCookie = (Cookie) cookiesMap.get(UserInterceptor.USER_ID_COOKIE_NAME);
285
					Cookie uidCookie = (Cookie) cookiesMap.get(UserInterceptor.USER_ID_COOKIE_NAME);
260
					Cookie uicCookie = (Cookie) cookiesMap.get(UserInterceptor.USER_INFO_COOKIE_NAME);
286
					Cookie uicCookie = (Cookie) cookiesMap.get(UserInterceptor.USER_INFO_COOKIE_NAME);
261
					HttpServletResponse response = ServletActionContext.getResponse();
287
					HttpServletResponse response = ServletActionContext.getResponse();
262
					if (uidCookie == null) {
288
					if (uidCookie == null) {
263
						DesEncrypter cookieEncryter = new DesEncrypter(UserInterceptor.COOKIE_DECRYPTION_STRING);
289
						DesEncrypter cookieEncryter = new DesEncrypter(UserInterceptor.COOKIE_DECRYPTION_STRING);
Line 274... Line 300...
274
					}
300
					}
275
					uidCookie.setMaxAge(UserInterceptor.FACEBOOK_USER_COOKIE_EXPIRY_TIME);
301
					uidCookie.setMaxAge(UserInterceptor.FACEBOOK_USER_COOKIE_EXPIRY_TIME);
276
					cookiesMap.put(UserInterceptor.USER_ID_COOKIE_NAME, uidCookie);
302
					cookiesMap.put(UserInterceptor.USER_ID_COOKIE_NAME, uidCookie);
277
			        response.addCookie(uidCookie);
303
			        response.addCookie(uidCookie);
278
			        
304
			        
279
			        if(isNewFacebookUser) {
-
 
280
			        	return true;
-
 
281
			        }
-
 
282
				}
305
				}
283
			} else {
306
			//} else {
284
				user = userClient.authenticateUser(email, encryptedPassword);
307
				user = userClient.authenticateUser(email, encryptedPassword);
-
 
308
			}else{
-
 
309
				json.put("message", "Login Id/password is wrong");
-
 
310
	    		json.put("status", "error");
-
 
311
	    		setLoginJson(json.toString());
285
			}
312
			}
286
			userClient.setUserAsLoggedIn(user.getUserId(),(new Date()).getTime());
313
			userClient.setUserAsLoggedIn(user.getUserId(),(new Date()).getTime());
287
			String pincode = userClient.getDefaultPincode(user.getUserId());
314
			String pincode = userClient.getDefaultPincode(user.getUserId());
288
			
-
 
289
			// TODO: setTotalItems shouldn't be a method on userinfo. This allows
315
			// TODO: setTotalItems shouldn't be a method on userinfo. This allows
290
			// for potentially updating the item count wrongly. The method setCartId
316
			// for potentially updating the item count wrongly. The method setCartId
291
			// should update the item count as well. Also, there can be a method
317
			// should update the item count as well. Also, there can be a method
292
			// called refreshItemCount() that automatically updates the number of
318
			// called refreshItemCount() that automatically updates the number of
293
			// items currently in the cart.
319
			// items currently in the cart.
294
			if(userinfo.getUserId() != -1){
320
			if(userinfo.getUserId() != -1){
295
				userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
321
				userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
296
				
-
 
297
				List<Long> items = userClient.getBrowseHistoryItems(userinfo.getUserId());
322
				List<Long> items = userClient.getBrowseHistoryItems(userinfo.getUserId());
298
				if(items != null){
323
				if(items != null){
299
					for(Long itemId: items){
324
					for(Long itemId: items){
300
						userClient.updateBrowseHistory(user.getUserId(), itemId);
325
						userClient.updateBrowseHistory(user.getUserId(), itemId);
301
					}
326
					}
302
				}
327
				}
303
				
-
 
304
				items = userClient.getMyResearchItems(userinfo.getUserId());
328
				items = userClient.getMyResearchItems(userinfo.getUserId());
305
				if(items != null){
329
				if(items != null){
306
					for(Long itemId: items){
330
					for(Long itemId: items){
307
						userClient.updateMyResearch(user.getUserId(), itemId);
331
						userClient.updateMyResearch(user.getUserId(), itemId);
308
					}
332
					}
309
				}
333
				}
310
			}
334
			}
311
			
-
 
312
			userinfo.setUserId(user.getUserId());
335
			userinfo.setUserId(user.getUserId());
313
			userinfo.setEmail(email);
336
			userinfo.setEmail(email);
314
			userinfo.setLoggedIn(true);
337
			userinfo.setLoggedIn(true);
315
			userinfo.setPincode(pincode);
338
			userinfo.setPincode(pincode);
316
			userinfo.setCartId(user.getActiveCartId());
339
			userinfo.setCartId(user.getActiveCartId());
Line 322... Line 345...
322
			if (src == null) {
345
			if (src == null) {
323
			    src = "";
346
			    src = "";
324
			}
347
			}
325
//			DataLogger.logData(EventType.LOGIN_SUCCESS, getSessionId(), userinfo.getUserId(),
348
//			DataLogger.logData(EventType.LOGIN_SUCCESS, getSessionId(), userinfo.getUserId(),
326
//                    email, src);
349
//                    email, src);
-
 
350
			json.put("message", "Login success");
-
 
351
    		json.put("status", "success");
-
 
352
    		setLoginJson(json.toString());
327
    		return true;
353
    		return true;
328
		} catch (Exception e) {
354
		} catch (Exception e) {
329
			log.error(UserMessage.USER_AUTHENTICATION_FAILURE, e);
355
			log.error(UserMessage.USER_AUTHENTICATION_FAILURE, e);
-
 
356
			setLoginJson("{\"status\":\"error\",\"message\":\"LoginId/password wrong\"}");
330
			return false;
357
			return false;
331
		}
358
		}
332
	}
359
	}
333
 
-
 
334
	
-
 
335
	
-
 
336
	
360
	
337
	private boolean createNewUserForFacebook(String email) {
361
	private boolean createNewUserForFacebook(String email) {
338
    	String password;
362
    	String password;
339
    	password = RandomStringUtils.randomAlphabetic(8);
363
    	password = RandomStringUtils.randomAlphabetic(8);
340
    	
364