掲示板

How to customize Action in liferay 7 GA3.?

thumbnail
7年前 に deep shah によって更新されました。

How to customize Action in liferay 7 GA3.?

New Member 投稿: 9 参加年月日: 16/06/07 最新の投稿
Hello Experts,

I am working with liferay customizations. I have to customize UpdatePasswordAction.

I have to override/customize following actions.. so how can i customize.?

UpdatePasswordAction looks like..

package com.liferay.portal.action;

import com.liferay.portal.kernel.exception.NoSuchUserException;
import com.liferay.portal.kernel.exception.UserLockoutException;
import com.liferay.portal.kernel.exception.UserPasswordException;
import com.liferay.portal.kernel.model.Company;
import com.liferay.portal.kernel.model.CompanyConstants;
import com.liferay.portal.kernel.model.Ticket;
import com.liferay.portal.kernel.model.TicketConstants;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.security.auth.AuthTokenUtil;
import com.liferay.portal.kernel.security.auth.PrincipalException;
import com.liferay.portal.kernel.security.auth.session.AuthenticatedSessionManagerUtil;
import com.liferay.portal.kernel.service.CompanyLocalServiceUtil;
import com.liferay.portal.kernel.service.TicketLocalServiceUtil;
import com.liferay.portal.kernel.service.UserLocalServiceUtil;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PortalUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.security.pwd.PwdToolkitUtilThreadLocal;
import com.liferay.portal.util.PropsValues;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
* @author Brian Wing Shun Chan
* @author Mika Koivisto
*/
public class UpdatePasswordAction extends Action {

@Override
public ActionForward execute(
ActionMapping actionMapping, ActionForm actionForm,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
WebKeys.THEME_DISPLAY);

Ticket ticket = getTicket(request);

request.setAttribute(WebKeys.TICKET, ticket);

String cmd = ParamUtil.getString(request, Constants.CMD);

if (Validator.isNull(cmd)) {
if (ticket != null) {
User user = UserLocalServiceUtil.getUser(ticket.getClassPK());

try {
UserLocalServiceUtil.checkLockout(user);

UserLocalServiceUtil.updatePasswordReset(
user.getUserId(), true);
}
catch (UserLockoutException ule) {
SessionErrors.add(request, ule.getClass(), ule);
}
}

return actionMapping.findForward("portal.update_password");
}

try {
updatePassword(request, response, themeDisplay, ticket);

String redirect = ParamUtil.getString(request, WebKeys.REFERER);

if (Validator.isNotNull(redirect)) {
redirect = PortalUtil.escapeRedirect(redirect);
}

if (Validator.isNull(redirect)) {
redirect = themeDisplay.getPathMain();
}

response.sendRedirect(redirect);

return null;
}
catch (Exception e) {
if (e instanceof UserPasswordException) {
SessionErrors.add(request, e.getClass(), e);

return actionMapping.findForward("portal.update_password");
}
else if (e instanceof NoSuchUserException ||
e instanceof PrincipalException) {

SessionErrors.add(request, e.getClass());

return actionMapping.findForward("portal.error");
}

PortalUtil.sendError(e, request, response);

return null;
}
}

protected Ticket getTicket(HttpServletRequest request) {
String ticketKey = ParamUtil.getString(request, "ticketKey");

if (Validator.isNull(ticketKey)) {
return null;
}

try {
Ticket ticket = TicketLocalServiceUtil.fetchTicket(ticketKey);

if ((ticket == null) ||
(ticket.getType() != TicketConstants.TYPE_PASSWORD)) {

return null;
}

if (!ticket.isExpired()) {
return ticket;
}

TicketLocalServiceUtil.deleteTicket(ticket);
}
catch (Exception e) {
}

return null;
}

protected boolean isValidatePassword(HttpServletRequest request) {
HttpSession session = request.getSession();

Boolean setupWizardPasswordUpdated = (Boolean)session.getAttribute(
WebKeys.SETUP_WIZARD_PASSWORD_UPDATED);

if ((setupWizardPasswordUpdated != null) &&
setupWizardPasswordUpdated) {

return false;
}

return true;
}

protected void updatePassword(
HttpServletRequest request, HttpServletResponse response,
ThemeDisplay themeDisplay, Ticket ticket)
throws Exception {

AuthTokenUtil.checkCSRFToken(
request, UpdatePasswordAction.class.getName());

long userId = 0;

if (ticket != null) {
userId = ticket.getClassPK();
}
else {
userId = themeDisplay.getUserId();
}

String password1 = request.getParameter("password1");
String password2 = request.getParameter("password2");
boolean passwordReset = false;

boolean previousValidate = PwdToolkitUtilThreadLocal.isValidate();

try {
boolean currentValidate = isValidatePassword(request);

PwdToolkitUtilThreadLocal.setValidate(currentValidate);

UserLocalServiceUtil.updatePassword(
userId, password1, password2, passwordReset);
}
finally {
PwdToolkitUtilThreadLocal.setValidate(previousValidate);
}

if (ticket != null) {
TicketLocalServiceUtil.deleteTicket(ticket);

User user = UserLocalServiceUtil.getUser(userId);

Company company = CompanyLocalServiceUtil.getCompanyById(
user.getCompanyId());

String login = null;

String authType = company.getAuthType();

if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
login = user.getEmailAddress();
}
else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
login = user.getScreenName();
}
else if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {
login = String.valueOf(userId);
}

AuthenticatedSessionManagerUtil.login(
request, response, login, password1, false, null);

UserLocalServiceUtil.updatePasswordReset(userId, false);
}
else if (PropsValues.SESSION_STORE_PASSWORD) {
HttpSession session = request.getSession();

session.setAttribute(WebKeys.USER_PASSWORD, password1);
}
}

}


I have to add some more login in above Action class so How can i do this.?

Thanks ,
Deep
7年前 に Ravi Shah によって更新されました。

RE: How to customize Action in liferay 7 GA3.?

Junior Member 投稿: 59 参加年月日: 14/01/02 最新の投稿
HI Deep,

UpdatePasswordAction is a struts action. If you check struts-config.xml in liferay's source code and search for com.liferay.portal.action.UpdatePasswordAction you will get to see complete entry for struts action:

<action path="/portal/update_password" type="com.liferay.portal.action.UpdatePasswordAction">
<forward name="portal.update_password" path="portal.update_password" />
</action>


So you need to override struts action.

In order to do that, you can create liferay module project, selecting activator in template. Once the component is created, You need to change the main class file that is generated. You need to add below code just above your class:

@Component(
immediate=true,
property={
"path={struts-action-path}"
},
service = StrutsAction.class
)


public class CustomUpdatePasswordAction extends BaseStrutsAction{}

Replace {struts-action-path} with your struts action path for update password action. So in your case it would be "/portal/update_password". After that it will allow to override liferay's default code. You can put your business logic in overridden methods.

Also remember to remove activator entry that is created because of activator template you have selected.