Răsfoiți Sursa

Improve detection of folder move into subfolder (#15422)

GitOrigin-RevId: 200d6e10a6e92ca85de24cae6d20f50c697ca584
Alf Eaton 2 ani în urmă
părinte
comite
1314f9082c

+ 24 - 8
services/web/app/src/Features/Project/ProjectEntityMongoUpdateHandler.js

@@ -634,6 +634,26 @@ function _confirmFolder(project, folderId) {
   }
 }
 
+function _checkValidFolderPath(folderPath, destinationFolderPath) {
+  if (!folderPath.endsWith('/')) {
+    folderPath += '/'
+  }
+
+  if (!destinationFolderPath.endsWith('/')) {
+    destinationFolderPath += '/'
+  }
+
+  if (destinationFolderPath === folderPath) {
+    throw new Errors.InvalidNameError('destination folder is the same as me')
+  }
+
+  if (destinationFolderPath.startsWith(folderPath)) {
+    throw new Errors.InvalidNameError(
+      'destination folder is a child folder of me'
+    )
+  }
+}
+
 async function _checkValidMove(
   project,
   entityType,
@@ -647,18 +667,14 @@ async function _checkValidMove(
       element_id: destFolderId,
       type: 'folder',
     })
+
   // check if there is already a doc/file/folder with the same name
   // in the destination folder
   _checkValidElementName(destEntity, entity.name)
+
+  // check if the folder being moved is a parent of the destination folder
   if (/folder/.test(entityType)) {
-    const isNestedFolder =
-      destFolderPath.fileSystem.slice(0, entityPath.fileSystem.length) ===
-      entityPath.fileSystem
-    if (isNestedFolder) {
-      throw new Errors.InvalidNameError(
-        'destination folder is a child folder of me'
-      )
-    }
+    _checkValidFolderPath(entityPath.fileSystem, destFolderPath.fileSystem)
   }
 }
 

+ 18 - 0
services/web/test/unit/src/Project/ProjectEntityMongoUpdateHandlerTests.js

@@ -37,6 +37,11 @@ describe('ProjectEntityMongoUpdateHandler', function () {
       fileSystem: '/test-folder/test-subfolder',
       mongo: 'rootFolder.0.folders.0.folders.0',
     }
+    this.notSubfolder = { _id: ObjectId(), name: 'test-folder-2' }
+    this.notSubfolderPath = {
+      fileSystem: '/test-folder-2/test-subfolder',
+      mongo: 'rootFolder.0.folders.0.folders.0',
+    }
     this.folder = {
       _id: ObjectId(),
       name: 'test-folder',
@@ -748,6 +753,19 @@ describe('ProjectEntityMongoUpdateHandler', function () {
         ).to.be.rejectedWith(Errors.InvalidNameError)
       })
     })
+
+    describe('when moving a folder to a subfolder which starts with the same characters', function () {
+      it('does not throw an error', async function () {
+        await expect(
+          this.subject.promises.moveEntity(
+            this.project._id,
+            this.folder._id,
+            this.notSubfolder._id,
+            'folder'
+          )
+        ).not.to.be.rejectedWith(Errors.InvalidNameError)
+      })
+    })
   })
 
   describe('deleteEntity', function () {