package eu.smartlm.abs.portal.security.login; import javax.portlet.ActionResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.SessionAttributes; import org.springframework.web.bind.support.SessionStatus; import org.springframework.web.portlet.bind.annotation.ActionMapping; import org.springframework.web.portlet.bind.annotation.RenderMapping; import eu.smartlm.abs.portal.security.login.model.User; import eu.smartlm.abs.portal.security.login.service.LoginService; import eu.smartlm.abs.portal.view.event.LoginEventData; /** * Login control that limits the query to the Accounting and Billing Database * @author David García Pérez - CESGA */ @Controller(value="loginController") @RequestMapping(value = "VIEW") @SessionAttributes(types = User.class) public class LoginController { public static int LOGGED_IN = 100; public static int LOGGED_OUT = 200; public static String LOGIN_EVENT = "loginEvent"; private int status = 200; private User user; @Autowired @Qualifier("myLoginService") private LoginService loginService; public void setLoginService(LoginService loginService) { this.loginService = loginService; } /** * Pointer to the default jsp portlet to show * @return the name of the jsp portlet */ @RenderMapping public String showLoginForm() { if(status == LOGGED_OUT) return "login"; else if (status == LOGGED_IN) return "logged"; else return "login"; } /** * Pointer to the jsp page with the logged information * @return the name of the jsp page */ @RenderMapping(params = "myaction=loggedIn") public String showLoggedInPage() { return "logged"; } /** * User object that it is created to be passed to the jsp page * @return a User Object */ @ModelAttribute("user") public User getCommandObject() { if (user == null) return new User(); return user; } /** * Initicialices the binder that associates fields of the jsp page to the user model * @param binder */ @InitBinder("user") public void initBinder(WebDataBinder binder) { } /** * Action to be processed when a user tries to login */ @ActionMapping(params = "myaction=loginUser") public void loginUser(@ModelAttribute User user, BindingResult bindingResult, ActionResponse response, SessionStatus sessionStatus) { if(loginService.isValidLogin(user)) { status = LOGGED_IN; this.user = loginService.getUser(user.getUsername()); response.setEvent(LOGIN_EVENT, new LoginEventData(status, this.user)); response.setRenderParameter("myaction", "loggedIn"); } else { status = LOGGED_OUT; } } /** * Action to be processed when a user logs out */ @ActionMapping(params = "myaction=logoutUser") public void loggoutUser(ActionResponse response, SessionStatus sessionStatus) { status = LOGGED_OUT; user = null; response.setEvent(LOGIN_EVENT, new LoginEventData(status, user)); response.setRenderParameter("myaction" , "login"); sessionStatus.setComplete(); } }