package com.zblog.web.backend.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zblog.biz.CommentManager;
import com.zblog.biz.PostManager;
import com.zblog.core.Constants;
import com.zblog.core.dal.constants.PostConstants;
import com.zblog.core.dal.entity.User;
import com.zblog.core.plugin.MapContainer;
import com.zblog.core.util.CookieUtil;
import com.zblog.core.util.StringUtils;
import com.zblog.service.CommentService;
import com.zblog.service.PostService;
import com.zblog.service.UploadService;
import com.zblog.service.UserService;
import com.zblog.service.shiro.StatelessToken;
import com.zblog.service.vo.OSInfo;
import com.zblog.web.backend.form.LoginForm;
import com.zblog.web.backend.form.validator.LoginFormValidator;
import com.zblog.web.support.CookieRemberManager;
import com.zblog.web.support.WebContextFactory;

@Controller
@RequestMapping("/backend")
public class BackendController{
  @Autowired
  private UserService userService;
  @Autowired
  private PostManager postManager;
  @Autowired
  private CommentManager commentManager;
  @Autowired
  private PostService postService;
  @Autowired
  private CommentService commentService;
  @Autowired
  private UploadService uploadService;

  @RequiresRoles(value = { "admin", "editor" }, logical = Logical.OR)
  @RequestMapping(value = "/index", method = RequestMethod.GET)
  public String index(Model model){
    model.addAttribute("osInfo", OSInfo.getCurrentOSInfo());

    /* 基本站点统计信息 */
    model.addAttribute("userCount", userService.count());
    model.addAttribute("postCount", postService.count());
    model.addAttribute("commentCount", commentService.count());
    model.addAttribute("uploadCount", uploadService.count());

    model.addAttribute("posts", postManager.listRecent(10, PostConstants.POST_CREATOR_ALL));
    model.addAttribute("comments", commentManager.listRecent());
    return "backend/index";
  }

  @RequestMapping(value = "/login", method = RequestMethod.GET)
  public String login(String msg, Model model){
    if(WebContextFactory.get().isLogon())
      return "redirect:/backend/index";

    if("logout".equals(msg)){
      model.addAttribute("msg", "您已登出。");
    }else if("unauthenticated".equals(msg)){
      model.addAttribute("msg", "你没有当前操作权限");
    }
    return "backend/login";
  }

  @RequestMapping(value = "/logout")
  public String logout(HttpServletRequest request, HttpServletResponse response){
    CookieRemberManager.logout(request, response);
    SecurityUtils.getSubject().logout();
    CookieUtil cookieUtil = new CookieUtil(request, response);
    cookieUtil.removeCokie(Constants.COOKIE_CSRF_TOKEN);
    return "redirect:/backend/login?msg=logout";
  }

  @RequestMapping(value = "/login", method = RequestMethod.POST)
  public String dashboard(LoginForm form, HttpServletRequest request, HttpServletResponse response){
    MapContainer result = LoginFormValidator.validateLogin(form);
    if(!result.isEmpty()){
      request.setAttribute("msg", result.get("msg"));
      return "backend/login";
    }

    User user = userService.login(form.getUsername(), form.getPassword());
    if(user == null){
      request.setAttribute("msg", "用户名密码错误");
      return "backend/login";
    }

    SecurityUtils.getSubject().login(new StatelessToken(user.getId(), user.getPassword()));
    CookieUtil cookieUtil = new CookieUtil(request, response);
    cookieUtil.setCookie(Constants.COOKIE_USER_NAME, form.getUsername(), false, 7 * 24 * 3600);
    CookieRemberManager.loginSuccess(request, response, user.getId(), user.getPassword(), form.isRemeber());

    return "redirect:" + StringUtils.emptyDefault(form.getRedirectURL(), "/backend/index");
  }

}

@@ -844,24 +844,66 @@
 ookieUtil;%0D%0A
+import com.zblog.core.util.ServletUtils;%0D%0A
 import com.z
@@ -3227,24 +3227,181 @@
 RF_TOKEN);%0D%0A
+    cookieUtil.removeCokie(%22comment_author%22);%0D%0A    cookieUtil.removeCokie(%22comment_author_email%22);%0D%0A    cookieUtil.removeCokie(%22comment_author_url%22);%0D%0A    %0D%0A
     return %22
@@ -4138,32 +4138,79 @@
 st, response);%0D%0A
+    /* %E6%A0%B9%E6%8D%AERFC-2109%E4%B8%AD%E7%9A%84%E8%A7%84%E5%AE%9A%EF%BC%8C%E5%9C%A8Cookie%E4%B8%AD%E5%8F%AA%E8%83%BD%E5%8C%85%E5%90%ABASCII%E7%9A%84%E7%BC%96%E7%A0%81 */%0D%0A
     cookieUtil.s
@@ -4286,24 +4286,342 @@
 4 * 3600);%0D%0A
+    cookieUtil.setCookie(%22comment_author%22, user.getNickName(), %22/%22, false, 365 * 24 * 3600);%0D%0A    cookieUtil.setCookie(%22comment_author_email%22, user.getEmail(), %22/%22, false, 365 * 24 * 3600, false);%0D%0A    cookieUtil.setCookie(%22comment_author_url%22, ServletUtils.getDomain(request), %22/%22, false, 365 * 24 * 3600, false);%0D%0A%0D%0A
     CookieRe

修复登录后台，默认文章回复用户不为当前登录用户bug