added change password

This commit is contained in:
Andrey Terentev 2024-05-03 10:38:36 +07:00 committed by Andrey Terentev
parent 0bd7271ef3
commit 313bdbf2fa
5 changed files with 23 additions and 2 deletions

View File

@ -19,4 +19,6 @@ public interface AccountRepository {
void removeAccount(@NotNull IAccount account); void removeAccount(@NotNull IAccount account);
void setPermissions(@NotNull IAccount account, @NotNull List<String> permissions); void setPermissions(@NotNull IAccount account, @NotNull List<String> permissions);
void setPassword(@NotNull IAccount account, @NotNull String newPassword);
} }

View File

@ -60,4 +60,11 @@ public class AccountRepositoryImpl implements AccountRepository {
params.put("permissions", String.join(",", permissions)); params.put("permissions", String.join(",", permissions));
}); });
} }
@Override
public void setPassword(@NotNull IAccount account, @NotNull String newPassword) {
rest.query("/accounts/" + account.getUsername() + "/password", HttpMethod.PUT, params -> {
params.put("newPassword", newPassword);
});
}
} }

View File

@ -91,7 +91,7 @@ public class AccountDetailsPage extends VerticalLayout implements BeforeEnterObs
return; return;
} }
//TODO: change password client.getAccountRepository().setPassword(account, pass);
Notifications.success("Password successfully changed!"); Notifications.success("Password successfully changed!");
newPassword.setValue(""); newPassword.setValue("");
confirmPassword.setValue(""); confirmPassword.setValue("");

View File

@ -6,6 +6,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import ru.dragonestia.picker.api.exception.AccountDoesNotExistsException; import ru.dragonestia.picker.api.exception.AccountDoesNotExistsException;
import ru.dragonestia.picker.api.exception.PermissionNotFoundException; import ru.dragonestia.picker.api.exception.PermissionNotFoundException;
@ -24,6 +25,7 @@ import java.util.HashSet;
public class AccountsController { public class AccountsController {
private final AccountService accountService; private final AccountService accountService;
private final PasswordEncoder passwordEncoder;
@GetMapping("/current") @GetMapping("/current")
ResponseAccount currentAccount() { ResponseAccount currentAccount() {
@ -97,4 +99,14 @@ public class AccountsController {
return ResponseEntity.ok().build(); return ResponseEntity.ok().build();
} }
@PreAuthorize("hasRole('ADMIN') || principal.username.equals(accountId)")
@PutMapping("/{accountId}/password")
ResponseEntity<?> changePassword(@PathVariable String accountId, @RequestParam String newPassword) {
var account = accountService.findAccount(accountId).orElseThrow(() -> new AccountDoesNotExistsException(accountId));
account.setPassword(passwordEncoder.encode(newPassword));
accountService.updateState(account);
return ResponseEntity.ok().build();
}
} }

View File

@ -22,7 +22,7 @@ public interface AccountService extends UserDetailsService {
@PreAuthorize("hasRole('ADMIN')") @PreAuthorize("hasRole('ADMIN')")
void removeAccount(@NotNull Account account); void removeAccount(@NotNull Account account);
@PreAuthorize("hasRole('ADMIN')") @PreAuthorize("hasRole('ADMIN') || principal.username.equals(account.username)")
void updateState(@NotNull Account account); void updateState(@NotNull Account account);
@Override @Override