Subversion Repositories SmartDukaan

Rev

Rev 762 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 762 Rev 781
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
 
-
 
7
import in.shop2020.model.v1.user.User;
6
import in.shop2020.model.v1.user.User;
8
import in.shop2020.serving.utils.Utils;
-
 
9
import in.shop2020.thrift.clients.UserContextServiceClient;
7
import in.shop2020.thrift.clients.UserContextServiceClient;
10
 
8
 
11
import java.io.IOException;
9
import java.io.IOException;
12
import java.util.Date;
10
import java.util.Date;
13
 
11
 
Line 19... Line 17...
19
import org.apache.struts2.convention.annotation.Results;
17
import org.apache.struts2.convention.annotation.Results;
20
 
18
 
21
/**
19
/**
22
 * 
20
 * 
23
 * @author rajveer
21
 * @author rajveer
24
 *
22
 * 
25
 */
23
 */
26
 
24
 
27
@Results({
25
@Results({
28
    @Result(name="success", type="redirectAction", 
26
		@Result(name = "failure", type = "redirectAction", params = {
29
    		params = {"actionName" , "home"}),
27
				"actionName", "login" }),
30
    @Result(name="failure", type="redirectAction", 
28
		@Result(name = "redirect", location = "${url}", type = "redirect") })
31
       		params = {"actionName" , "login"}),
29
public class LoginController extends BaseController {
32
    @Result(name="redirect", location="${url}", type="redirect")
-
 
33
})
-
 
34
 
30
 
35
public class LoginController extends BaseController{
-
 
-
 
31
	/**
36
		  
32
	 * 
-
 
33
	 */
-
 
34
	private static final long serialVersionUID = 5390035354379263121L;
37
 
35
 
38
	private static Log log = LogFactory.getLog(LoginController.class);
36
	private static Log log = LogFactory.getLog(LoginController.class);
39
	
37
 
40
	private String redirectUrl;
38
	private String redirectUrl;
41
	
39
 
42
	public LoginController(){
40
	public LoginController() {
43
		super();
41
		super();
44
	}
42
	}
45
 
43
 
46
 
-
 
47
    public String index() throws SecurityException, IOException {
44
	public String index() throws SecurityException, IOException {
48
    	htmlSnippets.put("LOGIN_HEADER", pageLoader.getLoginHeaderHtml());
45
		htmlSnippets.put("LOGIN_HEADER", pageLoader.getLoginHeaderHtml());
49
		return "index";
46
		return "index";
50
    }
47
	}
51
 
48
 
52
    public String create() throws SecurityException, Exception {
49
	public String create() throws SecurityException, Exception {
53
    	
-
 
54
    	if(loginUser()){
50
		if (loginUser()) {
-
 
51
			redirectUrl = (String) this.session.getAttribute("REDIRECT_URL");
55
    		if(getUrl()!=null){
52
			if (redirectUrl == null) {
56
    			redirectUrl = getUrl();
53
				redirectUrl = "";
-
 
54
			}
57
    			resetRedirectUrl();
55
			log.debug(redirectUrl);
58
    			return "redirect";
56
			resetRedirectUrl();
59
    		}
-
 
60
    		return "success";
57
			return "redirect";
61
    	}
-
 
62
    	else{
58
		} else {
63
    		addActionError("Either email or password is wrong.");
59
			addActionError("Either email or password is wrong.");
64
    		return "failure";
60
			return "failure";
65
    	}
61
		}
66
    }
62
	}
67
    
63
 
68
    private boolean loginUser(){
64
	private boolean loginUser() {
69
    	try{
65
		try {
70
	    	String email, password;
66
			String email, password;
71
	    	
67
 
72
	    	email = this.request.getParameter("email"); 
68
			email = this.request.getParameter("email");
73
	    	password = this.request.getParameter("password");
69
			password = this.request.getParameter("password");
74
	    	
70
 
75
	    	if(email == null || password == null){
71
			if (email == null || password == null) {
76
	    		return false;
72
				return false;
77
	    	}
73
			}
78
	    	
74
			log.debug(email);
-
 
75
			log.debug(password);
79
	    	UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
76
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
80
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
77
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient
-
 
78
					.getClient();
81
			User user = userClient.authenticateUser(email, password);
79
			User user = userClient.authenticateUser(email, password);
82
			userClient.setUserAsLoggedIn(user.getUserId(), (new Date()).getTime());
80
			userClient.setUserAsLoggedIn(user.getUserId(),
-
 
81
					(new Date()).getTime());
83
	
82
 
84
			userinfo.setUserId(user.getUserId());
83
			userinfo.setUserId(user.getUserId());
85
			userinfo.setNameOfUser(user.getName());
84
			userinfo.setNameOfUser(user.getName());
86
			userinfo.setEmail(email);
85
			userinfo.setEmail(email);
87
			userinfo.setLoggedIn(true);
86
			userinfo.setLoggedIn(true);
88
			
87
 
89
			// TODO: setTotalItems shouldn't be a method on userinfo. This allows
88
			// TODO: setTotalItems shouldn't be a method on userinfo. This
-
 
89
			// allows
90
			// for potentially updating the item count wrongly. The method setCartId
90
			// for potentially updating the item count wrongly. The method
-
 
91
			// setCartId
91
			// should update the item count as well. Also, there can be a method
92
			// should update the item count as well. Also, there can be a method
92
			// called refreshItemCount() that automatically updates the number of
93
			// called refreshItemCount() that automatically updates the number
-
 
94
			// of
93
			// items currently in the cart.
95
			// items currently in the cart.
94
			userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
96
			userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
95
			userinfo.setCartId(user.getActiveCartId());
97
			userinfo.setCartId(user.getActiveCartId());
96
			int totalItems = userClient.getCart(user.getActiveCartId()).getLinesSize();
98
			int totalItems = userClient.getCart(user.getActiveCartId())
-
 
99
					.getLinesSize();
97
			userinfo.setTotalItems(totalItems);
100
			userinfo.setTotalItems(totalItems);
98
 
101
 
99
			return true;
102
			return true;
100
    	}catch(Exception e){
103
		} catch (Exception e) {
101
    		log.error("Wrong username or password.");
104
			log.error("Wrong username or password.");
102
    		return false;
105
			return false;
103
    	}
106
		}
104
    }
107
	}
105
 
108
 
106
    String getUrl(){
109
	String getUrl() {
107
    	return this.redirectUrl;
110
		return this.redirectUrl;
108
    }
111
	}
109
    
112
 
110
	public String getLoginHeaderSnippet(){
113
	public String getLoginHeaderSnippet() {
111
		return htmlSnippets.get("LOGIN_HEADER");
114
		return htmlSnippets.get("LOGIN_HEADER");
112
	}
115
	}
113
 
116
 
114
}
117
}