implement registration validity checks
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-08-09 04:56:54 +02:00
parent c0f655b0df
commit 17dcc0ac4f
9 changed files with 108 additions and 27 deletions

View File

@@ -1,5 +1,6 @@
package wtf.beatrice.releasehive.services;
import org.apache.coyote.BadRequestException;
import wtf.beatrice.releasehive.dtos.LoginUserDto;
import wtf.beatrice.releasehive.dtos.RegisterUserDto;
import wtf.beatrice.releasehive.models.User;
@@ -7,7 +8,7 @@ import wtf.beatrice.releasehive.models.User;
public interface AccountService
{
User register(RegisterUserDto user);
User register(RegisterUserDto user) throws BadRequestException;
User login(LoginUserDto user);
}

View File

@@ -1,12 +1,17 @@
package wtf.beatrice.releasehive.services;
import org.apache.coyote.BadRequestException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import wtf.beatrice.releasehive.config.InternalConfiguration;
import wtf.beatrice.releasehive.dtos.LoginUserDto;
import wtf.beatrice.releasehive.dtos.RegisterUserDto;
import wtf.beatrice.releasehive.exceptions.HiveException;
import wtf.beatrice.releasehive.models.User;
import wtf.beatrice.releasehive.repositories.UserRepository;
@@ -28,7 +33,37 @@ public class AccountServiceImpl implements AccountService {
}
@Override
public User register(RegisterUserDto userDto) {
public User register(RegisterUserDto userDto) throws BadRequestException {
if (userDto.getEmail() == null ||
userDto.getEmail().isEmpty() ||
userDto.getPassword() == null ||
userDto.getPassword().isEmpty() ||
userDto.getUsername() == null ||
userDto.getUsername().isEmpty()) {
throw new BadRequestException("Please provide a valid email, password, and username");
}
if(!userDto.getEmail().matches(InternalConfiguration.EMAIL_REGEX_RCF)) {
throw new BadRequestException("Invalid email format");
}
if(!userDto.getUsername().matches(InternalConfiguration.USERNAME_REGEX)) {
throw new BadRequestException("Username contains invalid characters");
}
if(!userDto.getPassword().matches(InternalConfiguration.PASSWORD_REGEX)) {
throw new BadRequestException("Invalid password format");
}
if (userRepository.findByEmail(userDto.getEmail()).isPresent()) {
throw new BadRequestException("An account already exists with this email");
}
if(userRepository.findByUsername(userDto.getUsername()).isPresent()) {
throw new BadRequestException("Username already in use");
}
User user = new User();
user.setUsername(userDto.getUsername());

View File

@@ -1,31 +1,15 @@
package wtf.beatrice.releasehive.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import wtf.beatrice.releasehive.models.User;
import wtf.beatrice.releasehive.repositories.UserRepository;
import java.util.List;
@Service
public class UserService
public interface UserService
{
private final UserRepository userRepository;
List<User> getAllUsers();
public UserService(@Autowired UserRepository userRepository) {
this.userRepository = userRepository;
}
User loadUserByUsername(String username) throws UsernameNotFoundException;
public List<User> getAllUsers() {
return userRepository.findAll();
}
public User loadUserByUsername(String username) throws UsernameNotFoundException {
return userRepository.findByUsername(username).orElseThrow(() -> new UsernameNotFoundException(username));
}
public User loadUserByEmail(String email) throws UsernameNotFoundException {
return userRepository.findByEmail(email).orElseThrow(() -> new UsernameNotFoundException(email));
}
User loadUserByEmail(String email) throws UsernameNotFoundException;
}

View File

@@ -0,0 +1,34 @@
package wtf.beatrice.releasehive.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import wtf.beatrice.releasehive.models.User;
import wtf.beatrice.releasehive.repositories.UserRepository;
import java.util.List;
@Service
public class UserServiceImpl implements UserService
{
private final UserRepository userRepository;
public UserServiceImpl(@Autowired UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public List<User> getAllUsers() {
return userRepository.findAll();
}
@Override
public User loadUserByUsername(String username) throws UsernameNotFoundException {
return userRepository.findByUsername(username).orElseThrow(() -> new UsernameNotFoundException(username));
}
@Override
public User loadUserByEmail(String email) throws UsernameNotFoundException {
return userRepository.findByEmail(email).orElseThrow(() -> new UsernameNotFoundException(email));
}
}