diff options
author | Aurelien Poscia <aurelien.poscia@sonarsource.com> | 2023-11-17 16:54:31 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-11-20 20:02:38 +0000 |
commit | a9ef47fe7a52a378936ff4be9ffd3fb724f00ee9 (patch) | |
tree | eec770181191aaf4b3b8bb50c1cbbb5069f2476a /server/sonar-webserver-common | |
parent | aeb53a50d3ef212e5cf4068b2c4b17bca1ef6aaf (diff) | |
download | sonarqube-a9ef47fe7a52a378936ff4be9ffd3fb724f00ee9.tar.gz sonarqube-a9ef47fe7a52a378936ff4be9ffd3fb724f00ee9.zip |
SONAR-21051 remove un-necessarry re-fetch of UserDao
Diffstat (limited to 'server/sonar-webserver-common')
3 files changed, 21 insertions, 20 deletions
diff --git a/server/sonar-webserver-common/src/it/java/org/sonar/server/common/user/service/UserServiceIT.java b/server/sonar-webserver-common/src/it/java/org/sonar/server/common/user/service/UserServiceIT.java index 0ad92fa02f4..96aaa999c3c 100644 --- a/server/sonar-webserver-common/src/it/java/org/sonar/server/common/user/service/UserServiceIT.java +++ b/server/sonar-webserver-common/src/it/java/org/sonar/server/common/user/service/UserServiceIT.java @@ -675,8 +675,9 @@ public class UserServiceIT { UserDto admin = db.users().insertUser(); db.users().insertGlobalPermissionOnUser(admin, GlobalPermission.ADMINISTER); + String adminUuid = admin.getUuid(); assertThatThrownBy(() -> { - userService.deactivate(admin.getUuid(), false); + userService.deactivate(adminUuid, false); }) .isInstanceOf(BadRequestException.class) .hasMessage("User is last administrator, and cannot be deactivated"); diff --git a/server/sonar-webserver-common/src/main/java/org/sonar/server/common/user/UserDeactivator.java b/server/sonar-webserver-common/src/main/java/org/sonar/server/common/user/UserDeactivator.java index 92e2d51cdb1..eb379072517 100644 --- a/server/sonar-webserver-common/src/main/java/org/sonar/server/common/user/UserDeactivator.java +++ b/server/sonar-webserver-common/src/main/java/org/sonar/server/common/user/UserDeactivator.java @@ -38,22 +38,21 @@ public class UserDeactivator { this.userAnonymizer = userAnonymizer; } - public UserDto deactivateUser(DbSession dbSession, String login) { - UserDto user = doBeforeDeactivation(dbSession, login); - return deactivateUser(dbSession, user); + public UserDto deactivateUser(DbSession dbSession, UserDto userDto) { + UserDto user = doBeforeDeactivation(dbSession, userDto); + return deactivateUserInternal(dbSession, user); } - public UserDto deactivateUserWithAnonymization(DbSession dbSession, String login) { - UserDto user = doBeforeDeactivation(dbSession, login); + public UserDto deactivateUserWithAnonymization(DbSession dbSession, UserDto userDto) { + UserDto user = doBeforeDeactivation(dbSession, userDto); anonymizeUser(dbSession, user); - return deactivateUser(dbSession, user); + return deactivateUserInternal(dbSession, user); } - private UserDto doBeforeDeactivation(DbSession dbSession, String login) { - UserDto user = getUserOrThrow(dbSession, login); - ensureNotLastAdministrator(dbSession, user); - deleteRelatedData(dbSession, user); - return user; + private UserDto doBeforeDeactivation(DbSession dbSession, UserDto userDto) { + ensureNotLastAdministrator(dbSession, userDto); + deleteRelatedData(dbSession, userDto); + return userDto; } private void ensureNotLastAdministrator(DbSession dbSession, UserDto user) { @@ -82,7 +81,7 @@ public class UserDeactivator { dbClient.scimUserDao().deleteByUserUuid(dbSession, user.getUuid()); } - private UserDto deactivateUser(DbSession dbSession, UserDto user) { + private UserDto deactivateUserInternal(DbSession dbSession, UserDto user) { dbClient.userDao().deactivateUser(dbSession, user); dbSession.commit(); return getUserOrThrow(dbSession, user.getLogin()); diff --git a/server/sonar-webserver-common/src/main/java/org/sonar/server/common/user/service/UserService.java b/server/sonar-webserver-common/src/main/java/org/sonar/server/common/user/service/UserService.java index 5b60eb8b150..32e091e401c 100644 --- a/server/sonar-webserver-common/src/main/java/org/sonar/server/common/user/service/UserService.java +++ b/server/sonar-webserver-common/src/main/java/org/sonar/server/common/user/service/UserService.java @@ -46,6 +46,7 @@ import org.sonar.server.user.UserUpdater; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Strings.emptyToNull; import static java.util.Comparator.comparing; +import static java.util.Objects.requireNonNull; import static org.sonar.server.exceptions.NotFoundException.checkFound; import static org.sonar.server.user.ExternalIdentity.SQ_AUTHORITY; @@ -145,9 +146,9 @@ public class UserService { managedInstanceChecker.throwIfUserIsManaged(dbSession, uuid); UserDto deactivatedUser; if (Boolean.TRUE.equals(anonymize)) { - deactivatedUser = userDeactivator.deactivateUserWithAnonymization(dbSession, userDto.getLogin()); + deactivatedUser = userDeactivator.deactivateUserWithAnonymization(dbSession, userDto); } else { - deactivatedUser = userDeactivator.deactivateUser(dbSession, userDto.getLogin()); + deactivatedUser = userDeactivator.deactivateUser(dbSession, userDto); } dbSession.commit(); return deactivatedUser; @@ -178,18 +179,18 @@ public class UserService { if (Boolean.FALSE.equals(userCreateRequest.isLocal())) { newUserBuilder.setExternalIdentity(new ExternalIdentity(SQ_AUTHORITY, login, login)); } - return registerUser(dbSession, login, newUserBuilder.build()); + return registerUser(dbSession, newUserBuilder.build()); } } - private UserInformation registerUser(DbSession dbSession, String uuid, NewUser newUserBuilder) { - UserDto user = dbClient.userDao().selectByLogin(dbSession, newUserBuilder.login()); + private UserInformation registerUser(DbSession dbSession, NewUser newUser) { + UserDto user = dbClient.userDao().selectByLogin(dbSession, requireNonNull(newUser.login())); if (user == null) { - user = userUpdater.createAndCommit(dbSession, newUserBuilder, u -> { + user = userUpdater.createAndCommit(dbSession, newUser, u -> { }); } else { checkArgument(!user.isActive(), "An active user with login '%s' already exists", user.getLogin()); - user = userUpdater.reactivateAndCommit(dbSession, user, newUserBuilder, u -> { + user = userUpdater.reactivateAndCommit(dbSession, user, newUser, u -> { }); } return fetchUser(user.getUuid()); |