Skip to content

Commit

Permalink
feat-RuntimeController-增加/menu接口渲染当前用户对应的菜单树(目前仅实现管理员)
Browse files Browse the repository at this point in the history
  • Loading branch information
aruis committed Oct 7, 2024
1 parent 481ac5f commit 5197288
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -61,7 +61,7 @@ public String getAuthCondition(String userID, String module, String action) {

@Override
public List<String> getAllowedActions(String userID, String module) {
if (userID.equals(config.superUserId())) {
if (config.isSuperUser(userID)) {
List<Map<String, Object>> result = db.query("""
select v_alias
from platform.app_module_action
Expand Down Expand Up @@ -94,7 +94,7 @@ public Set<AuthorizedResource> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void testGetAuthorizedResources() {
@Test
void testGetAuthorizedResourcesForSuper() {
Set<AuthorizedResource> resources = authService.getAuthorizedResources("1");
assertEquals(12, resources.size());
assertTrue(resources.size() > 11);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -51,7 +52,7 @@ void testTreeANotShowMe() {
@Test
void testTreeNotFound() {
List<TreeNode> tree = TreeBuilder.build("id", "pid", list, "X", false, "name", 10);
assertEquals(0, tree.size());
assertNull(tree);
}

static Map buildNode(String id, String pid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

public class TreeBuilder {

private TreeBuilder() {
}

public static final String ROOT_PID = "__ROOT__";

public static List<TreeNode> build(String pkColumn, String parentKeyColumn, List<Map<String, Object>> list, String rootID, boolean showMe, String labelColumn, Integer maxLevel) {
Expand Down Expand Up @@ -69,6 +72,10 @@ private static List<TreeNode> buildChildren(Map<String, List<Map<String, Object>
return treeNode;
}).collect(Collectors.toList());

if (children.isEmpty()) {
return null;
}

return children;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public List<Map<String, Object>> query(String sql, List<?> params) {

@Override
public List<Map<String, Object>> query(String sql) {
return List.of();
return this.query(sql, Collections.emptyList());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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;
import jakarta.ws.rs.PathParam;
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;
Expand All @@ -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 {

Expand All @@ -35,6 +38,9 @@ public class MenuSchemaController extends ScaffoldForPlatform implements IChildr
@Inject
IAuthorizationService authorizationService;

@Inject
MuYunConfig config;

@Override
protected void afterInit() {
dictCategoryController.putDictCategory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<TreeNode> menu(@QueryParam("terminalType") String terminalType) {
// if (config.isSuperUser(whoami().getId())) {
List<Map<String, Object>> 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;
Expand Down

0 comments on commit 5197288

Please sign in to comment.