package com.matrix.system.common.tag; import com.matrix.system.common.authority.AuthorityManager; import com.matrix.system.common.authority.DefaultAuthorityManager; import com.matrix.system.common.constance.AppConstance; import org.thymeleaf.IEngineConfiguration; import org.thymeleaf.context.ITemplateContext; import org.thymeleaf.engine.AttributeName; import org.thymeleaf.model.IProcessableElementTag; import org.thymeleaf.processor.element.AbstractAttributeTagProcessor; import org.thymeleaf.processor.element.IElementTagStructureHandler; import org.thymeleaf.standard.expression.IStandardExpression; import org.thymeleaf.standard.expression.IStandardExpressionParser; import org.thymeleaf.standard.expression.StandardExpressions; import org.thymeleaf.templatemode.TemplateMode; import com.matrix.core.tools.WebUtil; /** * 页面按钮权限类 * * @author jiangyouyao * @email 512061637@qq.com * @date 2019年2月25日 */ public class ButtonRoleAttributeTagProcessor extends AbstractAttributeTagProcessor { /** * 标签名 */ private static final String ATTR_NAME = "btn"; /** * 优先级 */ private static final int PRECEDENCE = 10000; public ButtonRoleAttributeTagProcessor(final String dialectPrefix) { super( // This processor will apply only to HTML mode TemplateMode.HTML, // Prefix to be applied to name for matching dialectPrefix, // No tag name: match any tag name null, // No prefix to be applied to tag name false, // Name of the attribute that will be matched ATTR_NAME, // Apply dialect prefix to attribute name true, // Precedence (inside dialect's own precedence) PRECEDENCE, // Remove the matched attribute afterwards true); } /** * 自定义标签主方法 * * @author jiangyouyao * @email 512061637@qq.com * @date 2019年1月12日 * @param context * @param tag * @param attributeName * @param attributeValue * @param structureHandler */ @Override protected void doProcess(ITemplateContext context, IProcessableElementTag tag, AttributeName attributeName, String attributeValue, IElementTagStructureHandler structureHandler) { final IEngineConfiguration configuration = context.getConfiguration(); // Obtain the Thymeleaf Standard Expression parser final IStandardExpressionParser parser = StandardExpressions.getExpressionParser(configuration); // Parse the attribute value as a Thymeleaf Standard Expression final IStandardExpression expression = parser.parseExpression(context, attributeValue); // 标签的值 功能code-add 这种类型 final String attrValue = expression.execute(context).toString(); // 此时根据当前登录用户判断是否有该权限 AuthorityManager authority = (DefaultAuthorityManager) WebUtil.getBean(AppConstance.DEFAULT_AUTHORITYMANAGER); if (!authority.isBtnPermitted(attrValue)) { // 如果没有该按钮权限,则直接移除该模块 structureHandler.removeElement(); } } }