| 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;
|
| 7007 |
amar.kumar |
8 |
import in.shop2020.model.v1.user.Sex;
|
|
|
9 |
import in.shop2020.model.v1.user.ShoppingCartException;
|
|
|
10 |
import in.shop2020.model.v1.user.TrackLogType;
|
| 637 |
rajveer |
11 |
import in.shop2020.model.v1.user.User;
|
| 7007 |
amar.kumar |
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;
|
| 815 |
rajveer |
17 |
import in.shop2020.serving.utils.DesEncrypter;
|
| 1175 |
varun.gupt |
18 |
import in.shop2020.serving.utils.UserMessage;
|
| 7007 |
amar.kumar |
19 |
import in.shop2020.serving.utils.Utils;
|
| 3126 |
rajveer |
20 |
import in.shop2020.thrift.clients.UserClient;
|
| 2511 |
vikas |
21 |
import in.shop2020.utils.DataLogger;
|
| 637 |
rajveer |
22 |
|
|
|
23 |
import java.io.IOException;
|
|
|
24 |
import java.util.Date;
|
| 1623 |
rajveer |
25 |
import java.util.List;
|
| 637 |
rajveer |
26 |
|
| 7007 |
amar.kumar |
27 |
import javax.servlet.http.Cookie;
|
|
|
28 |
import javax.servlet.http.HttpServletResponse;
|
|
|
29 |
|
|
|
30 |
import org.apache.commons.lang.RandomStringUtils;
|
| 832 |
rajveer |
31 |
import org.apache.log4j.Logger;
|
| 7007 |
amar.kumar |
32 |
import org.apache.struts2.ServletActionContext;
|
| 4222 |
varun.gupt |
33 |
import org.apache.struts2.convention.annotation.Action;
|
|
|
34 |
import org.apache.struts2.convention.annotation.Actions;
|
|
|
35 |
import org.apache.struts2.convention.annotation.InterceptorRef;
|
| 637 |
rajveer |
36 |
import org.apache.struts2.convention.annotation.Result;
|
| 925 |
rajveer |
37 |
import org.apache.struts2.convention.annotation.Results;
|
| 7007 |
amar.kumar |
38 |
import org.apache.thrift.TException;
|
|
|
39 |
import org.apache.thrift.transport.TTransportException;
|
| 637 |
rajveer |
40 |
|
|
|
41 |
/**
|
|
|
42 |
*
|
|
|
43 |
* @author rajveer
|
| 781 |
vikas |
44 |
*
|
| 637 |
rajveer |
45 |
*/
|
| 925 |
rajveer |
46 |
@Results({
|
|
|
47 |
@Result(name="success", type="redirectAction", params = {"actionName" , "home"}),
|
|
|
48 |
@Result(name = "redirect", location = "${redirectUrl}", type = "redirect")
|
|
|
49 |
})
|
| 637 |
rajveer |
50 |
|
| 781 |
vikas |
51 |
public class LoginController extends BaseController {
|
| 650 |
rajveer |
52 |
|
| 781 |
vikas |
53 |
/**
|
|
|
54 |
*
|
|
|
55 |
*/
|
|
|
56 |
private static final long serialVersionUID = 5390035354379263121L;
|
| 650 |
rajveer |
57 |
|
| 832 |
rajveer |
58 |
private static Logger log = Logger.getLogger(Class.class);
|
| 815 |
rajveer |
59 |
private DesEncrypter desEncrypter = new DesEncrypter("saholic");
|
| 5510 |
rajveer |
60 |
private String loginResult = "0";
|
| 2933 |
vikas |
61 |
private String redirectUrl = "/";
|
| 7007 |
amar.kumar |
62 |
private Long userId = -1L;
|
|
|
63 |
|
| 781 |
vikas |
64 |
public LoginController() {
|
| 637 |
rajveer |
65 |
super();
|
|
|
66 |
}
|
| 4222 |
varun.gupt |
67 |
@Actions({
|
|
|
68 |
@Action(value="login", interceptorRefs={@InterceptorRef("myDefault")}),
|
|
|
69 |
@Action(value="login-mini", interceptorRefs={@InterceptorRef("myDefault")})
|
|
|
70 |
})
|
| 781 |
vikas |
71 |
public String index() throws SecurityException, IOException {
|
| 925 |
rajveer |
72 |
if(userinfo.isLoggedIn()){
|
|
|
73 |
return "success";
|
|
|
74 |
}
|
| 650 |
rajveer |
75 |
return "index";
|
| 781 |
vikas |
76 |
}
|
| 637 |
rajveer |
77 |
|
| 781 |
vikas |
78 |
public String create() throws SecurityException, Exception {
|
|
|
79 |
if (loginUser()) {
|
| 2959 |
chandransh |
80 |
log.info("Will redirect the user to:" + redirectUrl);
|
| 2637 |
vikas |
81 |
return "redirect";
|
| 781 |
vikas |
82 |
} else {
|
| 1175 |
varun.gupt |
83 |
addActionError(UserMessage.USER_AUTHENTICATION_FAILURE);
|
| 3185 |
vikas |
84 |
DataLogger.logData(EventType.LOGIN_FAILED, getSessionId(), userinfo.getUserId(), this.request.getParameter("email"));
|
| 830 |
vikas |
85 |
return "login";
|
| 781 |
vikas |
86 |
}
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
private boolean loginUser() {
|
|
|
90 |
try {
|
| 7007 |
amar.kumar |
91 |
String email, password, isFacebookUser;
|
| 781 |
vikas |
92 |
|
|
|
93 |
email = this.request.getParameter("email");
|
|
|
94 |
password = this.request.getParameter("password");
|
| 7007 |
amar.kumar |
95 |
isFacebookUser = this.request.getParameter("isFacebookUser");
|
|
|
96 |
|
|
|
97 |
if (email == null || (password == null && (isFacebookUser==null || !isFacebookUser.equals("True")))) {
|
| 781 |
vikas |
98 |
return false;
|
|
|
99 |
}
|
| 815 |
rajveer |
100 |
|
| 7007 |
amar.kumar |
101 |
String encryptedPassword = null;
|
|
|
102 |
if(!(password==null)){
|
|
|
103 |
encryptedPassword = desEncrypter.encrypt(password);
|
|
|
104 |
}
|
|
|
105 |
|
| 3126 |
rajveer |
106 |
UserClient userContextServiceClient = new UserClient();
|
| 1747 |
varun.gupt |
107 |
in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
|
| 7007 |
amar.kumar |
108 |
|
|
|
109 |
User user = null;
|
|
|
110 |
|
|
|
111 |
boolean shouldChangeCookieExpiry = false;
|
|
|
112 |
boolean isNewFacebookUser = false;
|
| 7023 |
amar.kumar |
113 |
if (isFacebookUser!=null && isFacebookUser.equals("True") && !email.equals("undefined")) {
|
| 7007 |
amar.kumar |
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);
|
|
|
162 |
}
|
| 793 |
rajveer |
163 |
userClient.setUserAsLoggedIn(user.getUserId(),(new Date()).getTime());
|
|
|
164 |
String pincode = userClient.getDefaultPincode(user.getUserId());
|
| 2637 |
vikas |
165 |
|
| 1175 |
varun.gupt |
166 |
// TODO: setTotalItems shouldn't be a method on userinfo. This allows
|
|
|
167 |
// for potentially updating the item count wrongly. The method setCartId
|
| 637 |
rajveer |
168 |
// should update the item count as well. Also, there can be a method
|
| 1175 |
varun.gupt |
169 |
// called refreshItemCount() that automatically updates the number of
|
| 637 |
rajveer |
170 |
// items currently in the cart.
|
| 1625 |
rajveer |
171 |
if(userinfo.getUserId() != -1){
|
| 1623 |
rajveer |
172 |
userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
|
| 1625 |
rajveer |
173 |
|
| 2982 |
rajveer |
174 |
List<Long> items = userClient.getBrowseHistoryItems(userinfo.getUserId());
|
|
|
175 |
if(items != null){
|
|
|
176 |
for(Long itemId: items){
|
|
|
177 |
userClient.updateBrowseHistory(user.getUserId(), itemId);
|
| 1623 |
rajveer |
178 |
}
|
|
|
179 |
}
|
| 4453 |
varun.gupt |
180 |
|
| 2982 |
rajveer |
181 |
items = userClient.getMyResearchItems(userinfo.getUserId());
|
|
|
182 |
if(items != null){
|
|
|
183 |
for(Long itemId: items){
|
|
|
184 |
userClient.updateMyResearch(user.getUserId(), itemId);
|
| 1625 |
rajveer |
185 |
}
|
|
|
186 |
}
|
| 1623 |
rajveer |
187 |
}
|
|
|
188 |
|
| 1625 |
rajveer |
189 |
userinfo.setUserId(user.getUserId());
|
|
|
190 |
userinfo.setEmail(email);
|
|
|
191 |
userinfo.setLoggedIn(true);
|
|
|
192 |
userinfo.setPincode(pincode);
|
| 637 |
rajveer |
193 |
userinfo.setCartId(user.getActiveCartId());
|
| 3830 |
chandransh |
194 |
Cart cart = userClient.getCart(user.getActiveCartId());
|
|
|
195 |
userinfo.setTotalItems(cart.getLinesSize());
|
|
|
196 |
userinfo.setTotalAmount(cart.getTotalPrice());
|
| 2959 |
chandransh |
197 |
log.info(userinfo);
|
| 2996 |
vikas |
198 |
String src = user.getSource();
|
|
|
199 |
if (src == null) {
|
|
|
200 |
src = "";
|
|
|
201 |
}
|
| 3185 |
vikas |
202 |
DataLogger.logData(EventType.LOGIN_SUCCESS, getSessionId(), userinfo.getUserId(),
|
| 2996 |
vikas |
203 |
email, src);
|
| 2637 |
vikas |
204 |
return true;
|
| 781 |
vikas |
205 |
} catch (Exception e) {
|
| 2959 |
chandransh |
206 |
log.error(UserMessage.USER_AUTHENTICATION_FAILURE, e);
|
| 781 |
vikas |
207 |
return false;
|
|
|
208 |
}
|
|
|
209 |
}
|
| 637 |
rajveer |
210 |
|
| 7007 |
amar.kumar |
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 |
|
| 5510 |
rajveer |
336 |
public String authenticateUser() {
|
|
|
337 |
String email, password;
|
|
|
338 |
|
|
|
339 |
email = this.request.getParameter("email");
|
|
|
340 |
password = this.request.getParameter("password");
|
|
|
341 |
|
|
|
342 |
if (email == null || password == null) {
|
|
|
343 |
loginResult = "0";
|
|
|
344 |
return "result";
|
|
|
345 |
}
|
|
|
346 |
|
|
|
347 |
String encryptedPassword = desEncrypter.encrypt(password);
|
|
|
348 |
try{
|
|
|
349 |
UserClient userContextServiceClient = new UserClient();
|
|
|
350 |
in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
|
|
|
351 |
userClient.authenticateUser(email, encryptedPassword);
|
|
|
352 |
}catch (Exception e) {
|
|
|
353 |
loginResult = "0";
|
|
|
354 |
return "result";
|
|
|
355 |
}
|
|
|
356 |
loginResult = "1";
|
|
|
357 |
return "result";
|
|
|
358 |
}
|
|
|
359 |
|
|
|
360 |
public String getLoginResult() {
|
|
|
361 |
return loginResult;
|
|
|
362 |
}
|
|
|
363 |
|
| 924 |
vikas |
364 |
public String getRedirectUrl() {
|
|
|
365 |
return redirectUrl;
|
| 781 |
vikas |
366 |
}
|
|
|
367 |
|
| 924 |
vikas |
368 |
public void setRedirectUrl(String redirectUrl) {
|
|
|
369 |
this.redirectUrl = redirectUrl;
|
|
|
370 |
}
|
| 6903 |
anupam.sin |
371 |
|
|
|
372 |
public static void main(String[] args) {
|
|
|
373 |
DesEncrypter des = new DesEncrypter("saholic");
|
|
|
374 |
System.out.println(des.decrypt("XvrWIvmYrUAdXqxhovZMSw"));
|
|
|
375 |
}
|
| 637 |
rajveer |
376 |
}
|