Jmix PermissionType.UI

在CUBA里可以通过PermissionType.UI来控制界面组件的显示,比如某些Button
image

在Jmix的ResourcePolicyType好像并没有UI这个选择?或者有什么东西可以替代PermissionType.UI呢?

image
我看文档里这个功能好像被删除了,那我是不是只能自己去实现界面组件是否显示了?

你好,是的,只能自己实现了。我们有个项目是自己实现的。示例比较简单:

package com.company.testdemo.screen;

import io.jmix.core.security.CurrentAuthentication;
import io.jmix.ui.component.Window;
import io.jmix.ui.screen.UiController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("tstdm_ComponentPermission")
public class ComponentPermission {
    private static final Logger log = LoggerFactory.getLogger(ComponentPermission.class);

    @Autowired
    private CurrentAuthentication currentAuthentication;

   // 仅示例用,实际上需要创建一个数据库实体及其配置界面。
    public static class UiComponentPermission{
        public String username;
        public String screenId;
        public String componentId;
        public boolean disabled;
        public boolean visible;

    }

    public void processNotPermittedComponent(Class<?> clazz,Window window){
        if(clazz.isAnnotationPresent(UiController.class)){
            var annotation = clazz.getAnnotation(UiController.class);
            var screenId = annotation.value();
            // 从注解获取 screenId,然后加载当前用户该界面的配置。
            var permissions = loadPermissionForUser(screenId);
            permissions.forEach(p->{
                var component = window.getComponent(p.componentId);
                if (component != null) {
                    component.setEnabled(!p.disabled);
                    component.setVisible(p.visible);
                }else{
                    log.error("Component {} does not exists in {}",p.componentId,p.screenId);
                }
            });
        }

    }

    private List<UiComponentPermission> loadPermissionForUser(String screenId) {
        var username = currentAuthentication.getUser().getUsername();

        //我这里举个例子,其实应该用 username 和 screenId 加载 UiComponentPermission 表数据
        var uip = new UiComponentPermission();
        uip.username = username;
        uip.screenId ="tstdm_Product.browse";
        uip.componentId = "buttonsPanel";
        uip.disabled = false;
        uip.visible = false;

        return List.of(uip);
    }
}

界面中的用法:

    @Autowired
    private ComponentPermission componentPermission;

    @Subscribe
    public void onAfterShow(AfterShowEvent event) {
        componentPermission.processNotPermittedComponent(getClass(),getWindow());
    }

Thanks a lot, 我參考一下哈