Featured post

Docker setup for Liferay 7 with MySQL

Showing posts with label Hook. Show all posts
Showing posts with label Hook. Show all posts

Tuesday, 6 November 2012

Reset or Recover Admin / User's password by Hook

Sometimes we go through such kind of problem that we lost all the details of Admin user/User or other users and unable to access portal.

One thing we can to rid out of this problem is to change the User_ table directly
 i.e. change it manually.
For That you can follow this link .

But in production we don't have that much access on server side, so what else we can do?
Thanks to Samuel Liu and Lisa Simpson who make this way so easy.

You can create a hook to reset user's password by specifying some properties like user's name, screenName, virtualHost or webId depends on which liferay version you are using.

Here is the link from where you can download/checkout this Hooks as per your liferay version.

If it's not matching your liferay version then create your own hook for this.

1) Create a startup hook.
2) Use PasswordUpdater.java in application.startup.events .


package hu.bzz.liferay;

import java.util.Date;
import java.util.Properties;

import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.events.SimpleAction;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.model.Company;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.service.persistence.CompanyUtil;

public class PasswordUpdater extends SimpleAction {

public void run(String[] arg0) throws ActionException {
Properties props = new Properties();
try {
String type = props.getProperty("type");

                        //If Liferay version is less than 6.1
String virtualHost = props.getProperty("virtualhost");
Company c = CompanyUtil.fetchByVirtualHost(virtualHost);

                        /* If Liferay version is  6.1

                        String webId = props.getProperty("webId");
Company c = CompanyUtil.fetchByWebId(webId);

User u = null;
String name = null;
if ("screenname".equals(type)) {
String screenName = props.getProperty("screenname");
u = UserLocalServiceUtil.getUserByScreenName(c.getCompanyId(), screenName);
name = screenName;
} else if ("e-mail".equals(type)) {
String emailAddress = props.getProperty("emailaddress");
u = UserLocalServiceUtil.getUserByEmailAddress(c.getCompanyId(), emailAddress);
name = emailAddress;
else {
_log.error("You should set type to screenname or e-mail if you want to use the password updater.");
String password = props.getProperty("password");
UserLocalServiceUtil.updatePasswordManually(u.getUserId(), password, false, true, new Date());
_log.info("Password for " + name + " was updated.");
} catch (Exception e) {

private static Log _log = LogFactoryUtil.getLog(PasswordUpdater.class);


3) create "password.changer.properties" in src and put required details.


# type should be e-mail or screenname

# This is the webId of the company (not of communities or organizations!), if you have 1 instance, it's webId most of the time

# This is the virtualhost of the company (not of communities or organizations!), if you have 1 instance, it's localhost most of the time

# Read only if the type is e-mail

# Read only if the type is screename


3) Deploy hook & login with new password which you set in property file.
4) After login, liferay will ask to set up a new password.
5) Undeploy the Hook, so it will not set the password again after server restart.

You are just done, Try & Enjoy the function.............:))

Friday, 7 September 2012

ckEditor does not let copy paste microsoft word formatting like tables, colouring inside table cells

Liferay is using ckeditor, but not responsible for any bug ckeditor has.

Let us see first that how this bug stop us to use copy paste function with formatting from MS Word.

1. Log to the system.
2. Go to control panel > Wiki.
3. Create wiki and add wiki page.
4. Open MicroSoft Word.
5. Create tables format them with colours or etc.
6. Click on this word icon

7. Copy and Paste this content from MS Word to ckeditor.

Expected result : It should copy paste all content with formatting.
Actual Result : It will format the content without any formatting.

Workaround :
1) Create a hook of tomcat-6.0.29\webapps\ROOT\html\js\editor\ckeditor\ckconfig.jsp
2) Append below lines in end of file -


3) Deploy hook & you are done.

You are just done, Try & Enjoy the function.............:))

Monday, 21 May 2012

Create Hook Inside a Portlet, Liferay

Hi Folks,

Yesterday i tried something new, it's easy but useful.

Sometimes we have a requirement where we need a portlet which depends on some extended functionality of Liferay. So in that case we have to create a external hook and provide two wars separately.

Their is an answer, we can create a Portlet which also contains a Hook.

Steps to Go -
1) Create a simple portlet.
2) Put a liferay-hook.xml in WEB-Inf in your portlet.
3) Map the entry, which functionality you want to extend, e.g. portal.properties,stratup-events.
4) Put the require file in place as you put in Hook.

Example : Create a Portlet which extends an startup events by Hook.
1) Create a Hello Portlet.
2) Put liferay-hook.xml in WEB-Inf.
3) Put this entry in liferay-hook.xml file -


4) Create portal.properties in src of Hello portlet.
5) Copy below line to portal.properties -


6) StartupAction is the class which extends SimpleAction and executes strartup events.

You are just done, Try & Enjoy the function.............:))

Tuesday, 14 February 2012

Add Hidden/Admin portlets in Add more Applications Menu (Liferay)

Most of the requirement from the client comes by saying that they want the same user portlet as available in Control Panel. This can be achieved by below details.

1) Create a hook name addUserToAddApplication.
2) Create structure WEB-INF/jsp/html/portlet/layout_configuration.
3) copy view_category.jsp from source html/portlet/layout_configuration.
4) Add below lines in your jsp on line no 53 just above the while loop -

//   Use your own Category.

     // 125 is portlet id of ENTERPRISE_ADMIN_USER PORTLET.

Portlet userportlet = PortletLocalServiceUtil.getPortletById(user.getCompanyId(), "125");

5) Deploy your hook.
6) Check your Add Application menu, you are free to add user portlet to your page.

Try & Enjoy the function....:))

Monday, 9 January 2012

Crop Image in Liferay for Images of Image Gallery or Profile Images

Cropping is the way to set/cut image as per your requirement. You can set height/width of the whichever area of the image, you want.
As we all knows that Liferay's Image Gallery does not provide function to crop an Image, so it's time to develop our own.

Steps to to Crop an Image in Liferay-
1) Copy these two functions into your JAVA file or you can simplify the path by creating an ext of com.liferay.portal.image.FileSystemHook.

private static File getFile(long imageId, String type) {
String path = buildPath(String.valueOf(imageId));
String _rootDir=PropsUtil.get("image.hook.file.system.root.dir");
return new File(
_rootDir + StringPool.SLASH + path + StringPool.SLASH +
imageId + StringPool.PERIOD + type);
private static String buildPath(String fileNameFragment) {
int fileNameFragmentLength = fileNameFragment.length();

if (fileNameFragmentLength <= 2) {
return StringPool.BLANK;

StringBundler sb = new StringBundler(
fileNameFragmentLength / 2 + fileNameFragmentLength);

for (int i = 0; i < fileNameFragmentLength; i += 2) {
if ((i + 2) < fileNameFragmentLength) {
sb.append(fileNameFragment.substring(i, i + 2));

return sb.toString();

2) You can use any jQuery plugin like tapmodo-Jcrop or Javascript to get co-ordinates of Image or preview Image before croping.

3) Then write below action as per your requirements, i keep it as simple as i can.

public static void cropImage(
ActionRequest request, ActionResponse response) throws PortalException, SystemException, IOException{
long imageId=ParamUtil.getLong(request,"imageId");
String imageType=ImageLocalServiceUtil.getImage(imageId).getType();

BufferedImage outImage=ImageIO.read(getFile(imageId, imageType));
x1,y1,x2,y2 are the co-ordinates, will be passed as parameters used to crop the image.
BufferedImage cropped=outImage.getSubimage(ParamUtil.getInteger(request, "x1"),ParamUtil.getInteger(request, "y1"),
ParamUtil.getInteger(request, "w"),ParamUtil.getInteger(request, "h"));

new FileOutputStream(getFile(imageId, imageType)));
ImageLocalServiceUtil.updateImage(imageId,getFile(imageId, imageType));

4) Try & Enjoy the function :)

Thursday, 3 November 2011

Create your own custom tld in Liferay at Portal Level.

If you are here that means you already know what is a TLD(Top Level Definition).
To include a TLD in Liferay at root level or you can say a portal level tld, you can follow the steps which will help you to create one on your own.

1. Create a hook name mytld.
2. Create mytld.jsp at /docroot/web-inf/jsps/html/taglib/ui/mytld/mytld.jsp.
3. Create an ext named mytld ext.
4. Move to web.xml by /docroot/web-inf/ext-web/docroot/web-inf/web.xml in ext.
5. Put below lines in web.xml.

<?xml version="1.0"?>

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">



6. Create a corresponding java class for ur taglib(if required) at /docroot/web-inf/ext-util-taglib/src/com/liferay/taglib/ui/MyTld.java in ext.
7. Write required code.
8. Move to /docroot/web-inf/ext-web/docroot/web-inf/tld/liferay-fc.tld.
9. Put below lines in liferay-fc.tld.

<?xml version="1.0" encoding="UTF-8"?>


10. Deploy your ext.
11.Deploy your hook.
12.Restart your server.
12.Your new tld is ready to use.

Please let me know, if there is any concern about this Blog.