/* 
 * Copyright (C) 2013 Open Source Consulting, Inc. All rights reserved by Open Source Consulting, Inc.
 *
 * 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; either version 2
 * of the License, or (at your option) any later version.
 * 
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * Revision History
 * Author			Date				Description
 * ---------------	----------------	------------
 * Bong-Jin Kwon	2013. 9. 25.		First Draft.
 */
package com.athena.meerkat.controller.web.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.WebAttributes;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.athena.meerkat.controller.web.common.model.SimpleJsonResponse;

/**
 * <pre>
 * 사용자 인증 컨트롤러.
 * </pre>
 * 
 * @author Bong-Jin Kwon
 * @version 1.0
 */
@Controller
@RequestMapping("/auth")
public class AuthController {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(AuthController.class);


	/**
	 * <pre>
	 * 
	 * </pre>
	 */
	public AuthController() {
		
	}

	@RequestMapping("/onAfterLogout")
	@ResponseBody
	public SimpleJsonResponse logout(SimpleJsonResponse jsonRes,
			HttpSession session) {

		session.invalidate();

		jsonRes.setMsg("로그아웃 되었습니다.");

		return jsonRes;
	}

	@RequestMapping("/onAfterLogin")
	@ResponseBody
	public SimpleJsonResponse onAfterLogin(SimpleJsonResponse jsonRes) {

		UserDetails loginUser = (UserDetails) SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal();
		jsonRes.setData(loginUser);
		//service.updateLastLogin(loginUser.getId());
		return jsonRes;
	}

	@ResponseStatus(value = HttpStatus.FORBIDDEN)
	@RequestMapping("/notLogin")
	@ResponseBody
	public SimpleJsonResponse notLogin(SimpleJsonResponse jsonRes) {

		jsonRes.setSuccess(false);
		jsonRes.setMsg("로그인 정보가 없습니다. 관리자에게 문의하세요.");
		jsonRes.setData("notLogin");

		return jsonRes;
	}

	@ResponseStatus(value = HttpStatus.FORBIDDEN)
	@RequestMapping("/accessDenied")
	@ResponseBody
	public SimpleJsonResponse accessDenied(SimpleJsonResponse jsonRes) {

		jsonRes.setSuccess(false);
		jsonRes.setMsg("해당 작업에 대한 권한이 없습니다. 관리자에게 문의하세요.");

		return jsonRes;
	}

	@RequestMapping("/loginFail")
	@ResponseBody
	public SimpleJsonResponse loginFail(HttpServletRequest request, SimpleJsonResponse jsonRes) {
		
		
		jsonRes.setSuccess(false);
		
		AuthenticationException ex = (AuthenticationException)request.getSession().getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
		
		if (ex instanceof AuthenticationServiceException) {
			jsonRes.setMsg(ex.toString());
		} else {
			jsonRes.setMsg("login ID 또는 password 가 잘못되었습니다.");
		}
		

		return jsonRes;
	}
	

}
// end of UserController.java
@@ -1193,24 +1193,88 @@
 erFactory;%0D%0A
+import org.springframework.beans.factory.annotation.Autowired;%0D%0A
 import org.s
@@ -1970,16 +1970,147 @@
 sponse;%0D
+%0Aimport com.athena.meerkat.controller.web.user.entities.User;%0D%0Aimport com.athena.meerkat.controller.web.user.services.UserService;%0D
 %0A%0D%0A/**%0D%0A
@@ -2270,17 +2270,16 @@
 ller %7B%0D%0A
-%09
 %0D%0A%09priva
@@ -2360,16 +2360,60 @@
 ss);%0D%0A%0D%0A
+%09@Autowired%0D%0A%09private UserService service;%0D%0A
 %0D%0A%09/**%0D%0A
@@ -2475,18 +2475,16 @@
 er() %7B%0D%0A
-%09%09
 %0D%0A%09%7D%0D%0A%0D%0A
@@ -2596,21 +2596,17 @@
 jsonRes,
-%0D%0A%09%09%09
+ 
 HttpSess
@@ -2913,14 +2913,8 @@
 xt()
-%0D%0A%09%09%09%09
 .get
@@ -2980,18 +2980,16 @@
 er);%0D%0A%09%09
-//
 service.
@@ -3004,16 +3004,24 @@
 stLogin(
+((User) 
 loginUse
@@ -3021,16 +3021,17 @@
 oginUser
+)
 .getId()
@@ -3789,22 +3789,16 @@
 Res) %7B%0D%0A
-%09%09%0D%0A%09%09
 %0D%0A%09%09json
@@ -3817,26 +3817,24 @@
 ss(false);%0D%0A
-%09%09
 %0D%0A%09%09Authenti
@@ -3879,16 +3879,17 @@
 ception)
+ 
 request.
@@ -3956,18 +3956,16 @@
 TION);%0D%0A
-%09%09
 %0D%0A%09%09if (
@@ -4123,12 +4123,8 @@
 %09%09%7D%0D
-%0A%09%09%0D
 %0A%0D%0A%09
@@ -4148,11 +4148,8 @@
 %0A%09%7D%0D
-%0A%09%0D
 %0A%0D%0A%7D

update last login user