jmix1.5 如何自定义按钮的权限

jmix1.5 如何自定义按钮的权限,需要编码吗

这块在Jmix去掉了。可以参考 Jmix PermissionType.UI - #3,来自 AnJingjing - Jmix - jmix.cn
自己实现一下

package com.dragonpass.common.auth;

import io.jmix.core.Messages;
import io.jmix.core.security.CurrentAuthentication;
import io.jmix.security.authentication.RoleGrantedAuthority;
import io.jmix.security.model.ResourcePolicy;
import io.jmix.security.model.ResourcePolicyType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 特殊策略权限
 * @author Murphy
 */
@Component
public class SpecificResourcePolicy {

    @Autowired
    private CurrentAuthentication currentAuthentication;
    @Autowired
    private Messages messages;

    private final Logger log = LoggerFactory.getLogger(SpecificResourcePolicy.class);

    /**
     * 校验组件是否权限
     * @param component 组件(带有ID且ID配置了特殊策略)
     * @return 校验结果
     */
    public Boolean hasAuth(String screedId, io.jmix.ui.component.Component component){
        // 权限集合
        var grantedAuthorities = currentAuthentication.getAuthentication().getAuthorities().stream().toList();
        // 获取所有的特殊策略
        List<ResourcePolicy> specifics = new ArrayList();
        grantedAuthorities.forEach(grantedAuthority -> {
            if (grantedAuthority instanceof RoleGrantedAuthority){
                RoleGrantedAuthority roleGrantedAuthority =  (RoleGrantedAuthority) grantedAuthority;
                specifics.addAll(
                        roleGrantedAuthority.getResourcePolicies().stream()
                                .filter(resourcePolicy -> ResourcePolicyType.SPECIFIC.equals(resourcePolicy.getType()))
                                .collect(Collectors.toList()));
            }
        });
        // 根据组件ID匹配策略
        String componentId = "ui." + screedId + "." +component.getId();
        log.info(messages.formatMessage("authentication.specific.resource", "component.id",componentId));
        ResourcePolicy resourcePolicy = specifics.stream().filter(
                specific->specific.getResource().equals(componentId))
                .findAny()
                .orElse(null);
        Boolean hasAuth = !(resourcePolicy == null);
        log.info(messages.formatMessage("authentication.specific.resource", "component.result",componentId));
        return hasAuth;
    }

    /**
     * 根据权限显示/隐藏组件
     * @param component
     */
    public void showByAuth(String screedId, io.jmix.ui.component.Component component){
        Boolean hasAuth = hasAuth(screedId, component);
        component.setVisible(hasAuth);
    }

    /**
     * 根据权限启用/禁用组件
     * @param component
     */
    public void EnableByAuth(String screedId, io.jmix.ui.component.Component component){
        Boolean hasAuth = hasAuth(screedId, component);
        component.setEnabled(hasAuth);
    }
}
1 个赞