source: src/main/java/com/example/fooddeliverysystem/service/impl/UserServiceImplementation.java

Last change on this file was 8d11f8c, checked in by jovanmanchev <jovanmanchev3003@…>, 20 months ago

code added, trial 2

  • Property mode set to 100644
File size: 2.5 KB
Line 
1package com.example.fooddeliverysystem.service.impl;
2
3import com.example.fooddeliverysystem.exceptions.UserNotFoundException;
4import com.example.fooddeliverysystem.model.User;
5
6import com.example.fooddeliverysystem.model.enums.Role;
7import com.example.fooddeliverysystem.model.userssecurity.UserPrincipal;
8import com.example.fooddeliverysystem.repository.*;
9import com.example.fooddeliverysystem.service.UserService;
10import org.springframework.security.config.authentication.UserServiceBeanDefinitionParser;
11import org.springframework.security.core.userdetails.UserDetails;
12import org.springframework.security.core.userdetails.UserDetailsService;
13import org.springframework.security.core.userdetails.UsernameNotFoundException;
14import org.springframework.stereotype.Service;
15
16import java.util.List;
17
18@Service
19public 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}
Note: See TracBrowser for help on using the repository browser.