package com.sptg.common.tag.processor;

import com.sptg.common.core.beans.UserBean;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.context.WebEngineContext;
import org.thymeleaf.engine.AttributeName;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.standard.processor.AbstractStandardConditionalVisibilityTagProcessor;
import org.thymeleaf.templatemode.TemplateMode;

import javax.servlet.http.HttpServletRequest;

/**
 * title: SecurityTagProcessor
 * package: com.sptg.common.tag.processor
 * description: 权限标签
 *
 *                  
 *                
 */
public class SecurityTagProcessor extends AbstractStandardConditionalVisibilityTagProcessor {

    private static final String ATTR_NAME = "security";

    public SecurityTagProcessor( final String dialectPrefix) {
        super(TemplateMode.HTML, dialectPrefix, ATTR_NAME, 300);
    }


    @Override
    protected boolean isVisible(
            final ITemplateContext context,
            final IProcessableElementTag tag,
            final AttributeName attributeName, final String attributeValue) {

        if (context instanceof WebEngineContext){
            WebEngineContext webEngineContext = (WebEngineContext)context;
            //  获取request对象
            HttpServletRequest request = webEngineContext.getRequest();
            UserBean onLineUser = (UserBean)request.getAttribute("onLineUser");
            if (null != onLineUser.getTokenList()){
                //  判断是否显示
                return onLineUser.getTokenList().contains(attributeValue);
            }
        }
        return false;
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }
}
