Вновь дела делаются.
This commit is contained in:
parent
42e4e9c1cc
commit
c3577d6ad5
@ -24,9 +24,13 @@ public class ClientDTO {
|
||||
this.password = gameClient.getPassword();
|
||||
this.email = gameClient.getEmail();
|
||||
this.balance = gameClient.getBalance();
|
||||
this.tanks = gameClient.getTanks().stream()
|
||||
.map(TankDTO::new)
|
||||
.toList();
|
||||
if(gameClient.getTanks() != null){
|
||||
this.tanks = gameClient.getTanks().stream()
|
||||
.map(TankDTO::new)
|
||||
.toList();
|
||||
} else {
|
||||
this.tanks = null;
|
||||
}
|
||||
this.role = gameClient.getRole();
|
||||
}
|
||||
|
||||
|
@ -19,15 +19,6 @@ public class UserSignupDto {
|
||||
@Size(min = 6, max = 64)
|
||||
private String passwordConfirm;
|
||||
|
||||
public UserSignupDto() {}
|
||||
|
||||
public UserSignupDto(String login, String password, String passwordConfirm, String email) {
|
||||
this.login = login;
|
||||
this.password = password;
|
||||
this.passwordConfirm = passwordConfirm;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public void setLogin(String login){
|
||||
this.login = login;
|
||||
}
|
||||
|
@ -103,6 +103,10 @@ public class GameClient {
|
||||
this.tanks.add(tank);
|
||||
}
|
||||
|
||||
public void setTanks(List<Tank> tanks){
|
||||
this.tanks = tanks;
|
||||
}
|
||||
|
||||
public UserRole getRole() {
|
||||
return role;
|
||||
}
|
||||
|
@ -5,11 +5,14 @@ import org.springframework.security.core.GrantedAuthority;
|
||||
public enum UserRole implements GrantedAuthority {
|
||||
ADMIN,
|
||||
USER;
|
||||
|
||||
private static final String PREFIX = "ROLE_";
|
||||
|
||||
@Override
|
||||
public String getAuthority() {
|
||||
return PREFIX + this.name();
|
||||
}
|
||||
|
||||
public static final class AsString {
|
||||
public static final String ADMIN = PREFIX + "ADMIN";
|
||||
public static final String USER = PREFIX + "USER";
|
||||
|
@ -1,39 +1,82 @@
|
||||
package premium_store.service;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import premium_store.controller.DTO.ClientDTO;
|
||||
import premium_store.controller.DTO.SupportClientDTO;
|
||||
import premium_store.controller.DTO.UserSignupDto;
|
||||
import premium_store.model.GameClient;
|
||||
import premium_store.model.Tank;
|
||||
import premium_store.model.UserRole;
|
||||
import premium_store.repository.GameClientRepository;
|
||||
import premium_store.repository.TankRepository;
|
||||
import premium_store.service.exception.ClientNotFoundException;
|
||||
import premium_store.util.validation.ValidatorUtil;
|
||||
|
||||
import javax.validation.ValidationException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class GameClientService {
|
||||
public class GameClientService implements UserDetailsService {
|
||||
private final GameClientRepository gameClientRepository;
|
||||
private final TankRepository tankRepository;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
public GameClientService(GameClientRepository gameClientRepository, TankRepository tankRepository, ValidatorUtil validatorUtil){
|
||||
public GameClientService(GameClientRepository gameClientRepository, TankRepository tankRepository, ValidatorUtil validatorUtil, PasswordEncoder passwordEncoder){
|
||||
this.gameClientRepository = gameClientRepository;
|
||||
this.tankRepository = tankRepository;
|
||||
this.validatorUtil = validatorUtil;
|
||||
this.passwordEncoder = passwordEncoder;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public GameClient addClient(String login, String newEmail, String password, Integer newBallance) {
|
||||
final GameClient gameClient = new GameClient(login, newEmail, password, newBallance);
|
||||
public GameClient addClient(String login, String email, String password, Integer ballance, String passwordConfirm, UserRole role) {
|
||||
//проверка на наличие такого же логина
|
||||
if (findByLogin(login) != null) {
|
||||
throw new ValidationException(String.format("User '%s' already exists", login));
|
||||
}
|
||||
|
||||
//проверка на соответствие пароля?
|
||||
if (!Objects.equals(password, passwordConfirm)) {
|
||||
throw new ValidationException("Passwords not equals");
|
||||
}
|
||||
|
||||
final GameClient gameClient = new GameClient(login, email, passwordEncoder.encode(password), ballance, role);
|
||||
validatorUtil.validate(gameClient);
|
||||
|
||||
return gameClientRepository.save(gameClient);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public GameClient findClient(Long id) {
|
||||
final Optional<GameClient> client = gameClientRepository.findById(id);
|
||||
@ -41,11 +84,20 @@ public class GameClientService {
|
||||
return client.orElseThrow(() -> new ClientNotFoundException(id));
|
||||
}
|
||||
|
||||
public GameClient findByLogin(String login) {
|
||||
return gameClientRepository.findOneByLoginIgnoreCase(login);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<GameClient> findAllClients() {
|
||||
return gameClientRepository.findAll();
|
||||
}
|
||||
|
||||
//метод для реализации пагинации на странице с пользователями
|
||||
public Page<GameClient> findAllPages(int page, int size) {
|
||||
return gameClientRepository.findAll(PageRequest.of(page - 1, size, Sort.by("id").ascending()));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public GameClient updateClient(Long id, String login, String newPassword, String newEmail, Integer newBalance, Tank newTank) {
|
||||
if (id <= 0) {
|
||||
@ -84,6 +136,16 @@ public class GameClientService {
|
||||
}
|
||||
|
||||
final GameClient currentGameClient = findClient(clientDTO.getId());
|
||||
final GameClient sameClient = findByLogin(clientDTO.getLogin());
|
||||
|
||||
//проверка логина и пароля
|
||||
if (sameClient != null && !Objects.equals(sameClient.getId(), currentGameClient.getId())) {
|
||||
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());
|
||||
@ -110,4 +172,15 @@ public class GameClientService {
|
||||
public void deleteAllClients() {
|
||||
gameClientRepository.deleteAll();
|
||||
}
|
||||
|
||||
//метод загрузки пользователя по его логину
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
final GameClient userEntity = findByLogin(username);
|
||||
if (userEntity == null) {
|
||||
throw new UsernameNotFoundException(username);
|
||||
}
|
||||
return new org.springframework.security.core.userdetails.User(
|
||||
userEntity.getLogin(), userEntity.getPassword(), Collections.singleton(userEntity.getRole()));
|
||||
}
|
||||
}
|
||||
|
@ -1,62 +0,0 @@
|
||||
package premium_store.service;
|
||||
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import premium_store.model.GameClient;
|
||||
import premium_store.repository.GameClientRepository;
|
||||
import premium_store.util.validation.ValidatorUtil;
|
||||
|
||||
import javax.validation.ValidationException;
|
||||
import java.util.Collections;
|
||||
import java.util.Objects;
|
||||
|
||||
@Service
|
||||
public class UserService implements UserDetailsService {
|
||||
private final GameClientRepository userRepository;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
|
||||
public UserService(GameClientRepository userRepository,
|
||||
PasswordEncoder passwordEncoder,
|
||||
ValidatorUtil validatorUtil) {
|
||||
this.userRepository = userRepository;
|
||||
this.passwordEncoder = passwordEncoder;
|
||||
this.validatorUtil = validatorUtil;
|
||||
}
|
||||
|
||||
public GameClient findByLogin(String login) {
|
||||
return userRepository.findOneByLoginIgnoreCase(login);
|
||||
}
|
||||
|
||||
public GameClient createUser(String login, String password, String passwordConfirm) {
|
||||
if (findByLogin(login) != null) {
|
||||
throw new ValidationException(String.format("User '%s' already exists", login));
|
||||
}
|
||||
|
||||
final GameClient user = new GameClient(login, passwordEncoder.encode(password), 0);
|
||||
|
||||
validatorUtil.validate(user);
|
||||
|
||||
if (!Objects.equals(password, passwordConfirm)) {
|
||||
throw new ValidationException("Passwords not equals");
|
||||
}
|
||||
|
||||
return userRepository.save(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
final GameClient userEntity = findByLogin(username);
|
||||
|
||||
if (userEntity == null) {
|
||||
throw new UsernameNotFoundException(username);
|
||||
}
|
||||
|
||||
return new org.springframework.security.core.userdetails.User(
|
||||
userEntity.getLogin(), userEntity.getEmail(), Collections.emptyList());
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user