source: src/main/java/it/finki/charitable/controller/HomeController.java@ 5577566

Last change on this file since 5577566 was 5577566, checked in by NikolaCenevski <cenevskinikola@…>, 3 years ago

prototip part 2

  • Property mode set to 100644
File size: 4.4 KB
Line 
1package it.finki.charitable.controller;
2
3import it.finki.charitable.entities.AppUser;
4import it.finki.charitable.entities.UserRole;
5import it.finki.charitable.security.ConfirmationToken;
6import it.finki.charitable.security.PasswordEncoder;
7import it.finki.charitable.services.ConfirmationTokenService;
8import it.finki.charitable.services.EmailService;
9import it.finki.charitable.services.UserService;
10import org.apache.commons.validator.routines.EmailValidator;
11import org.springframework.security.core.context.SecurityContextHolder;
12import org.springframework.stereotype.Controller;
13import org.springframework.ui.Model;
14import org.springframework.web.bind.annotation.ModelAttribute;
15import org.springframework.web.bind.annotation.RequestMapping;
16import org.springframework.web.bind.annotation.RequestMethod;
17import org.springframework.web.bind.annotation.RequestParam;
18
19import java.security.Principal;
20import java.util.UUID;
21
22@Controller
23public 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}
Note: See TracBrowser for help on using the repository browser.