1 : |
dgarcia |
1 |
package eu.smartlm.abs.portal.security.login.controller;
|
2 : |
|
|
|
3 : |
|
|
import javax.portlet.ActionRequest;
|
4 : |
|
|
import javax.portlet.ActionResponse;
|
5 : |
|
|
import javax.portlet.PortletSession;
|
6 : |
|
|
|
7 : |
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
8 : |
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
9 : |
|
|
import org.springframework.stereotype.Controller;
|
10 : |
|
|
import org.springframework.validation.BindingResult;
|
11 : |
|
|
import org.springframework.web.bind.WebDataBinder;
|
12 : |
|
|
import org.springframework.web.bind.annotation.InitBinder;
|
13 : |
|
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
14 : |
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
15 : |
|
|
import org.springframework.web.bind.annotation.SessionAttributes;
|
16 : |
|
|
import org.springframework.web.bind.support.SessionStatus;
|
17 : |
|
|
import org.springframework.web.portlet.bind.annotation.ActionMapping;
|
18 : |
|
|
import org.springframework.web.portlet.bind.annotation.RenderMapping;
|
19 : |
|
|
|
20 : |
|
|
import eu.smartlm.abs.portal.security.login.model.User;
|
21 : |
|
|
import eu.smartlm.abs.portal.security.login.service.LoginService;
|
22 : |
|
|
import eu.smartlm.abs.portal.view.event.LoginEventData;
|
23 : |
|
|
|
24 : |
|
|
|
25 : |
|
|
/**
|
26 : |
|
|
* Login control that limits the query to the Accounting and Billing Database
|
27 : |
|
|
* @author David García Pérez - CESGA
|
28 : |
|
|
*/
|
29 : |
|
|
@Controller(value="loginController")
|
30 : |
|
|
@RequestMapping(value = "VIEW")
|
31 : |
|
|
@SessionAttributes(types = User.class)
|
32 : |
|
|
public class LoginController {
|
33 : |
|
|
public static int LOGGED_IN = 100;
|
34 : |
|
|
public static int LOGGED_OUT = 200;
|
35 : |
|
|
public static String LOGIN_EVENT = "loginEvent";
|
36 : |
|
|
public static String LOGIN_EVENT_USER ="loginEventUser";
|
37 : |
|
|
private int status = 200;
|
38 : |
|
|
private User user;
|
39 : |
|
|
|
40 : |
|
|
@Autowired
|
41 : |
|
|
@Qualifier("myLoginService")
|
42 : |
|
|
private LoginService loginService;
|
43 : |
|
|
|
44 : |
|
|
public void setLoginService(LoginService loginService) {
|
45 : |
|
|
this.loginService = loginService;
|
46 : |
|
|
}
|
47 : |
|
|
|
48 : |
|
|
/**
|
49 : |
|
|
* Pointer to the default jsp portlet to show
|
50 : |
|
|
* @return the name of the jsp portlet
|
51 : |
|
|
*/
|
52 : |
|
|
@RenderMapping
|
53 : |
|
|
public String showLoginForm(ActionRequest request) {
|
54 : |
|
|
System.out.println("SESSION: " + request.getPortletSession().getId());
|
55 : |
|
|
if(status == LOGGED_OUT)
|
56 : |
|
|
return "login";
|
57 : |
|
|
else if (status == LOGGED_IN)
|
58 : |
|
|
return "logged";
|
59 : |
|
|
else
|
60 : |
|
|
return "login";
|
61 : |
|
|
}
|
62 : |
|
|
|
63 : |
|
|
/**
|
64 : |
|
|
* Pointer to the jsp page with the logged information
|
65 : |
|
|
* @return the name of the jsp page
|
66 : |
|
|
*/
|
67 : |
|
|
@RenderMapping(params = "myaction=loggedIn")
|
68 : |
|
|
public String showLoggedInPage() {
|
69 : |
|
|
return "logged";
|
70 : |
|
|
}
|
71 : |
|
|
|
72 : |
|
|
/**
|
73 : |
|
|
* User object that it is created to be passed to the jsp page
|
74 : |
|
|
* @return a User Object
|
75 : |
|
|
*/
|
76 : |
|
|
@ModelAttribute("user")
|
77 : |
|
|
public User getCommandObject() {
|
78 : |
|
|
if (user == null) return new User();
|
79 : |
|
|
return user;
|
80 : |
|
|
}
|
81 : |
|
|
|
82 : |
|
|
/**
|
83 : |
|
|
* Initicialices the binder that associates fields of the jsp page to the user model
|
84 : |
|
|
* @param binder
|
85 : |
|
|
*/
|
86 : |
|
|
@InitBinder("user")
|
87 : |
|
|
public void initBinder(WebDataBinder binder) { }
|
88 : |
|
|
|
89 : |
|
|
|
90 : |
|
|
/**
|
91 : |
|
|
* Action to be processed when a user tries to login
|
92 : |
|
|
*/
|
93 : |
|
|
@ActionMapping(params = "myaction=loginUser")
|
94 : |
|
|
public void loginUser(@ModelAttribute User user, BindingResult bindingResult, ActionRequest request, ActionResponse response, SessionStatus sessionStatus) {
|
95 : |
|
|
|
96 : |
|
|
if(loginService.isValidLogin(user)) {
|
97 : |
|
|
status = LOGGED_IN;
|
98 : |
|
|
this.user = loginService.getUser(user.getUsername());
|
99 : |
|
|
response.setEvent(LOGIN_EVENT, new LoginEventData(status, this.user));
|
100 : |
|
|
response.setRenderParameter("myaction", "loggedIn");
|
101 : |
|
|
request.getPortletSession().setAttribute(LOGIN_EVENT, LOGGED_IN, PortletSession.APPLICATION_SCOPE);
|
102 : |
|
|
request.getPortletSession().setAttribute(LOGIN_EVENT_USER, this.user, PortletSession.APPLICATION_SCOPE);
|
103 : |
|
|
} else {
|
104 : |
|
|
status = LOGGED_OUT;
|
105 : |
|
|
request.getPortletSession().setAttribute(LOGIN_EVENT, LOGGED_OUT, PortletSession.APPLICATION_SCOPE);
|
106 : |
|
|
}
|
107 : |
|
|
}
|
108 : |
|
|
|
109 : |
|
|
/**
|
110 : |
|
|
* Action to be processed when a user logs out
|
111 : |
|
|
*/
|
112 : |
|
|
@ActionMapping(params = "myaction=logoutUser")
|
113 : |
|
|
public void loggoutUser(ActionRequest request, ActionResponse response, SessionStatus sessionStatus) {
|
114 : |
|
|
status = LOGGED_OUT;
|
115 : |
|
|
user = null;
|
116 : |
|
|
response.setEvent(LOGIN_EVENT, new LoginEventData(status, user));
|
117 : |
|
|
response.setRenderParameter("myaction" , "login");
|
118 : |
|
|
request.getPortletSession().setAttribute(LOGIN_EVENT, LOGGED_OUT, PortletSession.APPLICATION_SCOPE);
|
119 : |
|
|
request.getPortletSession().setAttribute(LOGIN_EVENT_USER, null, PortletSession.APPLICATION_SCOPE);
|
120 : |
|
|
sessionStatus.setComplete();
|
121 : |
|
|
}
|
122 : |
|
|
}
|