ru.mynewtons.starter.oauth2.service.impl.UserDetailsServiceImpl Maven / Gradle / Ivy
package ru.mynewtons.starter.oauth2.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import ru.mynewtons.starter.oauth2.domain.UserDetails;
import ru.mynewtons.starter.oauth2.exception.EntityNotFoundException;
import ru.mynewtons.starter.oauth2.exception.UserDetailsEmailNotFoundException;
import ru.mynewtons.starter.oauth2.exception.UserNotEnabledException;
import ru.mynewtons.starter.oauth2.properties.UsersMessagesProperties;
import ru.mynewtons.starter.oauth2.repository.UserDetailsRepository;
import ru.mynewtons.starter.oauth2.service.UserDetailsService;
import java.util.List;
import java.util.Set;
@Service
@ConditionalOnMissingBean(name = "userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private UserDetailsRepository userDetailsRepository;
@Autowired
private UsersMessagesProperties messagesProperties;
@Override
public UserDetails findByEmail(String email) {
return userDetailsRepository.findByEmail(email);
}
@Override
public UserDetails save(UserDetails userDetails) {
return userDetailsRepository.save(userDetails);
}
@Override
public UserDetails findById(String id) {
UserDetails one = userDetailsRepository.getOne(id);
if (one == null) {
throw new EntityNotFoundException(messagesProperties.getMessage("entity.not.found.by"), UserDetails.class, id);
}
return one;
}
@Override
public List findAllByIds(Set ids) {
return userDetailsRepository.findAllById(ids);
}
@Override
public UserDetails enableUser(String userId, String comment) {
UserDetails existingUser = findById(userId);
existingUser.setEnabled(true);
return save(existingUser);
}
@Override
public UserDetails disableUser(String id, String comment) {
UserDetails existingUser = findById(id);
existingUser.setEnabled(false);
return save(existingUser);
}
@Override
public Page findAll(Pageable pageable) {
return userDetailsRepository.findAll(pageable);
}
@Override
public UserDetails loadUserByUsername(final String email) {
UserDetails existingUser = findByEmail(email);
if (existingUser == null) {
throw new UserDetailsEmailNotFoundException(messagesProperties.getMessage("user.auth.data.not.correct"));
} else if (!existingUser.getEnabled()) {
throw new UserNotEnabledException(messagesProperties.getMessage("user.auth.data.not.correct"));
}
return existingUser;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy