From 8b8bfd423d7b0fffdd38f6f28f44d1831e7497aa Mon Sep 17 00:00:00 2001 From: "yuhang2.zhang" Date: Sun, 1 Dec 2024 17:21:40 +0800 Subject: [PATCH] [Bug] [dinky-web] Task naming rule restrictions. --- .../dinky/controller/CatalogueController.java | 3 +++ .../service/catalogue/CatalogueService.java | 2 ++ .../catalogue/impl/CatalogueServiceImpl.java | 19 +++++++++++++++++++ .../java/org/dinky/data/enums/Status.java | 4 ++++ 4 files changed, 28 insertions(+) diff --git a/dinky-admin/src/main/java/org/dinky/controller/CatalogueController.java b/dinky-admin/src/main/java/org/dinky/controller/CatalogueController.java index 2ed07d9aaf..e989f9c124 100644 --- a/dinky-admin/src/main/java/org/dinky/controller/CatalogueController.java +++ b/dinky-admin/src/main/java/org/dinky/controller/CatalogueController.java @@ -175,6 +175,9 @@ public Result> getCatalogueSortType() { dataTypeClass = CatalogueTaskDTO.class) @CheckTaskOwner(checkParam = TaskId.class, checkInterface = TaskService.class) public Result createTask(@RequestBody CatalogueTaskDTO catalogueTaskDTO) { + if (!catalogueService.isValidTaskName(catalogueTaskDTO.getName())) { + return Result.failed(Status.TASK_NAME_INVALID); + } if (catalogueService.checkCatalogueTaskNameIsExistById(catalogueTaskDTO.getName(), catalogueTaskDTO.getId())) { return Result.failed(Status.TASK_IS_EXIST); } diff --git a/dinky-admin/src/main/java/org/dinky/service/catalogue/CatalogueService.java b/dinky-admin/src/main/java/org/dinky/service/catalogue/CatalogueService.java index 5a9fa7e5bb..d39fba19f5 100644 --- a/dinky-admin/src/main/java/org/dinky/service/catalogue/CatalogueService.java +++ b/dinky-admin/src/main/java/org/dinky/service/catalogue/CatalogueService.java @@ -152,6 +152,8 @@ public interface CatalogueService extends ISuperService { */ boolean checkCatalogueTaskNameIsExistById(String name, Integer id); + boolean isValidTaskName(String name); + /** * Check task operate permission. * Contains reflection invocation. Please do not delete. diff --git a/dinky-admin/src/main/java/org/dinky/service/catalogue/impl/CatalogueServiceImpl.java b/dinky-admin/src/main/java/org/dinky/service/catalogue/impl/CatalogueServiceImpl.java index 392bf968c4..61a4394d94 100644 --- a/dinky-admin/src/main/java/org/dinky/service/catalogue/impl/CatalogueServiceImpl.java +++ b/dinky-admin/src/main/java/org/dinky/service/catalogue/impl/CatalogueServiceImpl.java @@ -70,6 +70,8 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; import org.springframework.stereotype.Service; @@ -263,6 +265,23 @@ public boolean checkCatalogueTaskNameIsExistById(String name, Integer id) { .ne(id != null, Catalogue::getId, id)); } + /** + * check catalogue task name is valid + * + * @param taskName taskName + * @return true if valid , otherwise false + */ + @Override + public boolean isValidTaskName(String taskName) { + String TASK_NAME_PATTERN = "^[a-z0-9][a-z0-9.-]*[a-z0-9]$"; + Pattern pattern = Pattern.compile(TASK_NAME_PATTERN); + if (StringUtils.isBlank(taskName)) { + return false; + } + Matcher matcher = pattern.matcher(taskName); + return matcher.matches(); + } + /** * init some value * diff --git a/dinky-common/src/main/java/org/dinky/data/enums/Status.java b/dinky-common/src/main/java/org/dinky/data/enums/Status.java index 24f516a9a9..6200eccc15 100644 --- a/dinky-common/src/main/java/org/dinky/data/enums/Status.java +++ b/dinky-common/src/main/java/org/dinky/data/enums/Status.java @@ -196,6 +196,10 @@ public enum Status { CATALOGUE_NOT_EXIST(12017, "catalogue.not.exist"), CATALOGUE_IS_EXIST(12018, "catalogue.is.exist"), TASK_NAME_NOT_MATCH_CATALOGUE_NAME(12019, "task.name.not.match.catalogue.name"), + TASK_NAME_INVALID( + 12020, + "The task name must consist of lowercase letters, numbers, '-', or '.', " + + "and it must start and end with a letter or number."), /** * alert instance