de.berlios.jhelpdesk.web.AuthenticationController.java Source code

Java tutorial

Introduction

Here is the source code for de.berlios.jhelpdesk.web.AuthenticationController.java

Source

/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * Copyright: (C) 2006 jHelpdesk Developers Team
 */
package de.berlios.jhelpdesk.web;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import de.berlios.jhelpdesk.dao.UserDAO;
import de.berlios.jhelpdesk.model.User;

/**
 * Kontroler obsugujcy uwierzytelniania uytkownikw w systemie oraz
 * wylogowanie uytkownikw z systemu.
 * 
 * @author jjhop
 */
@Controller
public class AuthenticationController {

    @Autowired
    private UserDAO userDAOJpa;

    /**
     * Przygotowuje formularz logowania.
     *
     * @param map model widoku
     * @return identyfikator widoku formularza
     */
    @RequestMapping(value = "/login.html", method = RequestMethod.GET)
    public String setupLoginForm(ModelMap map) {
        map.addAttribute("user", new User());
        return "login";
    }

    /**
     * Uwierzytelenia uytkownika w systemie. Jeli operacja powiedzie si,
     * uzupenia sesj.
     *
     * @param user uytkownik do uwierzytelnienia
     * @return identyfikator widoku do wywietlenia, bdzie to widok formularza
     *         jeli nie uwierzytelnienie nie powiedzie si i widok domylny
     *         dla uytkownika jeli uda si uwierzytelni
     */
    // TODO: przeniesienie na domylny widok uytkownika
    @RequestMapping(value = "/login.html", method = RequestMethod.POST)
    protected String processLogin(@ModelAttribute("user") User user, HttpSession session) throws Exception {
        // TODO: w DAO metoda authenticate do wywalenia... uwierzytleniamy sprawdzajac
        // czy gosc podal pasujace haslo i login (email) oraz czy moze sie logowac (isActive)
        boolean isAuthenticatedWithJpa = userDAOJpa.authenticate(user.getLogin(), user.getPassword());
        if (isAuthenticatedWithJpa) {
            User loggedUser = userDAOJpa.getByLoginFetchFilters(user.getLogin());
            session.setAttribute("user", loggedUser);
            session.setAttribute("logged", Boolean.TRUE);
            return "redirect:" + loggedUser.getWelcomePage();
        }
        return "login";
    }

    /**
     * Metoda uniewania sesj uytkownika w systemie.
     *
     * @param session sesja uytkownika
     * @return identyfikator widoku do wywietlenia po wylogowaniu
     */
    @RequestMapping(value = "/logout.html")
    public String processLogout(HttpSession session) {
        session.invalidate();
        return "redirect:/login.html";
    }
}