Changeset 8bcd64c for src/main/java/edu/gjoko/schedlr/services
- Timestamp:
- 04/19/23 21:19:08 (19 months ago)
- Branches:
- master
- Children:
- 950fa0d
- Parents:
- 9050790
- Location:
- src/main/java/edu/gjoko/schedlr/services
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/edu/gjoko/schedlr/services/BusinessService.java
r9050790 r8bcd64c 3 3 import edu.gjoko.schedlr.entity.Business; 4 4 import edu.gjoko.schedlr.entity.ServiceType; 5 import edu.gjoko.schedlr.entity.Stakeholder; 5 6 import edu.gjoko.schedlr.entity.StakeholderType; 6 7 import edu.gjoko.schedlr.repositories.BusinessRepository; 7 8 import edu.gjoko.schedlr.repositories.ServiceTypeRepository; 8 9 import lombok.AllArgsConstructor; 10 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 9 11 import org.springframework.stereotype.Service; 10 12 11 13 import java.util.List; 12 14 13 import static edu.gjoko.schedlr.entity.BusinessStatus.ACTIVE;14 15 import static edu.gjoko.schedlr.entity.BusinessStatus.NEW; 15 16 … … 20 21 private final BusinessRepository businessRepository; 21 22 private final ServiceTypeRepository serviceTypeRepository; 23 private BCryptPasswordEncoder bCryptPasswordEncoder; 22 24 23 25 public void saveBusiness(Business business) { 24 26 saveNewServiceTypes(business); 25 27 business.getOwner().setStakeholderType(StakeholderType.BUSINESS_OWNER); 28 business.getOwner().setPassword(bCryptPasswordEncoder.encode(business.getOwner().getPassword())); 26 29 business.setBusinessStatus(NEW); 27 30 businessRepository.save(business); … … 52 55 } 53 56 57 public Business findByOwner(Long ownerId) { 58 var owner = new Stakeholder(); 59 owner.setId(ownerId); 60 return businessRepository.findBusinessByOwner(owner); 61 } 54 62 } -
src/main/java/edu/gjoko/schedlr/services/PostgresUserDetailsService.java
r9050790 r8bcd64c 1 1 package edu.gjoko.schedlr.services; 2 2 3 import edu.gjoko.schedlr.entity.Business; 4 import edu.gjoko.schedlr.entity.BusinessStatus; 3 5 import edu.gjoko.schedlr.entity.Stakeholder; 6 import edu.gjoko.schedlr.entity.StakeholderType; 7 import edu.gjoko.schedlr.repositories.BusinessRepository; 4 8 import edu.gjoko.schedlr.repositories.StakeholderRepository; 9 import lombok.RequiredArgsConstructor; 10 import org.springframework.security.access.AccessDeniedException; 5 11 import org.springframework.security.core.GrantedAuthority; 6 12 import org.springframework.security.core.authority.SimpleGrantedAuthority; … … 16 22 17 23 @Service 24 @RequiredArgsConstructor 18 25 public class PostgresUserDetailsService implements UserDetailsService { 19 26 … … 21 28 private final BCryptPasswordEncoder bCryptPasswordEncoder; 22 29 23 public PostgresUserDetailsService(StakeholderRepository stakeholderRepository, 24 BCryptPasswordEncoder bCryptPasswordEncoder) { 25 this.stakeholderRepository = stakeholderRepository; 26 this.bCryptPasswordEncoder = bCryptPasswordEncoder; 27 } 30 private final BusinessRepository businessRepository; 28 31 29 32 @Override … … 33 36 throw new UsernameNotFoundException("Non existing user"); 34 37 } 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 } 35 45 Set<GrantedAuthority> grantedAuthorities = new HashSet<>(); 36 46 grantedAuthorities.add(new SimpleGrantedAuthority(user.getStakeholderType().name())); 47 37 48 return new User(user.getUsername(), user.getPassword(), grantedAuthorities); 38 49 } 50 51 public Long loadStakeholderId(String username) { 52 return stakeholderRepository.findStakeholderByUsername(username).getId(); 53 } 39 54 }
Note:
See TracChangeset
for help on using the changeset viewer.