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

Last change on this file since 881a233 was f0d5cb7, checked in by KostaFortumanov <kfortumanov@…>, 3 years ago

Added moderator class

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