From 51972888c5e485aa7b5f9f75776a61bbb78abf0c Mon Sep 17 00:00:00 2001 From: Liu Rui Date: Mon, 7 Oct 2024 17:26:45 +0800 Subject: [PATCH] =?UTF-8?q?feat-RuntimeController-=E5=A2=9E=E5=8A=A0/menu?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=B8=B2=E6=9F=93=E5=BD=93=E5=89=8D=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=AF=B9=E5=BA=94=E7=9A=84=E8=8F=9C=E5=8D=95=E6=A0=91?= =?UTF-8?q?=EF=BC=88=E7=9B=AE=E5=89=8D=E4=BB=85=E5=AE=9E=E7=8E=B0=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=91=98=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../authorization/AuthorizationService.java | 8 ++--- .../net/ximatai/muyun/test/auth/TestAuth.java | 2 +- .../muyun/test/util/TreeBuilderTest.java | 19 ++++++------ .../net/ximatai/muyun/core/MuYunConfig.java | 7 +++++ .../net/ximatai/muyun/util/TreeBuilder.java | 7 +++++ .../muyun/database/std/DataAccessStd.java | 2 +- .../platform/controller/MenuController.java | 18 ++++++++++- .../controller/MenuSchemaController.java | 6 ++++ .../controller/RuntimeController.java | 30 +++++++++++++++++++ 9 files changed, 83 insertions(+), 16 deletions(-) diff --git a/muyun-authorization/src/main/java/net/ximatai/muyun/authorization/AuthorizationService.java b/muyun-authorization/src/main/java/net/ximatai/muyun/authorization/AuthorizationService.java index 79b7131b..3b7bff9f 100644 --- a/muyun-authorization/src/main/java/net/ximatai/muyun/authorization/AuthorizationService.java +++ b/muyun-authorization/src/main/java/net/ximatai/muyun/authorization/AuthorizationService.java @@ -24,7 +24,7 @@ public class AuthorizationService implements IAuthorizationService { @Override public boolean isAuthorized(String userID, String module, String action) { - if (userID.equals(config.superUserId())) { + if (config.isSuperUser(userID)) { return true; } @@ -47,7 +47,7 @@ and id_at_auth_role in (%s) @Override public boolean isDataAuthorized(String userID, String module, String action, String dataID) { - if (userID.equals(config.superUserId())) { + if (config.isSuperUser(userID)) { return true; } @@ -61,7 +61,7 @@ public String getAuthCondition(String userID, String module, String action) { @Override public List getAllowedActions(String userID, String module) { - if (userID.equals(config.superUserId())) { + if (config.isSuperUser(userID)) { List> result = db.query(""" select v_alias from platform.app_module_action @@ -94,7 +94,7 @@ public Set getAuthorizedResources(String userID) { String query; Object[] params; - if (userID.equals(config.superUserId())) { + if (config.isSuperUser(userID)) { query = """ select app_module.v_alias as v_alias_at_app_module, app_module_action.v_alias as v_alias_at_app_module_action diff --git a/muyun-boot/src/test/java/net/ximatai/muyun/test/auth/TestAuth.java b/muyun-boot/src/test/java/net/ximatai/muyun/test/auth/TestAuth.java index 1b5cd52f..e1602f92 100644 --- a/muyun-boot/src/test/java/net/ximatai/muyun/test/auth/TestAuth.java +++ b/muyun-boot/src/test/java/net/ximatai/muyun/test/auth/TestAuth.java @@ -138,7 +138,7 @@ void testGetAuthorizedResources() { @Test void testGetAuthorizedResourcesForSuper() { Set resources = authService.getAuthorizedResources("1"); - assertEquals(12, resources.size()); + assertTrue(resources.size() > 11); } @Test diff --git a/muyun-boot/src/test/java/net/ximatai/muyun/test/util/TreeBuilderTest.java b/muyun-boot/src/test/java/net/ximatai/muyun/test/util/TreeBuilderTest.java index 7ee2997d..5b79abf3 100644 --- a/muyun-boot/src/test/java/net/ximatai/muyun/test/util/TreeBuilderTest.java +++ b/muyun-boot/src/test/java/net/ximatai/muyun/test/util/TreeBuilderTest.java @@ -9,18 +9,19 @@ import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; class TreeBuilderTest { static List list = List.of( - buildNode("A", null), - buildNode("B", null), - buildNode("C", null), - buildNode("A.a", "A"), - buildNode("A.b", "A"), - buildNode("A.a.1", "A.a"), - buildNode("C.a.1", "C.a"), - buildNode("B.a", "B") + buildNode("A", null), + buildNode("B", null), + buildNode("C", null), + buildNode("A.a", "A"), + buildNode("A.b", "A"), + buildNode("A.a.1", "A.a"), + buildNode("C.a.1", "C.a"), + buildNode("B.a", "B") ); @Test @@ -51,7 +52,7 @@ void testTreeANotShowMe() { @Test void testTreeNotFound() { List tree = TreeBuilder.build("id", "pid", list, "X", false, "name", 10); - assertEquals(0, tree.size()); + assertNull(tree); } static Map buildNode(String id, String pid) { diff --git a/muyun-core/src/main/java/net/ximatai/muyun/core/MuYunConfig.java b/muyun-core/src/main/java/net/ximatai/muyun/core/MuYunConfig.java index 5ff6669e..982083d3 100644 --- a/muyun-core/src/main/java/net/ximatai/muyun/core/MuYunConfig.java +++ b/muyun-core/src/main/java/net/ximatai/muyun/core/MuYunConfig.java @@ -2,9 +2,16 @@ import io.smallrye.config.ConfigMapping; +import java.util.Objects; + @ConfigMapping(prefix = "muyun") public interface MuYunConfig { Boolean debug(); String superUserId(); + + default boolean isSuperUser(String userID) { + Objects.requireNonNull(userID); + return userID.equals(superUserId()); + } } diff --git a/muyun-core/src/main/java/net/ximatai/muyun/util/TreeBuilder.java b/muyun-core/src/main/java/net/ximatai/muyun/util/TreeBuilder.java index 56053f07..10318861 100644 --- a/muyun-core/src/main/java/net/ximatai/muyun/util/TreeBuilder.java +++ b/muyun-core/src/main/java/net/ximatai/muyun/util/TreeBuilder.java @@ -9,6 +9,9 @@ public class TreeBuilder { + private TreeBuilder() { + } + public static final String ROOT_PID = "__ROOT__"; public static List build(String pkColumn, String parentKeyColumn, List> list, String rootID, boolean showMe, String labelColumn, Integer maxLevel) { @@ -69,6 +72,10 @@ private static List buildChildren(Map return treeNode; }).collect(Collectors.toList()); + if (children.isEmpty()) { + return null; + } + return children; } } diff --git a/muyun-database-std/src/main/java/net/ximatai/muyun/database/std/DataAccessStd.java b/muyun-database-std/src/main/java/net/ximatai/muyun/database/std/DataAccessStd.java index 72d48c10..23bc61a4 100644 --- a/muyun-database-std/src/main/java/net/ximatai/muyun/database/std/DataAccessStd.java +++ b/muyun-database-std/src/main/java/net/ximatai/muyun/database/std/DataAccessStd.java @@ -165,7 +165,7 @@ public List> query(String sql, List params) { @Override public List> query(String sql) { - return List.of(); + return this.query(sql, Collections.emptyList()); } @Override diff --git a/muyun-platform/src/main/java/net/ximatai/muyun/platform/controller/MenuController.java b/muyun-platform/src/main/java/net/ximatai/muyun/platform/controller/MenuController.java index edcbf7b3..905d7bf5 100644 --- a/muyun-platform/src/main/java/net/ximatai/muyun/platform/controller/MenuController.java +++ b/muyun-platform/src/main/java/net/ximatai/muyun/platform/controller/MenuController.java @@ -1,5 +1,6 @@ package net.ximatai.muyun.platform.controller; +import io.quarkus.runtime.Startup; import jakarta.inject.Inject; import jakarta.ws.rs.Path; import net.ximatai.muyun.ability.IChildAbility; @@ -10,22 +11,37 @@ import net.ximatai.muyun.database.builder.TableWrapper; import net.ximatai.muyun.model.ReferenceInfo; import net.ximatai.muyun.platform.ScaffoldForPlatform; +import net.ximatai.muyun.platform.model.Dict; +import net.ximatai.muyun.platform.model.DictCategory; import java.util.List; import static net.ximatai.muyun.platform.PlatformConst.BASE_PATH; +@Startup @Path(BASE_PATH + "/menu") public class MenuController extends ScaffoldForPlatform implements ITreeAbility, IChildAbility, IReferenceAbility { @Inject ModuleController moduleController; + @Inject + DictCategoryController dictCategoryController; + @Override public String getMainTable() { return "app_menu"; } + @Override + protected void afterInit() { + dictCategoryController.putDictCategory( + new DictCategory("menu_opentype", "platform_dir", "菜单打开方式", 1).setDictList( + new Dict("tab", "内嵌TAB"), + new Dict("window", "新窗口") + ), false); + } + @Override public void fitOut(TableWrapper wrapper) { wrapper @@ -34,7 +50,7 @@ public void fitOut(TableWrapper wrapper) { .addColumn("v_name") .addColumn("v_icon") .addColumn("v_remark") - .addColumn("dict_opentype") + .addColumn("dict_menu_opentype") .addColumn("id_at_app_menu_schema") .addColumn("id_at_app_module") .addColumn(Column.of("b_enable").setDefaultValue(true)) diff --git a/muyun-platform/src/main/java/net/ximatai/muyun/platform/controller/MenuSchemaController.java b/muyun-platform/src/main/java/net/ximatai/muyun/platform/controller/MenuSchemaController.java index 2d062d1d..a52bb79d 100644 --- a/muyun-platform/src/main/java/net/ximatai/muyun/platform/controller/MenuSchemaController.java +++ b/muyun-platform/src/main/java/net/ximatai/muyun/platform/controller/MenuSchemaController.java @@ -1,5 +1,6 @@ package net.ximatai.muyun.platform.controller; +import io.quarkus.runtime.Startup; import jakarta.inject.Inject; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; @@ -7,6 +8,7 @@ import jakarta.ws.rs.QueryParam; import net.ximatai.muyun.ability.IChildrenAbility; import net.ximatai.muyun.ability.ISortAbility; +import net.ximatai.muyun.core.MuYunConfig; import net.ximatai.muyun.core.exception.MyException; import net.ximatai.muyun.database.builder.Column; import net.ximatai.muyun.database.builder.TableWrapper; @@ -23,6 +25,7 @@ import static net.ximatai.muyun.platform.PlatformConst.BASE_PATH; +@Startup @Path(BASE_PATH + "/menuSchema") public class MenuSchemaController extends ScaffoldForPlatform implements IChildrenAbility, ISortAbility { @@ -35,6 +38,9 @@ public class MenuSchemaController extends ScaffoldForPlatform implements IChildr @Inject IAuthorizationService authorizationService; + @Inject + MuYunConfig config; + @Override protected void afterInit() { dictCategoryController.putDictCategory( diff --git a/muyun-platform/src/main/java/net/ximatai/muyun/platform/controller/RuntimeController.java b/muyun-platform/src/main/java/net/ximatai/muyun/platform/controller/RuntimeController.java index d4e016b7..9f77390e 100644 --- a/muyun-platform/src/main/java/net/ximatai/muyun/platform/controller/RuntimeController.java +++ b/muyun-platform/src/main/java/net/ximatai/muyun/platform/controller/RuntimeController.java @@ -4,8 +4,16 @@ import jakarta.inject.Inject; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; +import jakarta.ws.rs.QueryParam; import net.ximatai.muyun.ability.IRuntimeAbility; +import net.ximatai.muyun.core.MuYunConfig; +import net.ximatai.muyun.database.IDatabaseOperationsStd; import net.ximatai.muyun.model.IRuntimeUser; +import net.ximatai.muyun.model.TreeNode; +import net.ximatai.muyun.util.TreeBuilder; + +import java.util.List; +import java.util.Map; import static net.ximatai.muyun.platform.PlatformConst.BASE_PATH; @@ -15,12 +23,34 @@ public class RuntimeController implements IRuntimeAbility { @Inject RoutingContext routingContext; + @Inject + MenuSchemaController menuSchemaController; + + @Inject + MuYunConfig config; + + @Inject + IDatabaseOperationsStd db; + @GET @Path("/whoami") public IRuntimeUser whoami() { return getUser(); } + @GET + @Path("/menu") + public List menu(@QueryParam("terminalType") String terminalType) { +// if (config.isSuperUser(whoami().getId())) { + List> list = db.query(""" + select id,pid,v_alias,v_name,v_url,'' as v_icon,'tab' as opentype from platform.app_module + where b_system = true; + """); + + return TreeBuilder.build("id", "pid", list, null, false, "v_name", null); +// } + } + @Override public RoutingContext getRoutingContext() { return routingContext;