com.liferay.my.account.web.internal.portlet.action.UpdatePasswordMVCActionCommand.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.my.account.web.internal.portlet.action.UpdatePasswordMVCActionCommand.java

Source

/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.my.account.web.internal.portlet.action;

import com.liferay.my.account.web.internal.constants.MyAccountPortletKeys;
import com.liferay.portal.kernel.exception.NoSuchUserException;
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.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.Authenticator;
import com.liferay.portal.kernel.security.auth.PrincipalException;
import com.liferay.portal.kernel.service.UserLocalService;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Portal;
import com.liferay.portal.kernel.util.Validator;

import java.util.HashMap;
import java.util.Map;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
 * @author Pei-Jung Lan
 */
@Component(immediate = true, property = { "javax.portlet.name=" + MyAccountPortletKeys.MY_ACCOUNT,
        "mvc.command.name=/users_admin/update_password" }, service = MVCActionCommand.class)
public class UpdatePasswordMVCActionCommand extends BaseMVCActionCommand {

    protected void authenticateUser(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

        String currentPassword = actionRequest.getParameter("password0");
        String newPassword = actionRequest.getParameter("password1");

        User user = _portal.getSelectedUser(actionRequest);

        if (Validator.isNotNull(currentPassword)) {
            if (Validator.isNull(newPassword)) {
                throw new UserPasswordException.MustNotBeNull(user.getUserId());
            }

            Company company = _portal.getCompany(actionRequest);

            String authType = company.getAuthType();

            Map<String, String[]> headerMap = new HashMap<>();
            Map<String, String[]> parameterMap = new HashMap<>();
            Map<String, Object> resultsMap = new HashMap<>();

            int authResult = Authenticator.FAILURE;

            if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
                authResult = _userLocalService.authenticateByEmailAddress(company.getCompanyId(),
                        user.getEmailAddress(), currentPassword, headerMap, parameterMap, resultsMap);
            } else if (authType.equals(CompanyConstants.AUTH_TYPE_ID)) {
                authResult = _userLocalService.authenticateByUserId(company.getCompanyId(), user.getUserId(),
                        currentPassword, headerMap, parameterMap, resultsMap);
            } else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
                authResult = _userLocalService.authenticateByScreenName(company.getCompanyId(),
                        user.getScreenName(), currentPassword, headerMap, parameterMap, resultsMap);
            }

            if (authResult == Authenticator.FAILURE) {
                throw new UserPasswordException.MustMatchCurrentPassword(user.getUserId());
            }
        } else if (Validator.isNotNull(newPassword)) {
            throw new UserPasswordException.MustNotBeNull(user.getUserId());
        }
    }

    @Override
    protected void doProcessAction(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

        try {
            authenticateUser(actionRequest, actionResponse);
        } catch (Exception e) {
            if (e instanceof NoSuchUserException || e instanceof PrincipalException) {

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

                actionResponse.setRenderParameter("mvcPath", "/error.jsp");
            } else if (e instanceof UserPasswordException) {
                SessionErrors.add(actionRequest, e.getClass(), e);

                String redirect = _portal.escapeRedirect(ParamUtil.getString(actionRequest, "redirect"));

                if (Validator.isNotNull(redirect)) {
                    sendRedirect(actionRequest, actionResponse, redirect);
                }
            } else {
                throw e;
            }
        }

        _mvcActionCommand.processAction(actionRequest, actionResponse);
    }

    @Reference(target = "(component.name=com.liferay.users.admin.web.internal.portlet.action.UpdatePasswordMVCActionCommand)")
    private MVCActionCommand _mvcActionCommand;

    @Reference
    private Portal _portal;

    @Reference
    private UserLocalService _userLocalService;

}