Sunday, June 22, 2014

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 these 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 .


___________________________________
Inside 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 the require 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 and login with new password which you ser in properties 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.
:)

#thanks to vkbardia 

No comments:

Post a Comment