/**
 * 
 */

package org.opencps.frontend.web.portal.portlet;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.opencps.frontend.web.portal.constants.FrontendWebPortalPortletKeys;
import org.opencps.usermgt.model.Applicant;
import org.opencps.usermgt.service.util.UserMgtUtils;
import org.osgi.service.component.annotations.Component;

import com.liferay.portal.kernel.model.CompanyConstants;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.portlet.bridges.mvc.BaseMVCActionCommand;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCActionCommand;
import com.liferay.portal.kernel.security.auth.session.AuthenticatedSessionManagerUtil;
import com.liferay.portal.kernel.service.UserLocalServiceUtil;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PortalUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.kernel.workflow.WorkflowConstants;

/**
 * @author phucnv
 * @date Sep 12, 2017
 */
@Component(property = {
	"javax.portlet.name=" + FrontendWebPortalPortletKeys.LOGIN_PORTLET_NAME,
	"mvc.command.name=/login/login"
}, service = MVCActionCommand.class)
public class LoginMVCActionCommand extends BaseMVCActionCommand {

	/*
	 * (non-Javadoc)
	 * @see com.liferay.portal.kernel.portlet.bridges.mvc.BaseMVCActionCommand#
	 * doProcessAction(javax.portlet.ActionRequest,
	 * javax.portlet.ActionResponse)
	 */
	@Override
	protected void doProcessAction(
		ActionRequest actionRequest, ActionResponse actionResponse)
		throws Exception {

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

		HttpServletRequest request = PortalUtil.getOriginalServletRequest(
			PortalUtil.getHttpServletRequest(actionRequest));

		HttpServletResponse response =
			PortalUtil.getHttpServletResponse(actionResponse);

		String login = ParamUtil.getString(actionRequest, "login");
		String password = ParamUtil.getString(actionRequest, "password");
		String action = ParamUtil.getString(actionRequest, "action");
		boolean rememberMe = ParamUtil.getBoolean(actionRequest, "rememberMe");
		String authType = CompanyConstants.AUTH_TYPE_EA;
		
		Applicant applicant = UserMgtUtils.getApplicant(login);

		login = applicant != null ? applicant.getContactEmail() : login;

		User user = UserLocalServiceUtil.getUserByEmailAddress(
			themeDisplay.getCompanyId(), login);

		hideDefaultSuccessMessage(actionRequest);

		if (user != null &&
			user.getStatus() == WorkflowConstants.STATUS_PENDING) {

			actionResponse.sendRedirect(
				"/confirm-account?active_user_id=" + user.getUserId() +
					"&redirectURL=" + themeDisplay.getURLCurrent());
		}
		else {
			AuthenticatedSessionManagerUtil.login(
				request, response, login, password, rememberMe, authType);

			if (action != null && action.equals("confirm_account")) {
				actionResponse.sendRedirect(
					themeDisplay.getPortalURL() + "/profile");
			}
			else {
				actionResponse.sendRedirect(themeDisplay.getPathMain());
			}
		}

	}

}

@@ -346,16 +346,78 @@
 licant;%0A
+import org.opencps.usermgt.service.ApplicantLocalServiceUtil;%0A
 import o
@@ -1067,24 +1067,73 @@
 PortalUtil;%0A
+import com.liferay.portal.kernel.util.Validator;%0A
 import com.l
@@ -2460,16 +2460,214 @@
 _EA;%0A%09%09%0A
+%09%09if (!Validator.isEmailAddress(login)) %7B%0A%09%09%09%0A%09%09%09Applicant app = ApplicantLocalServiceUtil.fetchByAppId(login);%0A%09%09%09%0A%09%09%09if (Validator.isNotNull(app)) %7B%0A%09%09%09%09login = app.getContactEmail();%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A
 %09%09Applic

Add login by applicantNoId