package eu.semberal.reminders.service;
import eu.semberal.reminders.entity.User;
import org.apache.commons.lang.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;
@Transactional
public class AdminServiceImpl implements AdminService {
@PersistenceContext private EntityManager entityManager;
private MailService mailService;
private EncoderService encoderService;
public void setMailService(MailService mailService) {
this.mailService = mailService;
}
public void setEncoderService(EncoderService encoderService) {
this.encoderService = encoderService;
}
private Logger log = LoggerFactory.getLogger(AdminServiceImpl.class);
@Override
public User registerNewUser(String email) throws EmailAlreadyTakenException {
if (email == null) throw new IllegalArgumentException("Email must not be null");
if (getUser(email) != null) throw new EmailAlreadyTakenException("There's an user with this email");
String randomPass = RandomStringUtils.randomAlphanumeric(new Random().nextInt(8) + 6);
String encodedPass = encoderService.encodeString(randomPass);
User u = new User();
u.setEmail(email);
u.setPassword(encodedPass);
u.setDateRegistered(new Date());
u.getRoles().add("user");
entityManager.persist(u);
mailService.sendEmailActivation(email, randomPass);
return u;
}
@Override
public User getUser(Long id) {
return entityManager.find(User.class, id);
}
@Override
public User getUser(String email) {
Query q = entityManager.createQuery("select u from User u where u.email=:email");
q.setParameter("email", email);
try {
return (User) q.getSingleResult();
} catch (NoResultException e) {
return null;
}
}
@Override
public User authenticateUser(String email, String password, boolean isLogin) throws InvalidCredentialsException {
if (email == null || password == null) throw new InvalidCredentialsException("Missing authentication data.");
String encodedPassword = encoderService.encodeString(password);
Query q = entityManager.createQuery("select u from User u where u.email=:email and u.password=:password");
q.setParameter("password", encodedPassword);
q.setParameter("email", email);
try {
User u = (User) q.getSingleResult();
if (isLogin) {
u.setLastLogin(new Date());
entityManager.merge(u);
}
return u;
} catch (NoResultException e) {
throw new InvalidCredentialsException("Login attempt unsuccessful");
}
}
@Override
public void changePassword(User u, String newPassword) {
if(u==null || newPassword==null) throw new IllegalArgumentException("Argument must not be null");
String encodedPassword = encoderService.encodeString(newPassword);
u.setPassword(encodedPassword);
entityManager.merge(u);
}
@Override
public void resetPassword(String email, boolean sendEmailNotification) throws UserNotFoundException {
Long userId = null;
Query q = entityManager.createQuery("select id from User u where u.email=:email");
q.setParameter("email", email);
try {
userId=(Long)q.getSingleResult();
} catch (NoResultException e) {
throw new UserNotFoundException("User with this email has not been found");
}
User user = entityManager.find(User.class, userId);
String randomPass = RandomStringUtils.randomAlphanumeric(new Random().nextInt(8) + 6);
String encodedPass = encoderService.encodeString(randomPass);
user.setPassword(encodedPass);
if (sendEmailNotification) {
mailService.sentPasswordReset(email, randomPass);
}
}
@Override
@SuppressWarnings("unchecked")
public List<User> getAllNotLoggedInUsers() {
Query q = entityManager.createQuery("select u from User u where u.lastLogin is null");
return q.getResultList();
}
public void deleteUser(Long id) throws UserNotFoundException {
User u = entityManager.find(User.class, id);
if (u == null) throw new UserNotFoundException("User has not been found.");
entityManager.remove(u);
}
}