[d3cf3a1] | 1 | package project.educatum.web;
|
---|
| 2 |
|
---|
| 3 |
|
---|
| 4 | import org.springframework.security.authentication.BadCredentialsException;
|
---|
| 5 | import org.springframework.security.core.userdetails.UserDetails;
|
---|
| 6 | import org.springframework.stereotype.Controller;
|
---|
| 7 | import org.springframework.ui.Model;
|
---|
| 8 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
| 9 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
| 10 | import org.springframework.web.bind.annotation.RequestMapping;
|
---|
| 11 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
| 12 | import project.educatum.model.Admin;
|
---|
| 13 | import project.educatum.model.Teacher;
|
---|
| 14 | import project.educatum.model.Student;
|
---|
| 15 | import project.educatum.model.exceptions.InvalidUserCredentialsException;
|
---|
| 16 | import project.educatum.model.exceptions.UserNotEnabledException;
|
---|
| 17 | import project.educatum.service.AdminService;
|
---|
| 18 | import project.educatum.service.AuthService;
|
---|
| 19 | import project.educatum.service.TeacherService;
|
---|
| 20 | import project.educatum.service.StudentService;
|
---|
| 21 |
|
---|
| 22 | import javax.servlet.http.HttpServletRequest;
|
---|
| 23 |
|
---|
| 24 | @Controller
|
---|
| 25 | @RequestMapping("/login")
|
---|
| 26 | public class LoginController {
|
---|
| 27 |
|
---|
| 28 | private final TeacherService teacherService;
|
---|
| 29 | private final StudentService studentService;
|
---|
| 30 | private final AdminService adminService;
|
---|
| 31 | private final AuthService authService;
|
---|
| 32 |
|
---|
| 33 | public LoginController(TeacherService teacherService, StudentService studentService, AdminService adminService, AuthService authService) {
|
---|
| 34 | this.teacherService = teacherService;
|
---|
| 35 | this.studentService = studentService;
|
---|
| 36 | this.adminService = adminService;
|
---|
| 37 | this.authService = authService;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | @GetMapping
|
---|
| 41 | public String getLoginPage() {
|
---|
| 42 | return "login";
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | @GetMapping("/forgotPassword")
|
---|
| 46 | public String getForgotPasswordPage(){
|
---|
| 47 | return "forgotPasswordForm.html";
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 |
|
---|
| 51 | @PostMapping
|
---|
| 52 | public String login(HttpServletRequest request, Model model, @RequestParam String username, @RequestParam String password) {
|
---|
| 53 | String email = username;
|
---|
| 54 |
|
---|
| 55 | for (Teacher n : teacherService.findAll()) {
|
---|
| 56 | if (n.getEmail().equals(email)) {
|
---|
| 57 | try {
|
---|
| 58 | UserDetails user = authService.loginTeacher(email, password);
|
---|
| 59 | request.getSession().setAttribute("user", user);
|
---|
| 60 | return "redirect:/teachers/allStudents";
|
---|
| 61 | } catch (BadCredentialsException | InvalidUserCredentialsException ex) {
|
---|
| 62 | model.addAttribute("haserror", true);
|
---|
| 63 | model.addAttribute("error", ex.getMessage());
|
---|
| 64 | return "login";
|
---|
| 65 | } catch (UserNotEnabledException ex) {
|
---|
| 66 | return "notEnabled";
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | for (Student u : studentService.findAll()) {
|
---|
| 72 | if (u.getEmail().equals(email)) {
|
---|
| 73 | try {
|
---|
| 74 | UserDetails user = authService.loginStudent(email, password);
|
---|
| 75 | request.getSession().setAttribute("user", user);
|
---|
| 76 | return "redirect:/students/listSubjectsTeachers?subjectID=1";
|
---|
| 77 | } catch (InvalidUserCredentialsException | BadCredentialsException ex) {
|
---|
| 78 | model.addAttribute("haserror", true);
|
---|
| 79 | model.addAttribute("error", ex.getMessage());
|
---|
| 80 | return "login";
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 |
|
---|
| 86 | for (Admin a : adminService.findAll()) {
|
---|
| 87 | if (a.getEmail().equals(email)) {
|
---|
| 88 | try {
|
---|
| 89 | UserDetails user = authService.loginAdmin(email, password);
|
---|
| 90 | request.getSession().setAttribute("user", user);
|
---|
| 91 | return "redirect:/admin/allTeachers";
|
---|
| 92 | } catch (InvalidUserCredentialsException | BadCredentialsException ex) {
|
---|
| 93 | model.addAttribute("haserror", true);
|
---|
| 94 | model.addAttribute("error", ex.getMessage());
|
---|
| 95 | return "login";
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | model.addAttribute("haserror", true);
|
---|
| 101 | model.addAttribute("error", "Bad credentials!");
|
---|
| 102 | return "login";
|
---|
| 103 | }
|
---|
| 104 | }
|
---|