Эх сурвалжийг харах

修复修改分组时缺少必要的验证的问题

mxd 3 жил өмнө
parent
commit
84a46d7766

+ 2 - 0
magic-api/src/main/java/org/ssssssss/magicapi/core/config/JsonCodeConstants.java

@@ -34,6 +34,8 @@ public interface JsonCodeConstants {
 
 	JsonCode MOVE_PATH_CONFLICT = new JsonCode(1011, "移动后路径会冲突,请换一个路径在试");
 
+	JsonCode SAVE_GROUP_PATH_CONFLICT = new JsonCode(1036, "保存后路径会冲突,请换一个路径在试");
+
 	JsonCode REQUEST_METHOD_REQUIRED = new JsonCode(1012, "请求方法不能为空");
 
 	JsonCode REQUEST_PATH_REQUIRED = new JsonCode(1013, "请求路径不能为空");

+ 24 - 2
magic-api/src/main/java/org/ssssssss/magicapi/core/service/impl/DefaultMagicResourceService.java

@@ -213,6 +213,7 @@ public class DefaultMagicResourceService implements MagicResourceService, JsonCo
 		notNull(IoUtils.validateFileName(group.getName()), NAME_INVALID);
 		// 需要填写parentId
 		notNull(group.getParentId(), GROUP_ID_REQUIRED);
+		MagicResourceStorage<? extends MagicEntity> storage = storages.get(group.getType());
 		return writeLock(() -> {
 			Resource resource;
 			// 判断是否要保存到根节点下
@@ -237,11 +238,33 @@ public class DefaultMagicResourceService implements MagicResourceService, JsonCo
 				// 创建文件夹
 				groupResource.mkdir();
 			} else {
+				Group oldGroup = groupCache.get(group.getId());
+				if(storage.requirePath() && !Objects.equals(oldGroup.getPath(), group.getPath())){
+					TreeNode<Group> treeNode = tree(group.getType());
+					String oldPath = oldGroup.getPath();
+					oldGroup.setPath(group.getPath());
+					// 递归找出该组下的文件
+					List<MagicEntity> entities = treeNode.findTreeNode(it -> it.getId().equals(group.getId()))
+							.flat()
+							.stream()
+							.flatMap(it -> fileCache.values().stream().filter(f -> f.getGroupId().equals(it.getId())))
+							.collect(Collectors.toList());
+					for (MagicEntity entity : entities) {
+						String newMappingKey = storage.buildKey(entity);
+						if (pathCache.get(group.getType()).entrySet().stream().anyMatch(entry -> entry.getValue().equals(newMappingKey) && !entry.getKey().equals(entity.getId()))) {
+							// 还原path
+							oldGroup.setPath(oldPath);
+							throw new InvalidArgumentException(SAVE_GROUP_PATH_CONFLICT);
+						}
+					}
+				}
 				Resource oldResource = getGroupResource(group.getId());
 				groupResource = resource.getDirectory(group.getName());
 				isTrue(oldResource != null && oldResource.exists(), GROUP_NOT_FOUND);
 				// 名字不一样时重命名
-				if (!oldResource.getFilePath().equals(resource.getFilePath())) {
+				if (!Objects.equals(oldGroup.getName(), group.getName())) {
+					// 判断分组文件夹需要不存在
+					isTrue(!groupResource.exists(), FILE_SAVE_FAILURE);
 					isTrue(oldResource.renameTo(groupResource), FILE_SAVE_FAILURE);
 				}
 			}
@@ -249,7 +272,6 @@ public class DefaultMagicResourceService implements MagicResourceService, JsonCo
 			if (groupResource.getResource(Constants.GROUP_METABASE).write(JsonUtils.toJsonString(group))) {
 				putGroup(group, groupResource);
 				TreeNode<Group> treeNode = tree(group.getType()).findTreeNode(it -> it.getId().equals(group.getId()));
-				MagicResourceStorage<? extends MagicEntity> storage = storages.get(group.getType());
 				// 刷新分组缓存
 				refreshGroup(groupResource, storage);
 				if (event.getAction() != EventAction.CREATE) {