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

@ -41,9 +41,12 @@ public static class SessionMethods
var user = session?.User; var user = session?.User;
var folder = Db.GetFolderById(folderId); var folder = Db.GetFolderById(folderId);
var parent = Db.GetFolderById(parentId); var parent = Db.GetFolderById(parentId);
return user is not null return user is not null
&& folder 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.UserType==2
&& user.HasAccessTo(folder) && user.HasAccessTo(folder)
&& user.HasAccessTo(parent) && user.HasAccessTo(parent)
@ -51,6 +54,19 @@ public static class SessionMethods
.Do(() => folder.ParentId = parentId) .Do(() => folder.ParentId = parentId)
.Apply(Db.Update); .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) public static Boolean MoveInstallation(this Session? session, Int64 installationId, Int64 parentId)
{ {