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