diff --git a/src/main/java/ru/team58/profileservice/controller/UserController.java b/src/main/java/ru/team58/profileservice/controller/UserController.java index 2c18b1b..36738de 100644 --- a/src/main/java/ru/team58/profileservice/controller/UserController.java +++ b/src/main/java/ru/team58/profileservice/controller/UserController.java @@ -3,6 +3,7 @@ package ru.team58.profileservice.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import ru.team58.profileservice.controller.exceptions.BadRequestException; +import ru.team58.profileservice.controller.exceptions.IncorrectEmailException; import ru.team58.profileservice.controller.exceptions.UnauthorizedException; import ru.team58.profileservice.controller.exceptions.UserNotFoundException; import ru.team58.profileservice.controller.schemes.CreateUserRequest; @@ -13,6 +14,7 @@ import ru.team58.profileservice.mapper.UserMapper; import ru.team58.profileservice.service.UserDTO; import ru.team58.profileservice.service.UserService; import ru.team58.profileservice.service.exceptions.UserAlreadyExistsException; +import ru.team58.profileservice.utils.EmailValidator; import java.security.Principal; import java.util.UUID; @@ -73,6 +75,9 @@ public class UserController { public void createMe(@RequestBody CreateUserRequest request) { UserDTO user = UserMapper.INSTANCE.toUserDTO(request); + if (!EmailValidator.validateEmail(user.getEmail())) + throw new IncorrectEmailException(); + try { userService.createUser(user); } catch (UserAlreadyExistsException ex) { diff --git a/src/main/java/ru/team58/profileservice/controller/advice/UserControllerExceptionHandler.java b/src/main/java/ru/team58/profileservice/controller/advice/UserControllerExceptionHandler.java index bd0d990..a45a5ee 100644 --- a/src/main/java/ru/team58/profileservice/controller/advice/UserControllerExceptionHandler.java +++ b/src/main/java/ru/team58/profileservice/controller/advice/UserControllerExceptionHandler.java @@ -37,4 +37,10 @@ public class UserControllerExceptionHandler { ErrorDetails errorDetails = new ErrorDetails("Unauthorized", HttpStatus.UNAUTHORIZED.value()); return new ResponseEntity<>(errorDetails, HttpStatus.UNAUTHORIZED); } + + @ExceptionHandler(IncorrectEmailException.class) + public ResponseEntity handleIncorrectEmailException(IncorrectEmailException ex) { + ErrorDetails errorDetails = new ErrorDetails("Email is incorrect", HttpStatus.BAD_REQUEST.value()); + return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST); + } } diff --git a/src/main/java/ru/team58/profileservice/controller/exceptions/IncorrectEmailException.java b/src/main/java/ru/team58/profileservice/controller/exceptions/IncorrectEmailException.java new file mode 100644 index 0000000..7e35e40 --- /dev/null +++ b/src/main/java/ru/team58/profileservice/controller/exceptions/IncorrectEmailException.java @@ -0,0 +1,4 @@ +package ru.team58.profileservice.controller.exceptions; + +public class IncorrectEmailException extends BadRequestException { +} diff --git a/src/main/java/ru/team58/profileservice/utils/EmailValidator.java b/src/main/java/ru/team58/profileservice/utils/EmailValidator.java new file mode 100644 index 0000000..84e007b --- /dev/null +++ b/src/main/java/ru/team58/profileservice/utils/EmailValidator.java @@ -0,0 +1,13 @@ +package ru.team58.profileservice.utils; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class EmailValidator { + public static boolean validateEmail(String email) { + String regexPattern = "^(?=.{1,64}@)[A-Za-z0-9_-]+(\\.[A-Za-z0-9_-]+)*@" + + "[^-][A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$"; + + return Pattern.compile(regexPattern).matcher(email).matches(); + } +}