[cf9cdbf] | 1 | package edu.gjoko.schedlr.services;
|
---|
| 2 |
|
---|
[8bcd64c] | 3 | import edu.gjoko.schedlr.entity.Business;
|
---|
| 4 | import edu.gjoko.schedlr.entity.BusinessStatus;
|
---|
[cf9cdbf] | 5 | import edu.gjoko.schedlr.entity.Stakeholder;
|
---|
[8bcd64c] | 6 | import edu.gjoko.schedlr.entity.StakeholderType;
|
---|
| 7 | import edu.gjoko.schedlr.repositories.BusinessRepository;
|
---|
[cf9cdbf] | 8 | import edu.gjoko.schedlr.repositories.StakeholderRepository;
|
---|
[8bcd64c] | 9 | import lombok.RequiredArgsConstructor;
|
---|
| 10 | import org.springframework.security.access.AccessDeniedException;
|
---|
[cf9cdbf] | 11 | import org.springframework.security.core.GrantedAuthority;
|
---|
| 12 | import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
---|
| 13 | import org.springframework.security.core.userdetails.User;
|
---|
| 14 | import org.springframework.security.core.userdetails.UserDetails;
|
---|
| 15 | import org.springframework.security.core.userdetails.UserDetailsService;
|
---|
| 16 | import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
---|
[763289e] | 17 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
---|
[cf9cdbf] | 18 | import org.springframework.stereotype.Service;
|
---|
| 19 |
|
---|
| 20 | import java.util.HashSet;
|
---|
| 21 | import java.util.Set;
|
---|
| 22 |
|
---|
| 23 | @Service
|
---|
[8bcd64c] | 24 | @RequiredArgsConstructor
|
---|
[cf9cdbf] | 25 | public class PostgresUserDetailsService implements UserDetailsService {
|
---|
| 26 |
|
---|
| 27 | private final StakeholderRepository stakeholderRepository;
|
---|
[763289e] | 28 | private final BCryptPasswordEncoder bCryptPasswordEncoder;
|
---|
[cf9cdbf] | 29 |
|
---|
[8bcd64c] | 30 | private final BusinessRepository businessRepository;
|
---|
[cf9cdbf] | 31 |
|
---|
| 32 | @Override
|
---|
| 33 | public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
---|
| 34 | Stakeholder user = stakeholderRepository.findStakeholderByUsername(username);
|
---|
| 35 | if(user == null) {
|
---|
| 36 | throw new UsernameNotFoundException("Non existing user");
|
---|
| 37 | }
|
---|
[8bcd64c] | 38 |
|
---|
| 39 | if(user.getStakeholderType() == StakeholderType.BUSINESS_OWNER) {
|
---|
| 40 | Business business = businessRepository.findBusinessByOwner(user);
|
---|
| 41 | if (business.getBusinessStatus() != BusinessStatus.ACTIVE) {
|
---|
| 42 | throw new SecurityException("User not approved by admin");
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
[cf9cdbf] | 45 | Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
|
---|
| 46 | grantedAuthorities.add(new SimpleGrantedAuthority(user.getStakeholderType().name()));
|
---|
[8bcd64c] | 47 |
|
---|
[cf9cdbf] | 48 | return new User(user.getUsername(), user.getPassword(), grantedAuthorities);
|
---|
| 49 | }
|
---|
[8bcd64c] | 50 |
|
---|
| 51 | public Long loadStakeholderId(String username) {
|
---|
| 52 | return stakeholderRepository.findStakeholderByUsername(username).getId();
|
---|
| 53 | }
|
---|
[cf9cdbf] | 54 | }
|
---|