From 990f1bd6d4bd3a8e585535388312aca43d04543d Mon Sep 17 00:00:00 2001 From: omenem Date: Thu, 27 Feb 2020 17:31:28 +0100 Subject: [PATCH] Initial Setup for email verification --- .../smarthut/Service/EmailSenderService.java | 23 ++++++ .../controller/UserAccountController.java | 68 ++++++++++++++++++ .../smarthut/models/ConfirmationToken.java | 71 +++++++++++++++++++ .../models/ConfirmationTokenRepository.java | 7 ++ .../smarthut/models/UserRepository.java | 2 + 5 files changed, 171 insertions(+) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/Service/EmailSenderService.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountController.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationToken.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationTokenRepository.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/Service/EmailSenderService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/Service/EmailSenderService.java new file mode 100644 index 0000000..35bc509 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/Service/EmailSenderService.java @@ -0,0 +1,23 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.Service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Service; + +@Service("emailSenderService") +public class EmailSenderService { + + private JavaMailSender javaMailSender; + + @Autowired + public EmailSenderService(JavaMailSender javaMailSender) { + this.javaMailSender = javaMailSender; + } + + @Async + public void sendEmail(SimpleMailMessage email) { + javaMailSender.send(email); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountController.java new file mode 100644 index 0000000..6b16039 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/UserAccountController.java @@ -0,0 +1,68 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.Service.EmailSenderService; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ConfirmationToken; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ConfirmationTokenRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.servlet.ModelAndView; + +@RestController +@EnableAutoConfiguration +@RequestMapping("/register") +public class UserAccountController { + + @Autowired private UserRepository userRepository; + + @Autowired private ConfirmationTokenRepository confirmationTokenRepository; + + @Autowired private EmailSenderService emailSenderService; + + @GetMapping + public ModelAndView displayRegistration(ModelAndView modelAndView, User user) { + modelAndView.addObject("user", user); + modelAndView.setViewName("register"); + return modelAndView; + } + + @PostMapping + public ModelAndView registerUser(ModelAndView modelAndView, User user) { + + User existingUser = userRepository.findByEmailIgnoreCase(user.getEmail()); + // Check if an User with the same email already exists + if (existingUser != null) { + modelAndView.addObject("message", "This email already exists!"); + modelAndView.setViewName("error"); + } else { + userRepository.save(user); + + ConfirmationToken confirmationToken = new ConfirmationToken(user); + + confirmationTokenRepository.save(confirmationToken); + + SimpleMailMessage mailMessage = new SimpleMailMessage(); + mailMessage.setTo(user.getEmail()); + mailMessage.setSubject("Complete Registration!"); + mailMessage.setFrom("smarthut.sm@gmail.com"); + mailMessage.setText( + "To confirm your account, please click here : " + + "http://localhost:8082/confirm-account?token=" + + confirmationToken.getConfirmationToken()); + + emailSenderService.sendEmail(mailMessage); + + modelAndView.addObject("emailId", user.getEmail()); + + modelAndView.setViewName("successfulRegisteration"); + } + + return modelAndView; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationToken.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationToken.java new file mode 100644 index 0000000..7f0b71c --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationToken.java @@ -0,0 +1,71 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import java.util.Date; +import java.util.UUID; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToOne; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; + +@Entity +public class ConfirmationToken { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "id", updatable = false, nullable = false) + private Long id; + + @Column(name = "confirmation_token") + private String confirmationToken; + + @Temporal(TemporalType.TIMESTAMP) + private Date createdDate; + + @OneToOne(targetEntity = User.class, fetch = FetchType.EAGER) + @JoinColumn(nullable = false, name = "user_id") + private User user; + + public ConfirmationToken(User user) { + this.user = user; + createdDate = new Date(); + confirmationToken = UUID.randomUUID().toString(); + } + + public Long getId() { + return id; + } + + public String getConfirmationToken() { + return confirmationToken; + } + + public Date getCreatedDate() { + return createdDate; + } + + public User getUser() { + return user; + } + + public void setId(Long id) { + this.id = id; + } + + public void setConfirmationToken(String confirmationToken) { + this.confirmationToken = confirmationToken; + } + + public void setCreatedDate(Date createdDate) { + this.createdDate = createdDate; + } + + public void setUser(User user) { + this.user = user; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationTokenRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationTokenRepository.java new file mode 100644 index 0000000..4bc18ce --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationTokenRepository.java @@ -0,0 +1,7 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import org.springframework.data.repository.CrudRepository; + +public interface ConfirmationTokenRepository extends CrudRepository { + ConfirmationToken findByConfirmationToken(String confirmationToken); +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/UserRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/UserRepository.java index 0b8c62a..01fd897 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/UserRepository.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/UserRepository.java @@ -5,4 +5,6 @@ import org.springframework.data.repository.CrudRepository; public interface UserRepository extends CrudRepository { User findByUsername(String username); + + User findByEmailIgnoreCase(String email); }