Featured post

Docker setup for Liferay 7 with MySQL

Showing posts with label Startup Hook. Show all posts
Showing posts with label Startup 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 .



PasswordUpdater.java


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 {

@Override
public void run(String[] arg0) throws ActionException {
Properties props = new Properties();
try {
props.load(this.getClass().getClassLoader().getResourceAsStream("password.changer.properties"));
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) {
_log.error(e);
}
}

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

}


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


password.changer.properties


# type should be e-mail or screenname
type=e-mail

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

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

# Read only if the type is e-mail
emailaddress=test@liferay.com

# Read only if the type is screename
screenname=test

password=changeIt



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.............:))

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 -


<hook>
<portal-properties>portal.properties</portal-properties>
</hook>

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

application.startup.events=com.gomedia.startup.events.StartupAction

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

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