935090232@qq.com
2021-11-21 59634aeabb04aae0e819bd4c5fe909bb9cdbeb28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.matrix.system.common.tag;
 
 
import com.matrix.core.tools.WebUtil;
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;
 
/**
 * 页面按钮权限类
 * 
 * @author jiangyouyao
 * @email 512061637@qq.com
 * @date 2019年2月25日
 */
public class ButtonRoleFnTagProcessor extends AbstractAttributeTagProcessor {
 
    /**
     * 标签名
     */
    private static final String ATTR_NAME = "fn";
 
    /**
     * 优先级
     */
    private static final int PRECEDENCE = 10000;
 
    public ButtonRoleFnTagProcessor(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.isFnPermitted(attrValue)) {
            // 如果没有该按钮权限,则直接移除该模块
            structureHandler.removeElement();
        }
 
    }
 
}