From 517b0b0d850eb7fcd713111074e856ee5b990bcd Mon Sep 17 00:00:00 2001 From: YueGuobin Date: Thu, 5 Nov 2026 19:22:27 +0800 Subject: [PATCH] fix: attribute duplicated project to the user who duplicated it duplicate() kept the original author in created_by. The project list filters on created_by, so a non-admin user could not see the copy they just made. Pass the caller through both duplication paths, the same way import already does since #3017. --- gns3server/api/routes/controller/projects.py | 6 +++++- gns3server/controller/project.py | 12 ++++++++--- tests/api/routes/controller/test_projects.py | 4 ++++ tests/controller/test_project.py | 22 +++++++++++++++++++- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/gns3server/api/routes/controller/projects.py b/gns3server/api/routes/controller/projects.py index 69a1a7183..63fc1d87b 100644 --- a/gns3server/api/routes/controller/projects.py +++ b/gns3server/api/routes/controller/projects.py @@ -886,7 +886,11 @@ async def duplicate_project( raise HTTPException(status_code=status.HTTP_403_FORBIDDEN) reset_mac_addresses = project_data.reset_mac_addresses - new_project = await project.duplicate(name=project_data.name, reset_mac_addresses=reset_mac_addresses) + new_project = await project.duplicate( + name=project_data.name, + reset_mac_addresses=reset_mac_addresses, + user=current_user.username, + ) # Add the new project in the same resource pools if the duplicated project belongs to any if pool_memberships: diff --git a/gns3server/controller/project.py b/gns3server/controller/project.py index 501e9f4e5..dcbebc8bc 100644 --- a/gns3server/controller/project.py +++ b/gns3server/controller/project.py @@ -2158,7 +2158,7 @@ class Project: while self._loading: await asyncio.sleep(0.5) - async def duplicate(self, name=None, reset_mac_addresses=True): + async def duplicate(self, name=None, reset_mac_addresses=True, user=None): """ Duplicate a project @@ -2169,6 +2169,7 @@ class Project: :param name: Name of the new project. A new one will be generated in case of conflicts :param reset_mac_addresses: Reset MAC addresses for the new project + :param user: The user duplicating the project. The new project is owned by this user """ # If the project was not open we open it temporary @@ -2180,7 +2181,7 @@ class Project: assert self._status != "closed" try: - proj = await self._fast_duplication(name, reset_mac_addresses) + proj = await self._fast_duplication(name, reset_mac_addresses=reset_mac_addresses, user=user) if proj: if previous_status == "closed": await self.close() @@ -2222,6 +2223,7 @@ class Project: name=name, reset_mac_addresses=reset_mac_addresses, keep_compute_ids=True, + user=user, ) log.info(f"Project '{project.name}' duplicated in {time.time() - begin:.4f} seconds") @@ -2233,7 +2235,7 @@ class Project: return project - async def _fast_duplication(self, name=None, location=None, reset_mac_addresses=True): + async def _fast_duplication(self, name=None, location=None, reset_mac_addresses=True, user=None): """ Fast duplication of a project. @@ -2242,6 +2244,7 @@ class Project: :param name: Name of the new project. A new one will be generated in case of conflicts :param location: Parent directory of the new project :param reset_mac_addresses: Reset MAC addresses for the duplicated project + :param user: The user duplicating the project. The new project is owned by this user """ # We don't duplicate a running project @@ -2280,6 +2283,9 @@ class Project: topology["auto_open"] = False topology["auto_close"] = False + if user: + topology["created_by"] = user + # regenerate IDs for the duplicated project regenerate_topology_ids(topology, new_project_path, reset_mac_addresses) diff --git a/tests/api/routes/controller/test_projects.py b/tests/api/routes/controller/test_projects.py index fa5e8e270..11b16d366 100644 --- a/tests/api/routes/controller/test_projects.py +++ b/tests/api/routes/controller/test_projects.py @@ -526,6 +526,8 @@ class TestControllerProjectRoutes: with open(os.path.join(project.path, "demo")) as f: content = f.read() assert content == "hello" + # the imported project is owned by the user who imported it + assert project.created_by == "admin" async def test_import_with_project_name( self, app: FastAPI, client: AsyncClient, tmpdir, controller: Controller @@ -556,6 +558,8 @@ class TestControllerProjectRoutes: ) assert response.status_code == status.HTTP_201_CREATED assert response.json()["name"] == "hello" + # the duplicated project is owned by the user who duplicated it + assert response.json()["created_by"] == "admin" async def test_lock_unlock(self, app: FastAPI, client: AsyncClient, project: Project, compute: Compute) -> None: diff --git a/tests/controller/test_project.py b/tests/controller/test_project.py index 9ea1c6726..20fc1f362 100644 --- a/tests/controller/test_project.py +++ b/tests/controller/test_project.py @@ -936,9 +936,10 @@ async def test_duplicate(project, controller): compute, "test", None, node_type="vmware", properties={"startup_config": "test.cfg"} ) - new_project = await project.duplicate(name="Hello") + new_project = await project.duplicate(name="Hello", user="user1") assert new_project.id != project.id assert new_project.name == "Hello" + assert new_project.created_by == "user1" await new_project.open() @@ -946,6 +947,25 @@ async def test_duplicate(project, controller): assert list(new_project.nodes.values())[1].compute.id == "remote" +@pytest.mark.asyncio +async def test_duplicate_created_by(project, controller): + """ + The duplicated project is owned by the user who duplicated it (fast path) + """ + + project.created_by = "alice" + project.dump() + + new_project = await project.duplicate(name="Hello", user="bob") + assert new_project.created_by == "bob" + # the original project keeps its owner + assert project.created_by == "alice" + + # without a user the original owner is kept (backward compatible) + new_project = await project.duplicate(name="Hello2") + assert new_project.created_by == "alice" + + def test_snapshots(project): """ List the snapshots