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

@@ -37,6 +37,9 @@ export function CategoryDetailsPage() {
category = query.result && flattenCategoryTree(query.result.data).find(c => c.id === id) category = query.result && flattenCategoryTree(query.result.data).find(c => c.id === id)
} }
// a category cannot be moved under itself or one of its own descendants
const excludedCategoryIds = category ? flattenCategoryTree(category).map(c => c.id) : [id]
const form = useForm<CategoryModificationRequest>() const form = useForm<CategoryModificationRequest>()
const { setValues } = form const { setValues } = form
@@ -125,7 +128,7 @@ export function CategoryDetailsPage() {
label={<Trans>Parent Category</Trans>} label={<Trans>Parent Category</Trans>}
{...form.getInputProps("parentId")} {...form.getInputProps("parentId")}
clearable clearable
withoutCategoryIds={[id]} withoutCategoryIds={excludedCategoryIds}
/> />
<NumberInput label={<Trans>Position</Trans>} {...form.getInputProps("position")} required min={0} /> <NumberInput label={<Trans>Position</Trans>} {...form.getInputProps("position")} required min={0} />
</> </>

View File

@@ -435,6 +435,19 @@ public class CategoryREST {
&& !Strings.CS.equals(req.getParentId(), String.valueOf(req.getId()))) { && !Strings.CS.equals(req.getParentId(), String.valueOf(req.getId()))) {
parent = feedCategoryDAO.findById(user, Long.valueOf(req.getParentId())); 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); category.setParent(parent);
if (req.getPosition() != null) { if (req.getPosition() != null) {