login.events.post=xxx.xxx.xxx.LandingPageAction
#Experience definition
site.priority.order=Site1,Site2,Site3
package xxx.xxx.xxx;
import com.liferay.portal.kernel.events.Action;
import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.struts.LastPath;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.model.Group;
import com.liferay.portal.model.User;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portal.kernel.util.WebKeys;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
public class LandingPageAction extends Action {
@Override
public void run(HttpServletRequest request, HttpServletResponse response) throws ActionException {
try {
//HttpSession session = request.getSession();
User user = PortalUtil.getUser(request);
Group landingToGroup = getCommunityPriorityOrder(user);
String path = PortalUtil.getPortalProperties().get(PropsKeys.LAYOUT_FRIENDLY_URL_PRIVATE_GROUP_SERVLET_MAPPING) + landingToGroup.getFriendlyURL();
LastPath lastPath = new LastPath(StringPool.BLANK, path);
//session.setAttribute(WebKeys.LAST_PATH, lastPath);
response.sendRedirect(lastPath.getPath());
} catch (Exception e) {
e.printStackTrace();
}
}
private Group getCommunityPriorityOrder(User user) throws ActionException {
Group landingToGroup = null;
try{
List<Group> userSiteList = user.getSiteGroups();
String communityPriorityOrder = (String) PortalUtil.getPortalProperties().get("site.priority.order");
if( userSiteList.size() > 0) {
landingToGroup = userSiteList.get(0);
}
String [] communityPriorityList = null;
communityPriorityList = StringUtil.split(communityPriorityOrder, ",");
for (String communityName : communityPriorityList){
for(Group group :userSiteList){
if(group.getName().equalsIgnoreCase(communityName)){
landingToGroup = group;
return landingToGroup;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return landingToGroup;
}
}