1 | package project.educatum.service.impl;
|
---|
2 |
|
---|
3 | import org.springframework.security.authentication.BadCredentialsException;
|
---|
4 | import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
---|
5 | import org.springframework.security.core.userdetails.UserDetails;
|
---|
6 | import org.springframework.security.core.userdetails.UserDetailsService;
|
---|
7 | import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
---|
8 | import org.springframework.security.crypto.password.PasswordEncoder;
|
---|
9 | import org.springframework.stereotype.Service;
|
---|
10 | import project.educatum.model.Admin;
|
---|
11 | import project.educatum.model.Student;
|
---|
12 | import project.educatum.model.Teacher;
|
---|
13 | import project.educatum.model.exceptions.InvalidArgumentsException;
|
---|
14 | import project.educatum.model.exceptions.UserNotEnabledException;
|
---|
15 | import project.educatum.repository.AdminRepository;
|
---|
16 | import project.educatum.repository.TeacherRepository;
|
---|
17 | import project.educatum.repository.StudentRepository;
|
---|
18 | import project.educatum.service.AuthService;
|
---|
19 |
|
---|
20 | import java.util.Objects;
|
---|
21 | import java.util.stream.Collectors;
|
---|
22 | import java.util.stream.Stream;
|
---|
23 |
|
---|
24 | @Service
|
---|
25 | public class AuthServiceImpl implements AuthService, UserDetailsService {
|
---|
26 |
|
---|
27 | private final TeacherRepository teachersRepository;
|
---|
28 | private final AdminRepository adminRepository;
|
---|
29 | private final StudentRepository studentsRepository;
|
---|
30 | private final PasswordEncoder passwordEncoder;
|
---|
31 |
|
---|
32 | public AuthServiceImpl(TeacherRepository teachersRepository, AdminRepository adminRepository, StudentRepository studentsRepository, PasswordEncoder passwordEncoder) {
|
---|
33 | this.teachersRepository = teachersRepository;
|
---|
34 | this.adminRepository = adminRepository;
|
---|
35 | this.studentsRepository = studentsRepository;
|
---|
36 | this.passwordEncoder = passwordEncoder;
|
---|
37 | }
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | public UserDetails loginTeacher(String email, String password) {
|
---|
41 | if (email == null || email.isEmpty() || password == null || password.isEmpty()) {
|
---|
42 | throw new InvalidArgumentsException();
|
---|
43 | }
|
---|
44 | Teacher n = teachersRepository.findByEmail(email);
|
---|
45 | if (n != null && n.getEnabled() != null && n.isEnabled()) {
|
---|
46 | if (!passwordEncoder.matches(password, n.getPassword())) {
|
---|
47 | throw new BadCredentialsException("Invalid credentials");
|
---|
48 | }
|
---|
49 | return loadUserByUsername(email);
|
---|
50 |
|
---|
51 | } else throw new UserNotEnabledException();
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public UserDetails loginStudent(String email, String password) {
|
---|
56 | if (email == null || email.isEmpty() || password == null || password.isEmpty()) {
|
---|
57 | throw new InvalidArgumentsException();
|
---|
58 | }
|
---|
59 | Student u = studentsRepository.findByEmail(email);
|
---|
60 | if (!passwordEncoder.matches(password, u.getPassword())) {
|
---|
61 | throw new BadCredentialsException("Passwords do not match!");
|
---|
62 | }
|
---|
63 | return loadUserByUsername(email);
|
---|
64 |
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | public UserDetails loginAdmin(String email, String password) {
|
---|
70 | if (email == null || email.isEmpty() || password == null || password.isEmpty())
|
---|
71 | throw new InvalidArgumentsException();
|
---|
72 |
|
---|
73 | UserDetails user = loadUserByUsername(email);
|
---|
74 |
|
---|
75 | if (!Objects.equals(user.getPassword(), passwordEncoder.encode(password)))
|
---|
76 | throw new BadCredentialsException("Incorrect password!");
|
---|
77 |
|
---|
78 | return user;
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
|
---|
84 | Admin userAdmin = this.adminRepository.findByEmail(email);
|
---|
85 | Teacher userTeacher = this.teachersRepository.findByEmail(email);
|
---|
86 | Student userStudent = this.studentsRepository.findByEmail(email);
|
---|
87 |
|
---|
88 | if (userAdmin != null) {
|
---|
89 | return new org.springframework.security.core.userdetails.User(
|
---|
90 | userAdmin.getEmail(),
|
---|
91 | userAdmin.getPassword(),
|
---|
92 | Stream.of(new SimpleGrantedAuthority("ROLE_ADMIN")).collect(Collectors.toList())
|
---|
93 | );
|
---|
94 | } else if (userTeacher != null) {
|
---|
95 | return new org.springframework.security.core.userdetails.User(
|
---|
96 | userTeacher.getEmail(),
|
---|
97 | userTeacher.getPassword(),
|
---|
98 | Stream.of(new SimpleGrantedAuthority("ROLE_NASTAVNIK")).collect(Collectors.toList())
|
---|
99 | );
|
---|
100 | } else if (userStudent != null) {
|
---|
101 | return new org.springframework.security.core.userdetails.User(
|
---|
102 | userStudent.getEmail(),
|
---|
103 | userStudent.getPassword(),
|
---|
104 | Stream.of(new SimpleGrantedAuthority("ROLE_UCENIK")).collect(Collectors.toList())
|
---|
105 | );
|
---|
106 | } else {
|
---|
107 | throw new UsernameNotFoundException(email);
|
---|
108 | }
|
---|
109 | }
|
---|
110 | } |
---|