Продолжаем правки по Алексею Александровичу.

This commit is contained in:
Programmist73 2023-05-13 11:32:16 +04:00
parent 6a83584f50
commit 1205d4125f
4 changed files with 33 additions and 57 deletions

View File

@ -10,8 +10,8 @@ import java.util.List;
public class ClientDTO {
private long id;
private String login;
private String email;
private String password;
private String email;
private Integer balance;
private List<TankDTO> tanks;
private UserRole role;
@ -51,20 +51,20 @@ public class ClientDTO {
this.login = login;
}
public String getEmail(){
return email;
}
public void setEmail(String email) {
this.email = email;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
public String getEmail(){
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getBalance(){

View File

@ -22,7 +22,7 @@ import java.util.List;
import java.util.stream.IntStream;
@Controller
@RequestMapping("/client")
@RequestMapping("/clients")
public class ClientMvcController {
private final GameClientService gameClientService;
private final TankService tankService;
@ -39,7 +39,7 @@ public class ClientMvcController {
return "client-edit";
}
@GetMapping(value = "/all")
@GetMapping
@Secured({UserRole.AsString.ADMIN})
public String getClients(@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "5") int size,
@ -61,26 +61,26 @@ public class ClientMvcController {
return "clients";
}
//@GetMapping(value = {"/edit", "/edit/{id}"})
@GetMapping(value = {"/edit", "/edit/{id}"})
@PostMapping
public String editClient(@ModelAttribute @Valid ClientDTO userDto,
BindingResult bindingResult,
Model model) {
/*if(id == null || id <= 0){
if(userDto.getId() <= 0){
model.addAttribute("supportClientDTO", new SupportClientDTO());
}
else {
model.addAttribute("clientId", id);
model.addAttribute("supportClientDTO", new SupportClientDTO(gameClientService.findClient(id)));
model.addAttribute("clientId", userDto.getId());
model.addAttribute("supportClientDTO", new SupportClientDTO(gameClientService.findClient(userDto.getId())));
}
List<TankDTO> tanks = tankService.findAllTanks().stream()
.map(TankDTO::new)
.toList();
model.addAttribute("tanks", tanks);*/
model.addAttribute("tanks", tanks);
if (bindingResult.hasErrors()) {
/*if (bindingResult.hasErrors()) {
model.addAttribute("errors", bindingResult.getAllErrors());
return "client-edit";
}
@ -95,12 +95,12 @@ public class ClientMvcController {
.getAuthorities();
UsernamePasswordAuthenticationToken authentication =
new UsernamePasswordAuthenticationToken(userDto.getLogin(), userDto.getPassword(), nowAuthorities);
new UsernamePasswordAuthenticationToken(userDto.getLogin(), nowAuthorities);
SecurityContextHolder.getContext().setAuthentication(authentication);
} catch (ValidationException e) {
model.addAttribute("errors", e.getMessage());
}
}*/
return "client-edit";
}
@ -128,6 +128,7 @@ public class ClientMvcController {
return "client-edit";
}
//тут два раза прокидываем пароль, так как метод требует на вход строку с подтверждением пароля
if(id == null || id <= 0){
gameClientService.addClient(clientDTO.getLogin(), clientDTO.getPassword(), clientDTO.getEmail(), clientDTO.getBalance(), clientDTO.getPassword(), clientDTO.getRole());
} else {

View File

@ -45,18 +45,8 @@ public class GameClient {
this.role = role;
}
public GameClient(ClientDTO clientDTO){
this.login = clientDTO.getLogin();
this.password = clientDTO.getPassword();
this.email = clientDTO.getEmail();
this.role = clientDTO.getRole();
}
public GameClient(UserSignupDto userSignupDto){
this.login = userSignupDto.getLogin();
this.email = userSignupDto.getEmail();
this.password = userSignupDto.getPassword();
this.role = UserRole.USER;
public GameClient(String login, String email, String password, Integer balance){
this(login, email, password, balance, UserRole.USER);
}
public Long getId(){
@ -126,12 +116,12 @@ public class GameClient {
GameClient gameClient = (GameClient) o;
return Objects.equals(id, gameClient.id);
return Objects.equals(id, gameClient.id) && Objects.equals(login, gameClient.login);
}
@Override
public int hashCode() {
return Objects.hash(id);
return Objects.hash(id, login);
}
//преобразование данных по объекту в строчку

View File

@ -34,7 +34,10 @@ public class GameClientService implements UserDetailsService {
private final ValidatorUtil validatorUtil;
private final PasswordEncoder passwordEncoder;
public GameClientService(GameClientRepository gameClientRepository, TankRepository tankRepository, ValidatorUtil validatorUtil, PasswordEncoder passwordEncoder){
public GameClientService(GameClientRepository gameClientRepository,
TankRepository tankRepository,
ValidatorUtil validatorUtil,
PasswordEncoder passwordEncoder) {
this.gameClientRepository = gameClientRepository;
this.tankRepository = tankRepository;
this.validatorUtil = validatorUtil;
@ -60,21 +63,8 @@ public class GameClientService implements UserDetailsService {
}
@Transactional
public GameClient addClient(UserSignupDto userSignupDto) {
//проверка на наличие такого же логина
if (findByLogin(userSignupDto.getLogin()) != null) {
throw new ValidationException(String.format("User '%s' already exists", userSignupDto.getLogin()));
}
if (!Objects.equals(userSignupDto.getPassword(), userSignupDto.getPasswordConfirm())) {
throw new ValidationException("Passwords not equals");
}
final GameClient client = new GameClient(userSignupDto);
validatorUtil.validate(client);
return gameClientRepository.save(client);
public GameClient addClient(String login, String email, String password, Integer ballance, String passwordConfirm) {
return addClient(login, email, password, ballance, passwordConfirm, UserRole.USER);
}
@Transactional(readOnly = true)
@ -143,12 +133,7 @@ public class GameClientService implements UserDetailsService {
throw new ValidationException(String.format("User '%s' already exists", clientDTO.getLogin()));
}
if (!passwordEncoder.matches(clientDTO.getPassword(), currentGameClient.getPassword())) {
throw new ValidationException("Incorrect password");
}
currentGameClient.setLogin(clientDTO.getLogin());
currentGameClient.setPassword(clientDTO.getPassword());
currentGameClient.setEmail(clientDTO.getEmail());
currentGameClient.setBalance(clientDTO.getBalance());
@ -176,11 +161,11 @@ public class GameClientService implements UserDetailsService {
//метод загрузки пользователя по его логину
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
final GameClient userEntity = findByLogin(username);
if (userEntity == null) {
final GameClient clientEntity = findByLogin(username);
if (clientEntity == null) {
throw new UsernameNotFoundException(username);
}
return new org.springframework.security.core.userdetails.User(
userEntity.getLogin(), userEntity.getPassword(), Collections.singleton(userEntity.getRole()));
clientEntity.getLogin(), clientEntity.getPassword(), Collections.singleton(clientEntity.getRole()));
}
}