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.

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  
    33import edu.gjoko.schedlr.entity.Business;
    44import edu.gjoko.schedlr.entity.ServiceType;
     5import edu.gjoko.schedlr.entity.Stakeholder;
    56import edu.gjoko.schedlr.entity.StakeholderType;
    67import edu.gjoko.schedlr.repositories.BusinessRepository;
    78import edu.gjoko.schedlr.repositories.ServiceTypeRepository;
    89import lombok.AllArgsConstructor;
     10import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    911import org.springframework.stereotype.Service;
    1012
    1113import java.util.List;
    1214
    13 import static edu.gjoko.schedlr.entity.BusinessStatus.ACTIVE;
    1415import static edu.gjoko.schedlr.entity.BusinessStatus.NEW;
    1516
     
    2021    private final BusinessRepository businessRepository;
    2122    private final ServiceTypeRepository serviceTypeRepository;
     23    private BCryptPasswordEncoder bCryptPasswordEncoder;
    2224
    2325    public void saveBusiness(Business business) {
    2426        saveNewServiceTypes(business);
    2527        business.getOwner().setStakeholderType(StakeholderType.BUSINESS_OWNER);
     28        business.getOwner().setPassword(bCryptPasswordEncoder.encode(business.getOwner().getPassword()));
    2629        business.setBusinessStatus(NEW);
    2730        businessRepository.save(business);
     
    5255    }
    5356
     57    public Business findByOwner(Long ownerId) {
     58        var owner = new Stakeholder();
     59        owner.setId(ownerId);
     60        return businessRepository.findBusinessByOwner(owner);
     61    }
    5462}
  • 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.