1 | package com.example.fooddeliverysystem.service.impl;
|
---|
2 |
|
---|
3 | import com.example.fooddeliverysystem.exceptions.UserNotFoundException;
|
---|
4 | import com.example.fooddeliverysystem.model.User;
|
---|
5 |
|
---|
6 | import com.example.fooddeliverysystem.model.enums.Role;
|
---|
7 | import com.example.fooddeliverysystem.model.userssecurity.UserPrincipal;
|
---|
8 | import com.example.fooddeliverysystem.repository.*;
|
---|
9 | import com.example.fooddeliverysystem.service.UserService;
|
---|
10 | import org.springframework.security.config.authentication.UserServiceBeanDefinitionParser;
|
---|
11 | import org.springframework.security.core.userdetails.UserDetails;
|
---|
12 | import org.springframework.security.core.userdetails.UserDetailsService;
|
---|
13 | import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
---|
14 | import org.springframework.stereotype.Service;
|
---|
15 |
|
---|
16 | import java.util.List;
|
---|
17 |
|
---|
18 | @Service
|
---|
19 | public class UserServiceImplementation implements UserService, UserDetailsService {
|
---|
20 | private final UserRepository userRepository;
|
---|
21 | private final AdminRepository adminRepository;
|
---|
22 | private final ConsumerRepository consumerRepository;
|
---|
23 | private final SalePlaceEmployeeRepository salePlaceEmployeeRepository;
|
---|
24 |
|
---|
25 | private final DeliverRepository deliverRepository;
|
---|
26 | public UserServiceImplementation(UserRepository userRepository, AdminRepository adminRepository, ConsumerRepository consumerRepository, SalePlaceEmployeeRepository salePlaceEmployeeRepository,
|
---|
27 | DeliverRepository deliverRepository) {
|
---|
28 | this.userRepository = userRepository;
|
---|
29 | this.adminRepository = adminRepository;
|
---|
30 | this.consumerRepository = consumerRepository;
|
---|
31 | this.salePlaceEmployeeRepository = salePlaceEmployeeRepository;
|
---|
32 | this.deliverRepository = deliverRepository;
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | public List<User> findAllUsers() {
|
---|
37 | return this.userRepository.findAll();
|
---|
38 | }
|
---|
39 |
|
---|
40 |
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
---|
44 | User user = this.userRepository.findByUsername(username).orElseThrow(() -> new UsernameNotFoundException("Username not found"));
|
---|
45 | Long id = user.getUser_id();
|
---|
46 | if(this.adminRepository.findById(id).isPresent())
|
---|
47 | user.setUserRole(Role.ADMIN);
|
---|
48 | else if(this.consumerRepository.findById(id).isPresent())
|
---|
49 | user.setUserRole(Role.CONSUMER);
|
---|
50 | else if(this.deliverRepository.findById(id).isPresent())
|
---|
51 | user.setUserRole(Role.DELIVER);
|
---|
52 | else
|
---|
53 | user.setUserRole(Role.SALEPLACEEMPLOYEE);
|
---|
54 | return new UserPrincipal(user);
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | }
|
---|