Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the problem of changing password not exist before #2493

Merged
merged 1 commit into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Objects;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import run.halo.app.core.extension.Role;
Expand Down Expand Up @@ -42,7 +43,14 @@ public Mono<User> updatePassword(String username, String newPassword) {
@Override
public Mono<User> updateWithRawPassword(String username, String rawPassword) {
return getUser(username)
.filter(user -> !passwordEncoder.matches(rawPassword, user.getSpec().getPassword()))
.filter(user -> {
if (!StringUtils.hasText(user.getSpec().getPassword())) {
// Check if the old password is set before, or the passwordEncoder#matches
// will complain an error due to null password.
return true;
}
return !passwordEncoder.matches(rawPassword, user.getSpec().getPassword());
})
.flatMap(user -> {
user.getSpec().setPassword(passwordEncoder.encode(rawPassword));
return client.update(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,18 @@ void shouldUpdatePasswordWithDifferentPassword() {

@Test
void shouldUpdatePasswordIfNoPasswordBefore() {
var oldUser = createUser("");
var oldUser = createUser(null);
var newUser = createUser("new-password");

when(client.get(User.class, "fake-user")).thenReturn(Mono.just(oldUser));
when(client.update(oldUser)).thenReturn(Mono.just(newUser));
when(passwordEncoder.matches("new-password", "")).thenReturn(false);
when(passwordEncoder.encode("new-password")).thenReturn("encoded-new-password");

StepVerifier.create(userService.updateWithRawPassword("fake-user", "new-password"))
.expectNext(newUser)
.verifyComplete();

verify(passwordEncoder).matches("new-password", "");
verify(passwordEncoder, never()).matches("new-password", null);
verify(passwordEncoder).encode("new-password");
verify(client).update(argThat(extension -> {
var user = (User) extension;
Expand Down