From 4f35d86d9106826bfbb91611d43c6134ae6a096e Mon Sep 17 00:00:00 2001 From: netroms Date: Fri, 28 Jul 2023 12:59:02 +0800 Subject: [PATCH] fix: update createdby when replicating users [DHIS2-15595] (#14730) * fix: update createdby when replicating users --- .../webapi/controller/UserControllerTest.java | 23 +++++++++++++++++++ .../controller/user/UserController.java | 1 + 2 files changed, 24 insertions(+) diff --git a/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/UserControllerTest.java b/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/UserControllerTest.java index eca8149cb5f0..b1d003b9413f 100644 --- a/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/UserControllerTest.java +++ b/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/UserControllerTest.java @@ -235,6 +235,29 @@ void testReplicateUser() { .content()); } + @Test + void testReplicateUserCreatedByUpdated() throws JsonProcessingException { + User newUser = createUserWithAuth("test", "ALL"); + + switchToNewUser(newUser); + + String replicatedUsername = "peter2"; + + assertWebMessage( + "Created", + 201, + "OK", + "User replica created", + POST( + "/users/" + peter.getUid() + "/replica", + "{'username':'" + replicatedUsername + "','password':'Saf€sEcre1'}") + .content()); + + User replicatedUser = userService.getUserByUsername(replicatedUsername); + + assertEquals(newUser.getUsername(), replicatedUser.getCreatedBy().getUsername()); + } + @Test void testReplicateUser_UserNameAlreadyTaken() { assertWebMessage( diff --git a/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/user/UserController.java b/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/user/UserController.java index 93d198a1ba73..43f9a30e08fd 100644 --- a/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/user/UserController.java +++ b/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/user/UserController.java @@ -469,6 +469,7 @@ public WebMessage replicateUser( userReplica.setUid(CodeGenerator.generateUid()); userReplica.setCode(null); userReplica.setCreated(new Date()); + userReplica.setCreatedBy(currentUser); userReplica.setLdapId(null); userReplica.setOpenId(null); userReplica.setUsername(username);