2014-02-10 139 views
0

我需要重定向嘗試登錄我的Liferay網站時禁用/禁用用戶的用戶。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); 
    } 
} 

任何幫助表示讚賞。

謝謝,皮埃爾

+0

您可以使用DefaultLandingPageAction添加你的邏輯。 –

+0

謝謝你的幫助! 不幸的是,對於「login.events.pre」操作,由於對「(userId> 0)」的測試,登錄時(Liferay 6.1 EE中)「login.events.post」操作不會被觸發。 |(remoteUser == null)「在_MainServlet.loginUser_中。 – pierreonthenet

回答

1

你的貓嘗試使用的事件處理的「內核」,延長servlet.service.events.pre

Liferay: How to configure Liferay Portal

+0

謝謝你,我在看到你的帖子之前使用它解決了我的問題。 我會分享我的代碼時,我會通過stackoverflow允許這樣做。 – pierreonthenet

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; 
     } 
    } 

}