Liferay中登录失败后重定向

时间:2014-02-10 11:05:50

标签: redirect login liferay hook

当用户被禁用/停用时,我需要重定向尝试登录Liferay网站的用户。

自从我搜索此问题的解决方案以来,已经过了几个小时,但我找不到任何解决方案。

我无法使用登录预操作挂钩,因为如果停用/禁用用户,则不会调用它。

我尝试使用login.jsp中的一个钩子但是它没有工作(即使我的测试表明它正在通过 response.sendRedirect 传递):

if(login!=null && !login.isEmpty()) {
    User u = UserLocalServiceUtil.fetchUserByEmailAddress(company.getCompanyId(), login);
    if(u!=null && !u.isActive()) {
        response.sendRedirect(LANDING_PAGE_DEACTIVATED);
    }
}

感谢任何帮助。

谢谢,皮埃尔

2 个答案:

答案 0 :(得分:1)

您尝试使用事件处理的“内核”并扩展servlet.service.events.pre

请参阅Liferay: How to configure Liferay Portal

答案 1 :(得分:1)

我通过使用ServicePostAction实现了我的目标:

servlet.service.events.post=fr.mycompany.loginpostaction.hook.ServicePostAction

使用此代码:

package fr.mycompany.loginpostaction.hook;

import com.liferay.portal.kernel.events.Action;
import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.CookieKeys;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.Company;
import com.liferay.portal.model.CompanyConstants;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.CookieUtil;

import fr.mycompany.loginpostaction.hook.utils.Constants;

import java.io.IOException;

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

import org.apache.commons.codec.binary.Hex;

/**
 * @author morinp
 *
 */
public class ServicePostAction extends Action {


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

    /* (non-Javadoc)
     * @see com.liferay.portal.kernel.events.Action#run(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    @Override
    public void run(HttpServletRequest request, HttpServletResponse response)
            throws ActionException {

        Company company;
        try {
            company = PortalUtil.getCompany(request);

            String login = getLogin(request, "_58_login", company);

            if(login!=null && !login.isEmpty()) {
                User u = UserLocalServiceUtil.fetchUserByEmailAddress(company.getCompanyId(), login);
                if(u!=null && !u.isActive()) {
                    response.sendRedirect(Constants.LANDING_PAGE_DEACTIVATED);
                }
            }
        } catch (PortalException e) {
            _log.error("Impossible de récupérer la company");
            e.printStackTrace();
        } catch (SystemException e) {
            _log.error("Impossible de récupérer la company");
            e.printStackTrace();
        } catch (IOException e) {
            _log.error("Impossible de rediriger l'utilisateur");
            e.printStackTrace();
        }

    }

    /*Méthode de com.liferay.portlet.login.util.LoginUtil :*/
    /* (non-Javadoc)
     * @see com.liferay.portlet.login.util.LoginUtil#getLogin(javax.servlet.http.HttpServletRequest, java.lang.String, com.liferay.portlet.login.util.LoginUtil)
     */
    public static String getLogin(
            HttpServletRequest request, String paramName, Company company)
        throws SystemException {

        String login = request.getParameter(paramName);

        if ((login == null) || login.equals(StringPool.NULL)) {
            login = GetterUtil.getString(
                getCookie(request, CookieKeys.LOGIN, false));

            if (Boolean.getBoolean(PropsUtil.get(PropsKeys.COMPANY_LOGIN_PREPOPULATE_DOMAIN))
                    && Validator.isNull(login)
                    && company.getAuthType().equals(CompanyConstants.AUTH_TYPE_EA)) {

                login = "@" + company.getMx();
            }
        }

        return login;
    }

    /*Méthode de com.liferay.portal.util.CookieKeys :*/
    /* (non-Javadoc)
     * @see com.liferay.portal.util.CookieKeys#getCookie(javax.servlet.http.HttpServletRequest, java.lang.String, boolean)
     */
    public static String getCookie(
        HttpServletRequest request, String name, boolean toUpperCase) {

        String value = CookieUtil.get(request, name, toUpperCase);

        if ((value != null) && isEncodedCookie(name)) {
            try {
                String encodedValue = value;
                String originalValue = new String(
                    Hex.decodeHex(encodedValue.toCharArray()));

                return originalValue;
            }
            catch (Exception e) {

                return value;
            }
        }

        return value;
    }

    /*Méthode de com.liferay.portal.util.CookieKeys :*/
    /* (non-Javadoc)
     * @see com.liferay.portal.util.CookieKeys#isEncodedCookie(java.lang.String)
     */
    public static boolean isEncodedCookie(String name) {
        if (name.equals(CookieKeys.ID) || name.equals(CookieKeys.LOGIN) ||
                name.equals(CookieKeys.PASSWORD) || name.equals(CookieKeys.SCREEN_NAME)) {

            return true;
        }
        else {
            return false;
        }
    }

}