refactor: optimize user creation (#6480)

#### What type of PR is this?
/kind improvement
/area core
/milestone 2.19.x

#### What this PR does / why we need it:
优化用户创建

#### Does this PR introduce a user-facing change?
```release-note
None
```
pull/6482/head
guqing 2024-08-19 15:38:42 +08:00 committed by GitHub
parent 7774eb1a3a
commit 30d482f0f8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 10 additions and 7 deletions

View File

@ -444,13 +444,9 @@ public class UserEndpoint implements CustomEndpoint {
})
.flatMap(userRequest -> {
User newUser = CreateUserRequest.from(userRequest);
return userService.createUser(newUser, userRequest.roles())
.then(Mono.defer(() -> userService.updateWithRawPassword(userRequest.name(),
userRequest.password()))
.retryWhen(Retry.backoff(5, Duration.ofMillis(100))
.filter(OptimisticLockingFailureException.class::isInstance)
)
);
var encryptedPwd = userService.encryptPassword(userRequest.password());
newUser.getSpec().setPassword(encryptedPwd);
return userService.createUser(newUser, userRequest.roles());
})
.flatMap(user -> ServerResponse.ok()
.contentType(MediaType.APPLICATION_JSON)

View File

@ -27,4 +27,6 @@ public interface UserService {
Mono<Boolean> confirmPassword(String username, String rawPassword);
Flux<User> listByEmail(String email);
String encryptPassword(String rawPassword);
}

View File

@ -213,6 +213,11 @@ public class UserServiceImpl implements UserService {
);
}
@Override
public String encryptPassword(String rawPassword) {
return passwordEncoder.encode(rawPassword);
}
void publishPasswordChangedEvent(String username) {
eventPublisher.publishEvent(new PasswordChangedEvent(this, username));
}