Sunday, 14 December 2014

Forgot Password implementation in Liferay


Hooking  login.jsp  And redirect to forgot password page which is hidden. I created forgot password portlet and added it to forgot password page .

And also removed Create Account link and forgot password link.
I create forgot password button which will redirect to forgot password page where we can enter email-Id  when we send it, it will fetch password from data base and send it to that email-Id .



<%
StringBundler forgotPasswordURL = null;
try{
       forgotPasswordURL = new StringBundler(7); //to create url for forget password page
     
               forgotPasswordURL.append(themeDisplay.getURLPortal()); //http://localhost:8080
forgotPasswordURL.append("/web/guest/forgot-password");
}
      catch(Exception e){
System.out.println("exception1");
}
%>
<a class="button" href="<%= forgotPasswordURL %>">Forgot Password</a>


Create a portlet  forgot password

view.jsp

<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@page import="javax.portlet.PortletURL" %>
<%@page import="javax.portlet.ActionRequest" %>
<portlet:defineObjects />

<%
 

  PortletURL forgotPasswordURL = renderResponse.createActionURL();
      forgotPasswordURL.setParameter(ActionRequest.ACTION_NAME,"fetchPassword");
    String userFound = (String)renderRequest.getAttribute("userFound");
   if(Validator.isNotNull(userFound)){
    if(userFound.equalsIgnoreCase("false")){
    %>
    <h3>Please signup, you are not register user.</h3>
    <%
    }
    else{
    %>
    <h3>Please check your mail, password sent to your mail.</h3>
    <%
    }
   }
   
    %>

<aui:form action="<%=forgotPasswordURL %>" method="post">

    <aui:input  name="emailId" id="emailId" lable="Email-Id" type="text" >
  <aui:validator name="required" />
  <aui:validator name="email"/>
    </aui:input>
 
    <aui:button type="submit" value="send" />
</aui:form>

Acion Class

ForgotPassword.java


package com.test;

import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import org.apache.log4j.Logger;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.slayer.model.BusinessUser;
import com.slayer.model.RegCustomer;
import com.slayer.service.BusinessUserLocalServiceUtil;
import com.slayer.service.RegCustomerLocalServiceUtil;

/**
 * Portlet implementation class forgotPassword
 */
public class forgotPassword extends MVCPortlet {
private static final Logger log = Logger.getLogger(forgotPassword.class);

public void fetchPassword(ActionRequest actionRequest, ActionResponse actionResponse)
throws IOException,PortletException
{
RegCustomer regCustomer = null;
BusinessUser businessUser = null;
String userFound = "false";
String emailId= ParamUtil.getString(actionRequest,"emailId");
//log.info("email ID  is "+emailId);
try{
 businessUser = BusinessUserLocalServiceUtil.getBusinessUser(emailId);
 //log.info("Bbuser: "+businessUser);
 userFound = "businessUser";
}
catch(Exception e){
 //log.error(e);
 try{
 regCustomer = RegCustomerLocalServiceUtil.getRegCustomer(emailId);
 //log.info("regCustomer: "+regCustomer);
 userFound = "regCustomer";
 }
 catch (Exception e1) {
 log.error(e1);
 }
}
//log.info("userFound: "+userFound);
if(userFound.equalsIgnoreCase("false")){
actionRequest.setAttribute("userFound", userFound);
}
else if(userFound.equalsIgnoreCase("businessUser")){
actionRequest.setAttribute("userFound", userFound);
log.info("B: pwd: "+businessUser.getPassword());
}
else if(userFound.equalsIgnoreCase("regCustomer")){
actionRequest.setAttribute("userFound", userFound);
log.info("U: pwd: "+regCustomer.getPassword());
}
}
}



I fetch data from two table BusinessUser and  RegCustomer




No comments: