Sunday, January 31, 2016

Change Liferay User Account Password Example 1

In this example we will see how to change/update liferay user account password.

STEPS:
Step 1: Add below entry in portal-ext.properties , path inside “liferay-portal-6.2.0-ce-ga1” or
liferay-portal-6.2.0-ce-ga1\tomcat-7.0.42\webapps\ROOT\WEB-INF\classes
session.shared.attributes.excludes = USER_PASSWORD
session.store.password=true

Step2: Create one liferay mvc project name: Passwordchanger  of portlet plugin type.  Now create one portlet  inside that project .In this example portlet class/controller name: ChangePassword.java
Step 3: Add below entry in liferay-portlet.xml  of created portlet after <icon></icon> tag
<private-session-attributes>false</private-session-attributes>

Step 4: Add below code in portlet view.jsp, path: docroot/html/changepassword/view.jsp
<%@ include file="init.jsp" %>

<portlet:defineObjects />


<%
PortletURL  updateUserPasswordURL = renderResponse.createActionURL();
updateUserPasswordURL.setParameter(actionRequest.ACTION_NAME, "changingMyPassword");
%>

<aui:form method="post" action="<%=updateUserPasswordURL%>">
  <liferay-ui:error exception="<%= UserPasswordException.class %>" message="that-password-has-already-been-used-please-enter-in-a-different-password" />
  <liferay-ui:error key="error-key" message="Current Password is invalid."></liferay-ui:error>

       <aui:fieldset cssClass="lfr-portrait-editor">
              <aui:input autoFocus="<%= true %>" type="password" label="Current Password" value="" name="currentPassword" autocomplete="off">
                       <aui:validator name="required" />
              </aui:input>
              <aui:input label="New Password" name="newPassword" size="30" type="password" value="">
                       <aui:validator name="required" />
              </aui:input>
              <aui:input label="Enter Again" name="confirmPassword" size="30" type="password" value="">
                       <aui:validator name="required" />
                            <aui:validator name="equalTo">
                                  '#<portlet:namespace />newPassword'
                            </aui:validator>
              </aui:input>
              <aui:button-row><aui:button type="submit"/></aui:button-row>
       </aui:fieldset>
</aui:form

Step 5: Create one new jsp name:init.jsp  page inside docroot/html/changepassword/init.jsp
And add below code.
<%@page import="com.liferay.portal.UserPasswordException"%>
<%@page import="javax.portlet.PortletURL"%>
<%@page import="com.liferay.portal.kernel.util.WebKeys"%>
<%@page import="com.liferay.portal.theme.ThemeDisplay"%>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui"%>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@page import="com.liferay.portal.service.UserLocalServiceUtil"%>
<%@page import="com.liferay.portal.model.User"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
Step 6: Add below code in portlet controller, in this example controller name: ChangePassword.java
package com.test;

import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import com.liferay.util.bridges.mvc.MVCPortlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.liferay.portal.UserPasswordException;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.EncryptorException;

/**
 * Portlet implementation class ChangePassword
 */
public class ChangePassword extends MVCPortlet {

       public void changingMyPassword(ActionRequest actionRequest,ActionResponse actionResponse) throws PortalException, SystemException, EncryptorException, IOException, PortletException
       {
                     try {
                                  ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
                                  HttpServletRequest httprequest = PortalUtil.getHttpServletRequest(actionRequest);
                                  HttpSession httpsession = httprequest.getSession();
                                 
                                  //Get the user's current password from session
                                  String userAccountdatabasePassword = (String)httpsession.getAttribute(WebKeys.USER_PASSWORD);
                                  String userInputcurrentPassword = (String) actionRequest.getParameter("currentPassword").trim();
                          
                                  String newPass = (String) actionRequest.getParameter("newPassword").trim();
                                  String confirmPass = (String) actionRequest.getParameter("confirmPassword").trim();
                                 
                                  if (userAccountdatabasePassword.equals(userInputcurrentPassword) && newPass.equals(confirmPass)) {
                                                UserLocalServiceUtil.updatePassword(themeDisplay.getUserId(), newPass,confirmPass, false);
                                                //Set the entered new password into session after changing the password
                                                httpsession.setAttribute(WebKeys.USER_PASSWORD, newPass);
                                                SessionMessages.add(actionRequest, "request_processed", "User Account Password updated successfully.");
                                                actionResponse.setRenderParameter("jspPage", "/html/changepassword/view.jsp");
                                  } else {
                                          SessionErrors.add(actionRequest, "error-key", "Sorry, Something wrong.");
                                          actionResponse.setRenderParameter("jspPage", "/html/changepassword/view.jsp");
                                  }
                     } catch (UserPasswordException e) {
                                     SessionErrors.add(actionRequest, UserPasswordException.class.getName(), e);
                                     actionResponse.setRenderParameter("jspPage", "/html/changepassword/view.jsp");
                     }
       }
}

After deploying portlet.

OUTPUT:

Login using your account credential,
Example :

Password: test

Liferay Cache Using MultiVMPoolUtil

Liferay Version: Liferay 6.2

In this post we will see how to use liferay cache.
In this example we will put values in cache inside render method and will get that cache value in jsp.
We can put cache in other places also.

STEPS:
Step1: Create one liferay mvc project name: Caching of portlet plugin type.  Now create one portlet  inside that project .In this example portlet class/controller name: Cache.java
Step 2: Add below code in portlet view.jsp, path: docroot/html/caching/view.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@page import="com.liferay.portal.model.User"%>
<%@page import="java.util.List"%>
<%@page import="com.liferay.portal.kernel.cache.MultiVMPoolUtil"%>
<portlet:defineObjects />

<%
String name = (String)MultiVMPoolUtil.getCache("my-Cache").get("Name");
List<User> users =(List<User>)MultiVMPoolUtil.getCache("myList-Cache").get("listValue");
%>

Name : <%=name%>
<%
if(Validator.isNull(users)){
       out.println("cache is cleared...");
} else{
       for(User userInfo: users){
              %>
              <p style="color:green;"><%=userInfo.getEmailAddress()%></p><br/>
        <%
        }
}
%>


Step 3: Add below code in portlet controller, in this example controller name: Cache.java
package com.test;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class Cache
 */
public class Cache extends MVCPortlet {

        @Override
              public void render(RenderRequest request, RenderResponse response)
                           throws PortletException, IOException {
                    
                  String name = "Azharuddin";
                     List<User>  userList = null;
                      try {
                            userList = UserLocalServiceUtil.getUsers(-1, UserLocalServiceUtil.getUsersCount());
                     } catch (SystemException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                     }
                      MultiVMPoolUtil.getCache("myList-Cache").put("listValue", (Serializable)userList);
                      MultiVMPoolUtil.getCache("my-Cache").put("Name",name);
                      // TODO Auto-generated method stub
                     super.render(request, response);
              }
      


}


After deploying portlet.


OUTPUT:

Saturday, December 26, 2015

Sign in using Google with Liferay

Below is the step to login into Liferay Portal using google. It will create a user with your gmail user name in your liferay portal and let you login and navigate to dashboard page without login with login portlet of Liferay.

First Setup your web application for Google+ OAuth2 API.
Step 1 : Go to Google Developers Console and login with your google credentials. At top right corner you will see create project option as shown in screen below. Create a project by clicking on the Create project link.
Image 1

 Step 2 : A popup will appear as shown below : 
Image 2
give your project name, Your project id will auto populate for you but if you want you can edit it by clicking on edit link given. Click on create button to create the project.
Next, you will be redirected to your Project’s DashBoard page as shown below.
Image 3
in the above image in the highlighted area click on the box to enable Google Apis that you need it.
It will take you to apis liberary page as shown below : 
Image 4


.
In the above image i have point with arrow to create credential in  APIS & AUTH-> Credentials.
When you click on OAth Client Id option from select it will take you to below screen :
Image 5
You must be seeing a warning message to set a product name on the consent screen. For this we have to go to OAuth consent screen. As shown below :
Image 6
Here just give your product name only as others fields are optional so leave as it is and click on save button it will take you to Image 4 so When you click on OAth Client Id option from select it will take you to below screen :
Image 7
Fill the detail as shown above and click on create button. a popup will come to you as shown below:
Image 8
Finally, CLIENT ID and CLIENT SECRET is created.

Any time you can edit this setting by clicking on your created credential as shown below : 
Image 9
Now we have done with client id and client secret key creation.

Step 2: Next you have to purchase Google Login App from Market place and install in your application. Here is the Link.
once you install Google Login App you can see Google link as shown below :
Image 10
Step 3: Now you we have to provide our client id and client secret key to our portal.
for that go to Control Panel > Portal Setting and in the right panel click on Authentication Link.
You will see screen as below :
Image 11


click on google tab and there provide your client id and client secret key.
That's All we have done all the step to integrate. Now you sign out and try clicking on google link in your login portlet to sign in its will create new user with your gmail user name and let you login.

Note : you can change your domain name in host file as 
1.27.0.0.1 yourdomainname.com


then restart your system.