1 | package it.finki.charitable.controller;
|
---|
2 |
|
---|
3 | import it.finki.charitable.entities.AppUser;
|
---|
4 | import it.finki.charitable.entities.UserRole;
|
---|
5 | import it.finki.charitable.security.ConfirmationToken;
|
---|
6 | import it.finki.charitable.security.PasswordEncoder;
|
---|
7 | import it.finki.charitable.services.ConfirmationTokenService;
|
---|
8 | import it.finki.charitable.services.EmailService;
|
---|
9 | import it.finki.charitable.services.UserService;
|
---|
10 | import org.apache.commons.validator.routines.EmailValidator;
|
---|
11 | import org.springframework.security.core.context.SecurityContextHolder;
|
---|
12 | import org.springframework.stereotype.Controller;
|
---|
13 | import org.springframework.ui.Model;
|
---|
14 | import org.springframework.web.bind.annotation.ModelAttribute;
|
---|
15 | import org.springframework.web.bind.annotation.RequestMapping;
|
---|
16 | import org.springframework.web.bind.annotation.RequestMethod;
|
---|
17 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
18 |
|
---|
19 | import java.security.Principal;
|
---|
20 | import java.util.UUID;
|
---|
21 |
|
---|
22 | @Controller
|
---|
23 | public class HomeController {
|
---|
24 |
|
---|
25 | private final UserService userService;
|
---|
26 | private final ConfirmationTokenService confirmationTokenService;
|
---|
27 | private final EmailService emailService;
|
---|
28 |
|
---|
29 | public HomeController(UserService userService, ConfirmationTokenService confirmationTokenService, EmailService emailService) {
|
---|
30 | this.userService = userService;
|
---|
31 | this.confirmationTokenService = confirmationTokenService;
|
---|
32 | this.emailService = emailService;
|
---|
33 | }
|
---|
34 |
|
---|
35 | @RequestMapping("/")
|
---|
36 | public String index() {
|
---|
37 | return "index";
|
---|
38 | }
|
---|
39 |
|
---|
40 | @RequestMapping("/login")
|
---|
41 | public String login(Principal principal) {
|
---|
42 | if(principal != null) {
|
---|
43 | return "redirect:/";
|
---|
44 | }
|
---|
45 |
|
---|
46 | return "login";
|
---|
47 | }
|
---|
48 |
|
---|
49 | @RequestMapping("/register")
|
---|
50 | public String register(Principal principal) {
|
---|
51 | if(principal != null) {
|
---|
52 | return "redirect:/";
|
---|
53 | }
|
---|
54 | return "register";
|
---|
55 | }
|
---|
56 |
|
---|
57 | @RequestMapping(value = "/register", method = RequestMethod.POST)
|
---|
58 | public String newUser(Model model, @RequestParam String firstName,
|
---|
59 | @RequestParam String lastName,
|
---|
60 | @RequestParam String email,
|
---|
61 | @RequestParam String password,
|
---|
62 | @RequestParam String confirmPassword
|
---|
63 | ) {
|
---|
64 |
|
---|
65 | boolean exists = userService.loadUserByUsername(email) != null;
|
---|
66 | if(exists) {
|
---|
67 | model.addAttribute("userExists", true);
|
---|
68 | return "register";
|
---|
69 | }
|
---|
70 |
|
---|
71 | boolean error = false;
|
---|
72 | if(!EmailValidator.getInstance().isValid(email)) {
|
---|
73 | model.addAttribute("emailError", true);
|
---|
74 | error = true;
|
---|
75 | }
|
---|
76 |
|
---|
77 | if(!password.equals(confirmPassword)) {
|
---|
78 | model.addAttribute("passwordError", true);
|
---|
79 | error = true;
|
---|
80 | }
|
---|
81 |
|
---|
82 | if(firstName.isEmpty() || lastName.isEmpty()) {
|
---|
83 | model.addAttribute("nameError", true);
|
---|
84 | error = true;
|
---|
85 | }
|
---|
86 |
|
---|
87 | if(error){
|
---|
88 | return "register";
|
---|
89 | }
|
---|
90 |
|
---|
91 | AppUser user = new AppUser();
|
---|
92 | user.setFirstName(firstName);
|
---|
93 | user.setLastName(lastName);
|
---|
94 | user.setEmail(email);
|
---|
95 | user.setPassword(PasswordEncoder.bCryptPasswordEncoder().encode(password));
|
---|
96 | user.setUserRole(UserRole.USER);
|
---|
97 | user.setEnabled(false);
|
---|
98 |
|
---|
99 | String token = UUID.randomUUID().toString();
|
---|
100 | ConfirmationToken confirmationToken = new ConfirmationToken(token, user);
|
---|
101 |
|
---|
102 | userService.saveUser(user);
|
---|
103 | confirmationTokenService.saveConfirmationToken(confirmationToken);
|
---|
104 |
|
---|
105 | emailService.sendValidationEmail(email, "CharitableMk account validation", token);
|
---|
106 |
|
---|
107 | model.addAttribute("success",true);
|
---|
108 |
|
---|
109 | return "register";
|
---|
110 | }
|
---|
111 |
|
---|
112 | @RequestMapping("/validate")
|
---|
113 | public String validate(Model model, @RequestParam String token) {
|
---|
114 | ConfirmationToken confirmationToken = confirmationTokenService.getConfirmationToken(token);
|
---|
115 | if(confirmationToken != null) {
|
---|
116 | AppUser user = confirmationToken.getUser();
|
---|
117 | user.setEnabled(true);
|
---|
118 | userService.saveUser(user);
|
---|
119 | model.addAttribute("successValidation", true);
|
---|
120 | return "login";
|
---|
121 | }
|
---|
122 |
|
---|
123 | model.addAttribute("error", true);
|
---|
124 | return "login";
|
---|
125 | }
|
---|
126 |
|
---|
127 | @ModelAttribute("user")
|
---|
128 | public AppUser addAttributes() {
|
---|
129 | if(SecurityContextHolder.getContext().getAuthentication().getPrincipal() != "anonymousUser") {
|
---|
130 | return (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
---|
131 | }
|
---|
132 | return null;
|
---|
133 | }
|
---|
134 | }
|
---|