FYPBackend / src / main / java / com / cardpaymentsystem / cardplatform / services / MyUserDetailsService.java
MyUserDetailsService.java
Raw
package com.cardpaymentsystem.cardplatform.services;

import com.cardpaymentsystem.cardplatform.exceptions.CardPlatformException;
import com.cardpaymentsystem.cardplatform.models.CustomerEntity;
import com.cardpaymentsystem.cardplatform.repository.BusinessEntityRepository;
import com.cardpaymentsystem.cardplatform.models.BusinessEntity;
import com.cardpaymentsystem.cardplatform.repository.CustomerEntityRepository;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Collections;
import java.util.Optional;

@Service
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    BusinessEntityRepository businessEntityRepository;

    @Autowired
    CustomerEntityRepository customerEntityRepository;

    private Collection<? extends GrantedAuthority> getAuthorities(String role) {
        return Collections.singletonList(new SimpleGrantedAuthority(role));
    }

    @SneakyThrows
    @Override
    public UserDetails loadUserByUsername(String usercombo) throws UsernameNotFoundException {

        // Char ':' is disabled in usernames
       String[] split = usercombo.split(":");
       String username = split[0];
       String usertype = split[1];

       if(usertype.equals("business")){
           Optional<BusinessEntity> user = businessEntityRepository.findByBusinessName(username);
           BusinessEntity business = user.orElseThrow( ()-> new UsernameNotFoundException("Business not found: " + username) );
           //return  user.map(MyBusinessDetails::new).get();
           String role = business.getRoles();
           return  new org.springframework.security.core.userdetails.User(business.getBusinessName(),
                   business.getPassword(), business.getEnabled(), true, true, true, getAuthorities(role));
       } else if (usertype.equals("customer")){
           Optional<CustomerEntity> user = customerEntityRepository.findByCustomerName(username);
           CustomerEntity customer = user.orElseThrow( ()-> new UsernameNotFoundException("Not found: " + username) );
           //return  user.map(MyCustomerDetails::new).get();
           String role = customer.getRoles();
           return  new org.springframework.security.core.userdetails.User(customer.getCustomerName(),
                   customer.getPassword(), customer.getEnabled(), true, true, true, getAuthorities(role));
       } else
           throw new CardPlatformException("No valid usertype appended to username: " + usercombo);
    }

}