package gal.udc.fic.muei.tfm.dap.flipper.security; import gal.udc.fic.muei.tfm.dap.flipper.domain.User; import gal.udc.fic.muei.tfm.dap.flipper.repository.UserRepository; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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.UsernameNotFoundException; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import javax.inject.Inject; import java.util.ArrayList; import java.util.Collection; import java.util.Optional; import java.util.stream.Collectors; import java.util.Collections; import java.util.List; /** * Authenticate a user from the database. */ @Component("userDetailsService") public class UserDetailsService implements org.springframework.security.core.userdetails.UserDetailsService { private final Logger log = LoggerFactory.getLogger(UserDetailsService.class); @Inject private UserRepository userRepository; @Override @Transactional public UserDetails loadUserByUsername(final String login) { log.debug("Authenticating {}", login); String lowercaseLogin = login.toLowerCase(); Optional<User> userFromDatabase = userRepository.findOneByLogin(lowercaseLogin); return userFromDatabase.map(user -> { if (!user.getActivated()) { throw new UserNotActivatedException("User " + lowercaseLogin + " was not activated"); } List<GrantedAuthority> grantedAuthorities = user.getAuthorities().stream() .map(authority -> new SimpleGrantedAuthority(authority)) .collect(Collectors.toList()); return new org.springframework.security.core.userdetails.User(lowercaseLogin, user.getPassword(), grantedAuthorities); }).orElseThrow(() -> new UsernameNotFoundException("User " + lowercaseLogin + " was not found in the database")); } }