package org.ccpit.base.security;

import org.ccpit.base.controller.BaseController;
import org.ccpit.base.user.User;
import org.ccpit.base.user.UserService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * Created by Administrator on 2015/9/8.
 */
public class CmsTagSupport extends TagSupport implements ApplicationContextAware{
    static String SPLIT = ",";
    static ApplicationContext ac;

    public User getUser(){
        HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
        Object obj = req.getSession().getAttribute(BaseController.USER_IN_SESSION);
        if (obj==null)
            return null;
        return (User) obj;
//        UserService userService = ac.getBean(UserService.class);
//        User user = userService.getUser(1L);
//        return user;
    }

    @Override
    public int doStartTag() throws JspException {
        if (getUser()==null)
            return SKIP_BODY;
        return validate()?EVAL_PAGE:SKIP_BODY;
    }

    /**
     * validate the security return the result
     * @return
     */
    public boolean validate(){
        return false;
    };

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ac=applicationContext;
    }
}
