Ignore:
Timestamp:
04/19/23 21:19:08 (19 months ago)
Author:
Gjoko Kostadinov <gjoko.kostadinov@…>
Branches:
master
Children:
950fa0d
Parents:
9050790
Message:

Add admin functionality and business admin functionality.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/edu/gjoko/schedlr/services/PostgresUserDetailsService.java

    r9050790 r8bcd64c  
    11package edu.gjoko.schedlr.services;
    22
     3import edu.gjoko.schedlr.entity.Business;
     4import edu.gjoko.schedlr.entity.BusinessStatus;
    35import edu.gjoko.schedlr.entity.Stakeholder;
     6import edu.gjoko.schedlr.entity.StakeholderType;
     7import edu.gjoko.schedlr.repositories.BusinessRepository;
    48import edu.gjoko.schedlr.repositories.StakeholderRepository;
     9import lombok.RequiredArgsConstructor;
     10import org.springframework.security.access.AccessDeniedException;
    511import org.springframework.security.core.GrantedAuthority;
    612import org.springframework.security.core.authority.SimpleGrantedAuthority;
     
    1622
    1723@Service
     24@RequiredArgsConstructor
    1825public class PostgresUserDetailsService implements UserDetailsService {
    1926
     
    2128    private final BCryptPasswordEncoder bCryptPasswordEncoder;
    2229
    23     public PostgresUserDetailsService(StakeholderRepository stakeholderRepository,
    24                                       BCryptPasswordEncoder bCryptPasswordEncoder) {
    25         this.stakeholderRepository = stakeholderRepository;
    26         this.bCryptPasswordEncoder = bCryptPasswordEncoder;
    27     }
     30    private final BusinessRepository businessRepository;
    2831
    2932    @Override
     
    3336            throw new UsernameNotFoundException("Non existing user");
    3437        }
     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        }
    3545        Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
    3646        grantedAuthorities.add(new SimpleGrantedAuthority(user.getStakeholderType().name()));
     47
    3748        return new User(user.getUsername(), user.getPassword(), grantedAuthorities);
    3849    }
     50
     51    public Long loadStakeholderId(String username) {
     52        return stakeholderRepository.findStakeholderByUsername(username).getId();
     53    }
    3954}
Note: See TracChangeset for help on using the changeset viewer.