Subversion Repositories SmartDukaan

Rev

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

Rev 20178 Rev 20180
Line 1... Line 1...
1
/**
1
/**
2
 * 
2
 * 
3
 */
3
 */
4
package in.shop2020.serving.controllers;
4
package in.shop2020.serving.controllers;
5
 
5
 
6
import in.shop2020.datalogger.EventType;
-
 
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;
-
 
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;
-
 
18
import in.shop2020.serving.utils.UserMessage;
-
 
19
import in.shop2020.thrift.clients.UserClient;
-
 
20
import in.shop2020.utils.DataLogger;
-
 
21
 
-
 
22
import java.io.IOException;
6
import java.io.IOException;
23
import java.util.Date;
7
import java.util.Date;
24
import java.util.List;
8
import java.util.List;
25
 
9
 
26
import javax.servlet.http.Cookie;
10
import javax.servlet.http.Cookie;
Line 37... Line 21...
37
import org.apache.struts2.convention.annotation.Results;
21
import org.apache.struts2.convention.annotation.Results;
38
import org.apache.thrift.TException;
22
import org.apache.thrift.TException;
39
import org.apache.thrift.transport.TTransportException;
23
import org.apache.thrift.transport.TTransportException;
40
import org.json.JSONObject;
24
import org.json.JSONObject;
41
 
25
 
-
 
26
import in.shop2020.model.v1.user.Cart;
-
 
27
import in.shop2020.model.v1.user.Sex;
-
 
28
import in.shop2020.model.v1.user.ShoppingCartException;
-
 
29
import in.shop2020.model.v1.user.TrackLogType;
-
 
30
import in.shop2020.model.v1.user.User;
-
 
31
import in.shop2020.model.v1.user.UserAffiliateException;
-
 
32
import in.shop2020.model.v1.user.UserContextException;
-
 
33
import in.shop2020.model.v1.user.WidgetException;
-
 
34
import in.shop2020.serving.interceptors.TrackingInterceptor;
-
 
35
import in.shop2020.serving.interceptors.UserInterceptor;
-
 
36
import in.shop2020.serving.utils.DesEncrypter;
-
 
37
import in.shop2020.serving.utils.FacebookUtility;
-
 
38
import in.shop2020.serving.utils.UserMessage;
-
 
39
import in.shop2020.thrift.clients.UserClient;
-
 
40
 
42
/**
41
/**
43
 * 
42
 * 
44
 * @author rajveer
43
 * @author rajveer
45
 * 
44
 * 
46
 */
45
 */
Line 60... Line 59...
60
	private DesEncrypter desEncrypter = new DesEncrypter("saholic");
59
	private DesEncrypter desEncrypter = new DesEncrypter("saholic");
61
	private String loginResult = "0";
60
	private String loginResult = "0";
62
	private String redirectUrl = "/";
61
	private String redirectUrl = "/";
63
	private String signed_request;
62
	private String signed_request;
64
	private Long userId = -1L;
63
	private Long userId = -1L;
-
 
64
	private String loginJson = "{}";
65
	
65
	
66
	public LoginController() {
66
	public LoginController() {
67
		super();
67
		super();
68
	}
68
	}
69
	@Actions({
69
	@Actions({
Line 90... Line 90...
90
//            DataLogger.logData(EventType.LOGIN_FAILED, getSessionId(), userinfo.getUserId(), this.request.getParameter("email"));
90
//            DataLogger.logData(EventType.LOGIN_FAILED, getSessionId(), userinfo.getUserId(), this.request.getParameter("email"));
91
			return "login";
91
			return "login";
92
		}
92
		}
93
	}
93
	}
94
 
94
 
-
 
95
	public String verifyFbUser(){
-
 
96
		JSONObject json = new JSONObject();
-
 
97
		try{
-
 
98
			String email  =  this.request.getParameter("email");
-
 
99
			String accessToken = this.request.getParameter("accessToken");
-
 
100
			UserClient userContextServiceClient = new UserClient();
-
 
101
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
-
 
102
			User user = userClient.getUserByEmail(email);
-
 
103
			if(user==null||user.getUserId()<1){
-
 
104
				//if user exists
-
 
105
				//verify its accessToken
-
 
106
			JSONObject result=	new FacebookUtility().verifyFbToken(accessToken,email,user.isIsFacebookUser(),user.getFacebookId());
-
 
107
			
-
 
108
			if("true".equalsIgnoreCase(result.getString("status"))){
-
 
109
				
-
 
110
				String storedFbId = user.getFacebookId();
-
 
111
				if((storedFbId==null|| (storedFbId!=null && storedFbId.isEmpty()))){
-
 
112
					user.setFacebookId(result.getString("id"));
-
 
113
					user.setIsFacebookUser(true);
-
 
114
				}
-
 
115
				json.put("status", "success");
-
 
116
				setFbLoginCookie(userClient, user);
-
 
117
			}else{
-
 
118
				json.put("status", "fail");
-
 
119
				json.put("message", "No such user exist");
-
 
120
			}
-
 
121
			}else{
-
 
122
				//no such user exists
-
 
123
				//ask him to signup
-
 
124
				json.put("status", "fail");
-
 
125
				json.put("message", "No such user exist");
-
 
126
			}
-
 
127
			
-
 
128
			setLoginJson(json.toString());
-
 
129
		}catch(Exception e){
-
 
130
			log.error(UserMessage.USER_AUTHENTICATION_FAILURE, e);
-
 
131
		}
-
 
132
		return "login-response";
-
 
133
	}
-
 
134
	
-
 
135
	private void setFbLoginCookie(in.shop2020.model.v1.user.UserContextService.Client userClient, User user){
-
 
136
 
-
 
137
		try{
-
 
138
		Cookie uidCookie = (Cookie) cookiesMap.get(UserInterceptor.USER_ID_COOKIE_NAME);
-
 
139
		Cookie uicCookie = (Cookie) cookiesMap.get(UserInterceptor.USER_INFO_COOKIE_NAME);
-
 
140
		HttpServletResponse response = ServletActionContext.getResponse();
-
 
141
		if (uidCookie == null) {
-
 
142
			DesEncrypter cookieEncryter = new DesEncrypter(UserInterceptor.COOKIE_DECRYPTION_STRING);
-
 
143
			uidCookie = new Cookie(UserInterceptor.USER_ID_COOKIE_NAME, cookieEncryter.encrypt(userId + ""));
-
 
144
			uidCookie.setPath("/");
-
 
145
			if(!domainName.isEmpty()) {
-
 
146
				uidCookie.setDomain(domainName);
-
 
147
			}
-
 
148
		}
-
 
149
		if (uicCookie != null) {
-
 
150
			uicCookie.setMaxAge(UserInterceptor.FACEBOOK_USER_COOKIE_EXPIRY_TIME);
-
 
151
			cookiesMap.put(UserInterceptor.USER_INFO_COOKIE_NAME, uicCookie);
-
 
152
	        response.addCookie(uicCookie);
-
 
153
		}
-
 
154
		uidCookie.setMaxAge(UserInterceptor.FACEBOOK_USER_COOKIE_EXPIRY_TIME);
-
 
155
		cookiesMap.put(UserInterceptor.USER_ID_COOKIE_NAME, uidCookie);
-
 
156
        response.addCookie(uidCookie);
-
 
157
        
-
 
158
        userClient.setUserAsLoggedIn(user.getUserId(),(new Date()).getTime());
-
 
159
		String pincode = userClient.getDefaultPincode(user.getUserId());
-
 
160
		
-
 
161
		// TODO: setTotalItems shouldn't be a method on userinfo. This allows
-
 
162
		// for potentially updating the item count wrongly. The method setCartId
-
 
163
		// should update the item count as well. Also, there can be a method
-
 
164
		// called refreshItemCount() that automatically updates the number of
-
 
165
		// items currently in the cart.
-
 
166
		if(userinfo.getUserId() != -1){
-
 
167
			userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
-
 
168
			
-
 
169
			List<Long> items = userClient.getBrowseHistoryItems(userinfo.getUserId());
-
 
170
			if(items != null){
-
 
171
				for(Long itemId: items){
-
 
172
					userClient.updateBrowseHistory(user.getUserId(), itemId);
-
 
173
				}
-
 
174
			}
-
 
175
			
-
 
176
			items = userClient.getMyResearchItems(userinfo.getUserId());
-
 
177
			if(items != null){
-
 
178
				for(Long itemId: items){
-
 
179
					userClient.updateMyResearch(user.getUserId(), itemId);
-
 
180
				}
-
 
181
			}
-
 
182
		}
-
 
183
		
-
 
184
		userinfo.setUserId(user.getUserId());
-
 
185
		userinfo.setEmail(user.getEmail());
-
 
186
		userinfo.setLoggedIn(true);
-
 
187
		userinfo.setPincode(pincode);
-
 
188
		userinfo.setCartId(user.getActiveCartId());
-
 
189
		Cart cart = userClient.getCart(user.getActiveCartId());
-
 
190
		userinfo.setTotalItems(cart.getLinesSize());
-
 
191
		userinfo.setTotalAmount(cart.getTotalPrice());
-
 
192
		userinfo.setPrivateDealUser(isPrivateDealUser());
-
 
193
		String src = user.getSource();
-
 
194
		if (src == null) {
-
 
195
		    src = "";
-
 
196
		}
-
 
197
	
-
 
198
		}catch(Exception e){
-
 
199
			e.printStackTrace();
-
 
200
		}
-
 
201
	}
-
 
202
	
-
 
203
	
-
 
204
	
95
	private boolean loginUser() {
205
	private boolean loginUser() {
96
		try {
206
		try {
97
			log.info("in loginuser");
207
			log.info("in loginuser");
98
			String email, password = null, isFacebookUser;
208
			String email, password = null, isFacebookUser;
99
 
209
 
Line 395... Line 505...
395
	}
505
	}
396
	public void setSigned_request(String signed_request) {
506
	public void setSigned_request(String signed_request) {
397
		this.signed_request = signed_request;
507
		this.signed_request = signed_request;
398
	}
508
	}
399
	
509
	
-
 
510
	
-
 
511
	
-
 
512
	public String getLoginJson() {
-
 
513
		return loginJson;
-
 
514
	}
-
 
515
	public void setLoginJson(String loginJson) {
-
 
516
		this.loginJson = loginJson;
-
 
517
	}
400
	public static void main(String[] args) {
518
	public static void main(String[] args) {
401
        DesEncrypter des = new DesEncrypter("saholic");
519
        DesEncrypter des = new DesEncrypter("saholic");
402
        System.out.println(des.decrypt("V9gWakvuejQEJqCJjYhZtA"));
520
        System.out.println(des.decrypt("V9gWakvuejQEJqCJjYhZtA"));
403
    }
521
    }
404
	
522