103 lines
3.7 KiB
Java
103 lines
3.7 KiB
Java
package com.webproglabs.lab1.services;
|
|
|
|
import com.webproglabs.lab1.dao.UserRepository;
|
|
import com.webproglabs.lab1.models.User;
|
|
import com.webproglabs.lab1.models.UserRole;
|
|
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.util.StringUtils;
|
|
|
|
import javax.persistence.EntityNotFoundException;
|
|
import javax.transaction.Transactional;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
import java.util.Optional;
|
|
|
|
@Service
|
|
public class UserService implements UserDetailsService {
|
|
private final UserRepository userRepository;
|
|
private final PasswordEncoder passwordEncoder;
|
|
|
|
public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder) {
|
|
this.userRepository = userRepository;
|
|
this.passwordEncoder = passwordEncoder;
|
|
}
|
|
|
|
@Transactional
|
|
public User findUserById(Long id) {
|
|
final Optional<User> user = userRepository.findById(id);
|
|
return user.orElseThrow(EntityNotFoundException::new);
|
|
}
|
|
|
|
@Transactional
|
|
public User findUserByLogin(String login) {
|
|
final User user = userRepository.findOneByLoginIgnoreCase(login).orElse(null);
|
|
return user;
|
|
}
|
|
|
|
@Transactional
|
|
public List<User> findAllUsers() {
|
|
return userRepository.findAll();
|
|
}
|
|
|
|
@Transactional
|
|
public User createUser(String login, String password, String passwordConfirm, UserRole role) {
|
|
if (findUserByLogin(login) != null) {
|
|
throw new IllegalArgumentException("User " + login + " already exists");
|
|
}
|
|
final User user = new User(login, passwordEncoder.encode(password), role);
|
|
if (!Objects.equals(password, passwordConfirm)) {
|
|
throw new IllegalArgumentException("Passwords not equals");
|
|
}
|
|
return userRepository.save(user);
|
|
}
|
|
|
|
@Transactional
|
|
public User createUser(String login, String password, String passwordConfirm) {
|
|
return createUser(login, password, passwordConfirm, UserRole.USER);
|
|
}
|
|
|
|
@Transactional
|
|
public User updateUser(Long id, String login, String password, String passwordConfirm) {
|
|
if (!StringUtils.hasText(login) || !StringUtils.hasText(password)) {
|
|
throw new IllegalArgumentException("User data is null or empty");
|
|
}
|
|
|
|
final User currentUser = findUserById(id);
|
|
|
|
if (Objects.equals(password, currentUser.getPassword())) {
|
|
throw new IllegalArgumentException("New password is the same as old");
|
|
}
|
|
|
|
if (!Objects.equals(password, passwordConfirm)) {
|
|
throw new IllegalArgumentException("Password mismatch");
|
|
}
|
|
|
|
currentUser.setLogin(login);
|
|
currentUser.setPassword(passwordEncoder.encode(password));
|
|
return userRepository.save(currentUser);
|
|
}
|
|
|
|
@Transactional
|
|
public User deleteUser(Long id) {
|
|
final User currentUser = findUserById(id);
|
|
userRepository.delete(currentUser);
|
|
return currentUser;
|
|
}
|
|
|
|
|
|
@Override
|
|
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
|
final User userEntity = findUserByLogin(username);
|
|
if (userEntity == null) {
|
|
throw new UsernameNotFoundException(username);
|
|
}
|
|
return new org.springframework.security.core.userdetails.User(
|
|
userEntity.getLogin(), userEntity.getPassword(), Collections.singleton(userEntity.getRole()));
|
|
}
|
|
}
|