source: src/main/java/project/educatum/service/impl/AuthServiceImpl.java@ d3cf3a1

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

Initial commit

  • Property mode set to 100644
File size: 4.6 KB
Line 
1package project.educatum.service.impl;
2
3import org.springframework.security.authentication.BadCredentialsException;
4import org.springframework.security.core.authority.SimpleGrantedAuthority;
5import org.springframework.security.core.userdetails.UserDetails;
6import org.springframework.security.core.userdetails.UserDetailsService;
7import org.springframework.security.core.userdetails.UsernameNotFoundException;
8import org.springframework.security.crypto.password.PasswordEncoder;
9import org.springframework.stereotype.Service;
10import project.educatum.model.Admin;
11import project.educatum.model.Student;
12import project.educatum.model.Teacher;
13import project.educatum.model.exceptions.InvalidArgumentsException;
14import project.educatum.model.exceptions.UserNotEnabledException;
15import project.educatum.repository.AdminRepository;
16import project.educatum.repository.TeacherRepository;
17import project.educatum.repository.StudentRepository;
18import project.educatum.service.AuthService;
19
20import java.util.Objects;
21import java.util.stream.Collectors;
22import java.util.stream.Stream;
23
24@Service
25public 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}
Note: See TracBrowser for help on using the repository browser.