package com.nevernote.service.jpa;
import java.util.List;
import org.jasypt.util.password.BasicPasswordEncryptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.google.common.collect.Lists;
import com.nevernote.domain.Users;
import com.nevernote.repository.UsersRepository;
import com.nevernote.service.UsersService;
@Service("usersService")
@Repository
@Transactional
public class UsersServiceImpl implements UsersService {
final static Logger logger = LoggerFactory.getLogger(UsersServiceImpl.class);
@Autowired
private UsersRepository usersRepository;
@Transactional(readOnly = true)
@Override
public List<Users> findAll() {
logger.info("---------- Finding all contacts ----------");
return Lists.newArrayList(usersRepository.findAll());
}
@Transactional(readOnly = true)
@Override
public Users findById(String id) {
logger.info("---------- Finding contact with ID: {} ----------", id);
return usersRepository.findOne(id);
}
@Override
public Users save(String id, Users user) {
return usersRepository.save(user);
}
@Override
public Users loginCheck(String id, String pass) {
Users validate = usersRepository.findOne(id);
BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor();
if ((validate != null && validate.getEnabled()) && passwordEncryptor.checkPassword(pass, validate.getPassword())){
return validate;
} else{
return null;
}
}
@Override
public boolean changePassword(String id, String password) {
Users userUpdate;
BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor();
userUpdate = usersRepository.findOne(id);
if (userUpdate == null || !userUpdate.getEnabled()){
return false;
} else {
userUpdate.setPassword(passwordEncryptor.encryptPassword(password));
save(userUpdate.getId(), userUpdate);
}
return true;
}
}