refactor: 优化菜单初始化逻辑

This commit is contained in:
zhouhao 2025-09-27 12:14:26 +08:00
parent 8e829817c5
commit 3811266945
3 changed files with 37 additions and 11 deletions

View File

@ -15,9 +15,11 @@
*/
package org.jetlinks.community.auth.configuration;
import com.google.common.collect.Sets;
import lombok.Getter;
import lombok.Setter;
import org.hswebframework.web.authorization.Authentication;
import org.hswebframework.web.authorization.DefaultDimensionType;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.Collections;
@ -29,6 +31,11 @@ import java.util.Set;
@ConfigurationProperties(prefix = "menu")
public class MenuProperties {
//默认只有角色和可以绑定菜单
private Set<String> dimensions = Sets.newHashSet(
DefaultDimensionType.role.getId()
);
private Set<String> allowAllMenusUsers = new HashSet<>(Collections.singletonList("admin"));
private String allowPermission = "menu";

View File

@ -17,7 +17,6 @@ package org.jetlinks.community.auth.initialize;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.hswebframework.web.api.crud.entity.QueryParamEntity;
import org.hswebframework.web.authorization.DefaultDimensionType;
import org.hswebframework.web.authorization.Permission;
import org.hswebframework.web.authorization.events.AuthorizationInitializeEvent;
@ -71,22 +70,16 @@ public class MenuAuthenticationInitializeService {
permissionCacheHelper
.getPermissionCaching(),
// T2: 菜单定义列表
menuService
.createQuery()
.where(MenuEntity::getStatus, 1)
.fetch()
.collectList(),
menuCaching,
// T3: 角色赋予的菜单列表
menuService
.getGrantedMenus(QueryParamEntity.of(), event
.getAuthentication()
.getDimensions())
.getGrantedMenus(event.getAuthentication().getDimensions(),
menuCaching)
.collectList()
.filter(CollectionUtils::isNotEmpty)
)
.<Permission>flatMapIterable(tp3 -> {
Map<String, PermissionEntity> permissions = tp3.getT1();
List<MenuEntity> menus = tp3.getT2();
List<MenuEntity> menus = new ArrayList<>(tp3.getT2().values());
List<MenuView> grantedMenus = tp3.getT3();
MenuGrantRequest request = new MenuGrantRequest();
request.setTargetType(DefaultDimensionType.role.getId());

View File

@ -161,6 +161,32 @@ public class DefaultMenuService
.as(this::convertToView);
}
public Flux<MenuView> getGrantedMenus(List<Dimension> dimensions,
Mono<Map<String, MenuEntity>> menuEntityMap) {
if (CollectionUtils.isEmpty(dimensions)) {
return Flux.empty();
}
List<String> keyList = dimensions
.stream()
.filter(this::isMenuDimension)
.map(dimension -> MenuBindEntity.generateTargetKey(dimension.getType().getId(), dimension.getId()))
.collect(Collectors.toList());
return convertToView(CollectionUtils.isEmpty(keyList)
? Flux.empty()
: bindRepository
.createQuery()
.where()
.in(MenuBindEntity::getTargetKey, keyList)
.fetch(),
menuEntityMap);
}
private boolean isMenuDimension(Dimension dimension) {
//是配置中的维度并且没有忽略
return properties.getDimensions().contains(dimension.getType().getId());
}
public Flux<MenuView> getGrantedMenus(String dimensionType, String dimensionId) {
return getGrantedMenus(dimensionType, Collections.singleton(dimensionId));
}