|
1 | 1 | package com.bulat.jobboard.controller;
|
2 | 2 |
|
| 3 | +import com.bulat.jobboard.dto.CaptchaResponseDto; |
| 4 | +import com.bulat.jobboard.model.User; |
| 5 | +import com.bulat.jobboard.service.UserService; |
| 6 | +import com.bulat.jobboard.utils.Attributes; |
| 7 | +import com.bulat.jobboard.utils.UserValidator; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.beans.factory.annotation.Value; |
3 | 10 | import org.springframework.stereotype.Controller;
|
| 11 | +import org.springframework.ui.ModelMap; |
| 12 | +import org.springframework.validation.BindingResult; |
4 | 13 | import org.springframework.web.bind.annotation.GetMapping;
|
| 14 | +import org.springframework.web.bind.annotation.PostMapping; |
5 | 15 | import org.springframework.web.bind.annotation.RequestMapping;
|
| 16 | +import org.springframework.web.bind.annotation.RequestParam; |
| 17 | +import org.springframework.web.client.RestTemplate; |
| 18 | + |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.Objects; |
6 | 21 |
|
7 | 22 | @Controller
|
8 | 23 | @RequestMapping("/signUp")
|
9 | 24 | public class SignUpController {
|
| 25 | + private final static String CAPTCHA_URL = "https://www.google.com/recaptcha/api/siteverify?secret=%s&response=%s"; |
| 26 | + |
| 27 | + private final UserValidator userValidator; |
| 28 | + private final RestTemplate restTemplate; |
| 29 | + private final UserService userService; |
| 30 | + |
| 31 | + @Value("${recaptcha.secret}") |
| 32 | + private String secret; |
| 33 | + |
| 34 | + @Autowired |
| 35 | + public SignUpController(UserValidator userValidator, RestTemplate restTemplate, UserService userService) { |
| 36 | + this.userValidator = userValidator; |
| 37 | + this.restTemplate = restTemplate; |
| 38 | + this.userService = userService; |
| 39 | + } |
10 | 40 |
|
11 | 41 | @GetMapping
|
12 | 42 | public String getRegistration(){
|
13 | 43 | return "signUp";
|
14 | 44 | }
|
| 45 | + |
| 46 | + @PostMapping |
| 47 | + public String signUp(User user, BindingResult result, ModelMap model, |
| 48 | + @RequestParam("g-recaptcha-response") String captchaResponse){ |
| 49 | + userValidator.validate(user, result); |
| 50 | + StringBuilder error = errorChecking(captchaResponse, result); |
| 51 | + if (error.length() == 0){ |
| 52 | + Attributes.addSuccessAttributes(model, "Success!"); |
| 53 | + userService.signUp(user); |
| 54 | + }else{ |
| 55 | + Attributes.addErrorAttributes(model, String.valueOf(error)); |
| 56 | + } |
| 57 | + return "/signUp"; |
| 58 | + } |
| 59 | + |
| 60 | + private StringBuilder errorChecking(String captchaResponse, BindingResult result){ |
| 61 | + StringBuilder builder = new StringBuilder(); |
| 62 | + String url = String.format(CAPTCHA_URL, secret, captchaResponse); |
| 63 | + CaptchaResponseDto response = restTemplate.postForObject(url, Collections.emptyList(), CaptchaResponseDto.class); |
| 64 | + if (!Objects.requireNonNull(response).isSuccess()) { |
| 65 | + builder.append("Fill captcha! "); |
| 66 | + } |
| 67 | + if (result.hasErrors()) { |
| 68 | + builder.append("This mail is already taken! "); |
| 69 | + } |
| 70 | + return builder; |
| 71 | + } |
15 | 72 | }
|
0 commit comments