source: src/main/java/edu/gjoko/schedlr/services/PostgresUserDetailsService.java

Last change on this file was 77205be, checked in by gjoko kostadinov <gjokokostadinov@…>, 11 months ago

Add entire code

  • Property mode set to 100755
File size: 2.1 KB
Line 
1package edu.gjoko.schedlr.services;
2
3import edu.gjoko.schedlr.entity.Business;
4import edu.gjoko.schedlr.entity.BusinessStatus;
5import edu.gjoko.schedlr.entity.Stakeholder;
6import edu.gjoko.schedlr.entity.StakeholderType;
7import edu.gjoko.schedlr.repositories.BusinessRepository;
8import edu.gjoko.schedlr.repositories.StakeholderRepository;
9import lombok.RequiredArgsConstructor;
10import org.springframework.security.core.GrantedAuthority;
11import org.springframework.security.core.authority.SimpleGrantedAuthority;
12import org.springframework.security.core.userdetails.User;
13import org.springframework.security.core.userdetails.UserDetails;
14import org.springframework.security.core.userdetails.UserDetailsService;
15import org.springframework.security.core.userdetails.UsernameNotFoundException;
16import org.springframework.stereotype.Service;
17
18import java.util.HashSet;
19import java.util.Set;
20
21@Service
22@RequiredArgsConstructor
23public class PostgresUserDetailsService implements UserDetailsService {
24
25 private final StakeholderRepository stakeholderRepository;
26 private final BusinessRepository businessRepository;
27
28 @Override
29 public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
30 Stakeholder user = stakeholderRepository.findStakeholderByUsername(username);
31 if (user == null) {
32 throw new UsernameNotFoundException("Non existing user");
33 }
34
35 if (user.getStakeholderType() == StakeholderType.BUSINESS_OWNER) {
36 Business business = businessRepository.findBusinessByOwner(user);
37 if (business.getBusinessStatus() != BusinessStatus.ACTIVE) {
38 throw new SecurityException("User not approved by admin");
39 }
40 }
41 Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
42 grantedAuthorities.add(new SimpleGrantedAuthority(user.getStakeholderType().name()));
43
44 return new User(user.getUsername(), user.getPassword(), grantedAuthorities);
45 }
46
47 public Long loadStakeholderId(String username) {
48 return stakeholderRepository.findStakeholderByUsername(username).getId();
49 }
50}
Note: See TracBrowser for help on using the repository browser.