avoid adding folder to itself, children and parent

This commit is contained in:
Yinyin Liu 2026-02-26 13:13:38 +01:00
parent 80639e9169
commit abedc6c203
1 changed files with 17 additions and 1 deletions

View File

@ -44,6 +44,9 @@ public static class SessionMethods
return user is not null
&& folder is not null
&& parent is not null
&& folderId != parentId // can't move into itself
&& !IsFolderAncestorOf(folderId, parentId) // can't move into a descendant
&& user.UserType==2
&& user.HasAccessTo(folder)
&& user.HasAccessTo(parent)
@ -52,6 +55,19 @@ public static class SessionMethods
.Apply(Db.Update);
}
// Walks up the folder tree from candidateDescendantId to check if ancestorId is an ancestor.
// Prevents circular references when moving a folder into one of its own descendants.
private static Boolean IsFolderAncestorOf(Int64 ancestorId, Int64 candidateDescendantId)
{
var current = Db.GetFolderById(candidateDescendantId);
while (current is not null && current.ParentId != 0)
{
if (current.ParentId == ancestorId) return true;
current = Db.GetFolderById(current.ParentId);
}
return false;
}
public static Boolean MoveInstallation(this Session? session, Int64 installationId, Int64 parentId)
{
var user = session?.User;