com.ggfix.gg.user.controller.UserAuthorityController.java Source code

Java tutorial

Introduction

Here is the source code for com.ggfix.gg.user.controller.UserAuthorityController.java

Source

package com.ggfix.gg.user.controller;
/*
 * Copyright (c) 2010 Hewlett-Packard Company, Inc. All Rights Reserved.
 * This software is the confidential and proprietary information of 
 * Hewlett-Packard, Inc. ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Hewlett-Packard.
 * HP MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. HP SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

import com.ggfix.gg.user.model.User;
import com.ggfix.gg.user.service.UserManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * ??.
 * Revision: 7/17/14
 * Author: <a href="mailto:si-ke.lv@hp.com">Lv,Si-Ke</a>
 */
@Controller
public class UserAuthorityController {
    @Resource
    private UserManager userManager;

    @RequestMapping("/login")
    public String login(String account, String password, HttpSession session) {
        if (this.userManager.login(account, password)) {
            List<User> users = this.userManager.getUsersByEmail(account);
            if (users != null) {
                session.setAttribute("User", users.get(0));
                session.setAttribute("UserId", users.get(0).getId());
                session.setAttribute("UserName", users.get(0).getFirstName());
                session.removeAttribute("errorInfo");
            }
        } else {
            if (session.getAttribute("UserName") == null)
                session.setAttribute("UserName", session.getId());
            session.setAttribute("errorInfo", "wrongUserOrPassword");
        }
        return "index";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        User user = (User) session.getAttribute("User");
        //
        this.userManager.logout(user);
        //logout
        session.removeAttribute("User");
        session.removeAttribute("UserId");
        session.removeAttribute("UserName");
        session.removeAttribute("errorInfo");
        return "index";
    }

    @RequestMapping("/index")
    public String goToIndex() {
        return "index";
    }
}