1 | package project.educatum.web;
|
---|
2 |
|
---|
3 |
|
---|
4 | import org.springframework.security.core.userdetails.UserDetails;
|
---|
5 | import org.springframework.stereotype.Controller;
|
---|
6 | import org.springframework.ui.Model;
|
---|
7 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
8 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
9 | import org.springframework.web.bind.annotation.RequestMapping;
|
---|
10 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
11 | import project.educatum.model.exceptions.InvalidArgumentsException;
|
---|
12 | import project.educatum.model.exceptions.PasswordsDoNotMatchException;
|
---|
13 | import project.educatum.model.exceptions.UserNotEnabledException;
|
---|
14 | import project.educatum.model.exceptions.UsernameAlreadyExistsException;
|
---|
15 | import project.educatum.service.*;
|
---|
16 |
|
---|
17 | import javax.servlet.http.HttpServletRequest;
|
---|
18 |
|
---|
19 | @Controller
|
---|
20 | @RequestMapping("/register")
|
---|
21 | public class RegisterController {
|
---|
22 |
|
---|
23 | private final TeacherService teacherService;
|
---|
24 | private final StudentService studentService;
|
---|
25 | private final AdminService adminService;
|
---|
26 | private final SubjectService subjectService;
|
---|
27 | private final AuthService authService;
|
---|
28 |
|
---|
29 | public RegisterController(TeacherService teacherService, StudentService studentService, AdminService adminService, SubjectService subjectService, AuthService authService) {
|
---|
30 | this.teacherService = teacherService;
|
---|
31 | this.studentService = studentService;
|
---|
32 | this.adminService = adminService;
|
---|
33 | this.subjectService = subjectService;
|
---|
34 | this.authService = authService;
|
---|
35 | }
|
---|
36 |
|
---|
37 |
|
---|
38 | @GetMapping
|
---|
39 | public String getRegisterPage(@RequestParam(required = false) String error, Model model) {
|
---|
40 | if (error != null && !error.isEmpty()) {
|
---|
41 | model.addAttribute("hasErrors", true);
|
---|
42 | model.addAttribute("errors", error);
|
---|
43 |
|
---|
44 | }
|
---|
45 | return "/register";
|
---|
46 | }
|
---|
47 |
|
---|
48 | @PostMapping("/registrirajSe")
|
---|
49 | public String register(@RequestParam String ime,
|
---|
50 | @RequestParam String prezime,
|
---|
51 | @RequestParam String email,
|
---|
52 | @RequestParam String password,
|
---|
53 | @RequestParam String repeatPassword,
|
---|
54 | @RequestParam String role,
|
---|
55 | @RequestParam String telBroj,
|
---|
56 | @RequestParam(required = false) String opis,
|
---|
57 | HttpServletRequest request) {
|
---|
58 |
|
---|
59 |
|
---|
60 | if (role.equals("ROLE_NASTAVNIK")) {
|
---|
61 | try {
|
---|
62 | this.teacherService.register(ime, prezime, email, password, repeatPassword, telBroj, opis);
|
---|
63 | UserDetails user = authService.loginTeacher(email, password);
|
---|
64 | request.getSession().setAttribute("user", user);
|
---|
65 | return "redirect:/home/chooseSubject";
|
---|
66 |
|
---|
67 | } catch (PasswordsDoNotMatchException | InvalidArgumentsException | UsernameAlreadyExistsException exception) {
|
---|
68 | return "redirect:/register?error=" + exception.getMessage();
|
---|
69 |
|
---|
70 | } catch (UserNotEnabledException e) {
|
---|
71 | request.getSession().setAttribute("user", teacherService.findByEmail(email));
|
---|
72 | return "redirect:/home/chooseSubject";
|
---|
73 | }
|
---|
74 | } else if (role.equals("ROLE_UCENIK")) {
|
---|
75 | try {
|
---|
76 | this.studentService.register(ime, prezime, email, password, repeatPassword, telBroj, opis);
|
---|
77 | UserDetails user = authService.loginStudent(email, password);
|
---|
78 | request.getSession().setAttribute("user", user);
|
---|
79 | return "redirect:/home/zainteresiran";
|
---|
80 |
|
---|
81 | } catch (PasswordsDoNotMatchException | InvalidArgumentsException exception) {
|
---|
82 | return "redirect:/register?error=" + exception.getMessage();
|
---|
83 |
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | return "redirect:/login";
|
---|
88 | }
|
---|
89 |
|
---|
90 | }
|
---|
91 |
|
---|