source: src/main/java/project/educatum/web/LoginController.java

Last change on this file was d3cf3a1, checked in by Marija Micevska <marija_micevska@…>, 2 years ago

Initial commit

  • Property mode set to 100644
File size: 4.0 KB
Line 
1package project.educatum.web;
2
3
4import org.springframework.security.authentication.BadCredentialsException;
5import org.springframework.security.core.userdetails.UserDetails;
6import org.springframework.stereotype.Controller;
7import org.springframework.ui.Model;
8import org.springframework.web.bind.annotation.GetMapping;
9import org.springframework.web.bind.annotation.PostMapping;
10import org.springframework.web.bind.annotation.RequestMapping;
11import org.springframework.web.bind.annotation.RequestParam;
12import project.educatum.model.Admin;
13import project.educatum.model.Teacher;
14import project.educatum.model.Student;
15import project.educatum.model.exceptions.InvalidUserCredentialsException;
16import project.educatum.model.exceptions.UserNotEnabledException;
17import project.educatum.service.AdminService;
18import project.educatum.service.AuthService;
19import project.educatum.service.TeacherService;
20import project.educatum.service.StudentService;
21
22import javax.servlet.http.HttpServletRequest;
23
24@Controller
25@RequestMapping("/login")
26public 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}
Note: See TracBrowser for help on using the repository browser.