prevent moving a category into one of its own descendants (#2355)

This commit is contained in:
Athou
2026-10-30 20:51:45 +01:00
parent 20ec5d3eb9
commit fa8a447787
2 changed files with 17 additions and 1 deletions

View File

@@ -435,6 +435,19 @@ public class CategoryREST {
&& !Strings.CS.equals(req.getParentId(), String.valueOf(req.getId()))) {
parent = feedCategoryDAO.findById(user, Long.valueOf(req.getParentId()));
}
if (parent != null) {
Long parentId = parent.getId();
List<FeedCategory> descendants =
feedCategoryDAO.findAllChildrenCategories(user, category);
boolean parentIsDescendant =
descendants.stream().anyMatch(c -> c.getId().equals(parentId));
if (parentIsDescendant) {
return Response.status(Status.BAD_REQUEST)
.entity(
"<message>cannot set a category's own descendant as its parent</message>")
.build();
}
}
category.setParent(parent);
if (req.getPosition() != null) {