package cn.feast.coding.controller.system;

import cn.feast.coding.model.system.User;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by IntelliJ IDEA.
 * Creator : peng
 * Date : 2018-03-19
 * Time : 14:25
 */
@Controller
public class LoginController {

    @RequestMapping(value = "/success", method = RequestMethod.GET)
    public String success(ModelMap model) {
        return "index";
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(){
        return "login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(User user, HttpServletRequest req, ModelMap model) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(), user.getUserPass());
        String error = "";
        try
        {
            subject.login(token);
        } catch (UnknownAccountException e)
        {
            error = "用户名/密码错误";
        } catch (IncorrectCredentialsException e)
        {
            error = "用户名/密码错误";
        } catch (LockedAccountException e)
        {
            error = "登录账户被锁";
        } catch (ExcessiveAttemptsException e)
        {
            error = "密码输入错误超过5次，请30分钟后再登录";
        } catch (Exception e)
        {
            error = "请登录";
        }
        if (StringUtils.isNotEmpty(error))
        {
            model.addAttribute("error", error);
            return "login";
        }
        return "redirect:success";
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout(){
        return "login";
    }
}
