How to verify your new users email addresses after sending a mail to their in-box containing a specific link which holds a key identifying the user to be verified.
The Approach :
- Checking if the URL contains parameter Generated Key to invoke the Verification method in the Managed Bean
- Rendering the verification page depending on passed parameter in URL
- Checking with database if any user have the validation generated key [1]
- Update user account status to not require any more validation
The Tool box :
- JSF 2.2 [User Interface Management]
- Spring 2.4.0.Release [Layers Management]
- Hibernate 4.0.1.Final [Database Management]
- Apache Tomcat 8.0 [Server Instance]
- We are assuming that you know how to setup a Spring/Hibernate/JSF project, otherwise you can check this link before starting.
- We are assuming that you already have a method sending to user a mail with a generated key in URL (linksample.xhtml?key=123&name=Alaa&lname=Chaibi) [2]
UserModel.java :
- package project.model;
- import java.io.Serializable;
- import java.util.Date;
- import javax.persistence.CascadeType;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.JoinTable;
- import javax.persistence.OneToOne;
- import javax.persistence.Table;
- import javax.persistence.JoinColumn;
- @Entity(name="UserModel")
- @Table(name="USER")
- public class UserModel implements Serializable {
- private static final long serialVersionUID = GENERATE A KEY;
- @Id
- @GeneratedValue(strategy=GenerationType.AUTO)
- int iduser;
- String password;
- int accountstatus;
- String nomprofile;
- String prenprofile;
- String mailprofile;
- String validation;
- public int getIduser() {return iduser;}
- public void setIduser(int iduser) {this.iduser = iduser;}
- public String getPassword() {return password;}
- public void setPassword(String password) {this.password = password;}
- public int getAccountstatus() {return accountstatus;}
- public void setAccountstatus(int accountstatus) {this.accountstatus = accountstatus;}
- public String getNomprofile() {return nomprofile;}
- public void setNomprofile(String nomprofile) {this.nomprofile = nomprofile;}
- public String getPrenprofile() {return prenprofile;}
- public void setPrenprofile(String prenprofile) {this.prenprofile = prenprofile;}
- public String getMailprofile() {return mailprofile;}
- public void setMailprofile(String mailprofile) {this.mailprofile = mailprofile;}
- public String getValidation() {return validation;}
- public void setValidation(String validation) {this.validation = validation;}
- }
UserServiceImpl.java :
- package project.service;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import project.dao.UserDAO;
- import project.model.UserModel;
- import java.util.List;
- @Service("UserService")
- @Transactional(readOnly = true)
- public class UserServiceImpl implements UserService {
- @Autowired
- UserDAO userDAO;
- public UserDAO getUserDAO() {return userDAO;}
- public void setUserDAO(UserDAO userDAO) {this.userDAO = userDAO;}
- public UserModel getVerification(String verification){
- return getUserDAO().getVerification(verification);
- }
- @Transactional(readOnly = false)
- public boolean updateVerification(int iduser){
- return getUserDAO().updateVerification(iduser);
- }
- }
UserService.java :
- package project.service;
- import java.util.List;
- import project.dao.UserDAO;
- import project.model.UserModel;
- public interface UserService {
- public UserDAO getUserDAO();
- public void setUserDAO(UserDAO userDAO);
- public UserModel getVerification(String verification);
- public boolean updateVerification(int iduser);
- }
- package project.dao;
- import java.util.List;
- import org.hibernate.Query;
- import org.hibernate.SessionFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Repository;
- import project.model.UserModel;
- @Repository
- public class UserDAOImpl implements UserDAO {
- public @interface ComponentScan {
- }
- @Autowired
- private SessionFactory sessionFactory;
- public SessionFactory getSessionFactory() {return sessionFactory;}
- public void setSessionFactory(SessionFactory sessionFactory) {this.sessionFactory = sessionFactory;}
- public UserModel getVerification(String verification) {
- UserModel userModel = new UserModel();
- Query query = getSessionFactory().getCurrentSession()
- .createQuery("from UserModel u where u.validation = :Verification");
- query.setParameter("Verification", verification);
- userModel = (UserModel) query.uniqueResult();
- return userModel ;
- }
- public boolean updateVerification(int iduser){
- int query = (int) getSessionFactory().getCurrentSession()
- .createSQLQuery("UPDATE user u Set u.AccountStatus = 1 WHERE u.iduser = :Iduser").setParameter("Iduser", iduser).executeUpdate();
- if (query == 0){return false;}
- else{return true;}
- }
- }
- package project.dao;
- import java.util.List;
- import project.model.UserModel;
- public interface UserDAO {
- public UserModel getVerification(String verification);
- public boolean updateVerification(int iduser);
- }
- package project.managedbean;
- import java.io.Serializable;
- import javax.faces.application.FacesMessage;
- import javax.faces.bean.ManagedBean;
- import javax.faces.bean.ManagedProperty;
- import javax.faces.bean.RequestScoped;
- import javax.faces.context.FacesContext;
- import project.model.UserModel;
- import project.service.UserService;
- @ManagedBean(name = "userMB")
- @RequestScoped
- public class UserManagedBean implements Serializable {
- private static final long serialVersionUID = GENERATE A KEY;
- @ManagedProperty(value = "#{UserService}")
- UserService userService;
- UserModel userObject;
- public UserService getUserService() {
- return userService;
- }
- public void setUserService (UserService userService) {
- this.userService= userService;
- }
- public void verify(String code) {
- FacesContext context = FacesContext.getCurrentInstance();
- if (getUsertoverify(code) != null) {
- // With this condition we put data (User row where code is found) into userObject
- if (userObject.getAccountstatus() == 0) {
- // Test if user status require verification with user id from received data userObject
- if (getUserService().updateVerification(userObject.getIduser()) == true) {
- context.addMessage("verify", new FacesMessage(FacesMessage.SEVERITY_INFO, "Success: ",
- "Verification completed successfully"));
- } else {
- context.addMessage("verify", new FacesMessage(FacesMessage.SEVERITY_WARN, "Error: ",
- "An error happened when updating this user account."));
- }
- } else {
- context.addMessage("verify", new FacesMessage(FacesMessage.SEVERITY_INFO, "Account Status : ",
- "Verification is not needed for this user"));
- }
- } else {
- context.addMessage("verify",
- new FacesMessage(FacesMessage.SEVERITY_WARN, "Wrong code : ", "Incorrect passed code"));
- }
- }
- public UserModel getUsertoverify(String code) {
- if (userObject== null) {
- userObject= new UseModel();
- userObject= getUsermanagerService().getVerification(code);
- }
- return userData;
- }
- }
- <html xmlns="http://www.w3.org/1999/xhtml"
- xmlns:h="http://java.sun.com/jsf/html"
- xmlns:f="http://java.sun.com/jsf/core"
- xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
- <h:head>
- <f:metadata>
- <f:viewAction action="#{userMB.verify(param['key'])}" if="#{not empty param['key']}"/>
- </f:metadata>
- </h:head>
- <h:body>
- <h:panelGroup rendered="#{empty param['key']}">
- Show a message, a page or anything when no "key" parameter.
- </h:panelGroup>
- <h:panelGroup rendered="#{not empty param['key']}">
- Verification for #{param['key']}
- <p:messages id="verify" for="verify" showDetail="true"
- closable="true" />
- </h:panelGroup>
- </h:body>
[1] : We can use only one query to update the user directly, but we made a test before in order to manage testing account status by selecting the user before updating it.
[2] : Consider making the generated key surely unique by adding exact server time with seconds and id of user. In a personal point of view, I would generate a base64 key of the entered mail adress and use it as a generated key after verifying that the entered mail is never used in user table.
Aucun commentaire:
Enregistrer un commentaire