001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.events;
016    
017    import com.liferay.portal.exception.LayoutPermissionException;
018    import com.liferay.portal.exception.NoSuchGroupException;
019    import com.liferay.portal.exception.NoSuchLayoutException;
020    import com.liferay.portal.exception.NoSuchUserException;
021    import com.liferay.portal.kernel.events.Action;
022    import com.liferay.portal.kernel.events.ActionException;
023    import com.liferay.portal.kernel.exception.PortalException;
024    import com.liferay.portal.kernel.image.ImageToolUtil;
025    import com.liferay.portal.kernel.interval.IntervalActionProcessor;
026    import com.liferay.portal.kernel.log.Log;
027    import com.liferay.portal.kernel.log.LogFactoryUtil;
028    import com.liferay.portal.kernel.portlet.LiferayWindowState;
029    import com.liferay.portal.kernel.security.auth.PrincipalException;
030    import com.liferay.portal.kernel.security.permission.ActionKeys;
031    import com.liferay.portal.kernel.security.permission.PermissionChecker;
032    import com.liferay.portal.kernel.security.permission.PermissionCheckerFactoryUtil;
033    import com.liferay.portal.kernel.security.permission.PermissionThreadLocal;
034    import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
035    import com.liferay.portal.kernel.servlet.HttpHeaders;
036    import com.liferay.portal.kernel.servlet.PortalWebResourceConstants;
037    import com.liferay.portal.kernel.servlet.PortalWebResourcesUtil;
038    import com.liferay.portal.kernel.servlet.SessionErrors;
039    import com.liferay.portal.kernel.util.ColorSchemeFactoryUtil;
040    import com.liferay.portal.kernel.util.CookieKeys;
041    import com.liferay.portal.kernel.util.FriendlyURLNormalizerUtil;
042    import com.liferay.portal.kernel.util.GetterUtil;
043    import com.liferay.portal.kernel.util.HttpUtil;
044    import com.liferay.portal.kernel.util.LocaleUtil;
045    import com.liferay.portal.kernel.util.ParamUtil;
046    import com.liferay.portal.kernel.util.PropsKeys;
047    import com.liferay.portal.kernel.util.ServerDetector;
048    import com.liferay.portal.kernel.util.SessionParamUtil;
049    import com.liferay.portal.kernel.util.StringBundler;
050    import com.liferay.portal.kernel.util.StringPool;
051    import com.liferay.portal.kernel.util.StringUtil;
052    import com.liferay.portal.kernel.util.UnicodeProperties;
053    import com.liferay.portal.kernel.util.Validator;
054    import com.liferay.portal.kernel.util.WebKeys;
055    import com.liferay.portal.kernel.webserver.WebServerServletTokenUtil;
056    import com.liferay.portal.model.ColorScheme;
057    import com.liferay.portal.model.Company;
058    import com.liferay.portal.model.Group;
059    import com.liferay.portal.model.GroupConstants;
060    import com.liferay.portal.model.Image;
061    import com.liferay.portal.model.Layout;
062    import com.liferay.portal.model.LayoutConstants;
063    import com.liferay.portal.model.LayoutSet;
064    import com.liferay.portal.model.LayoutTemplate;
065    import com.liferay.portal.model.LayoutType;
066    import com.liferay.portal.model.LayoutTypeAccessPolicy;
067    import com.liferay.portal.model.LayoutTypePortlet;
068    import com.liferay.portal.model.LayoutTypePortletConstants;
069    import com.liferay.portal.model.RoleConstants;
070    import com.liferay.portal.model.Theme;
071    import com.liferay.portal.model.User;
072    import com.liferay.portal.model.impl.VirtualLayout;
073    import com.liferay.portal.service.GroupLocalServiceUtil;
074    import com.liferay.portal.service.ImageLocalServiceUtil;
075    import com.liferay.portal.service.LayoutLocalServiceUtil;
076    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
077    import com.liferay.portal.service.RoleLocalServiceUtil;
078    import com.liferay.portal.service.ServiceContext;
079    import com.liferay.portal.service.ServiceContextFactory;
080    import com.liferay.portal.service.ServiceContextThreadLocal;
081    import com.liferay.portal.service.ThemeLocalServiceUtil;
082    import com.liferay.portal.service.UserLocalServiceUtil;
083    import com.liferay.portal.service.permission.GroupPermissionUtil;
084    import com.liferay.portal.service.permission.LayoutPermissionUtil;
085    import com.liferay.portal.service.permission.PortalPermissionUtil;
086    import com.liferay.portal.theme.ThemeDisplay;
087    import com.liferay.portal.theme.ThemeDisplayFactory;
088    import com.liferay.portal.util.LayoutClone;
089    import com.liferay.portal.util.LayoutCloneFactory;
090    import com.liferay.portal.util.PortalUtil;
091    import com.liferay.portal.util.PortletKeys;
092    import com.liferay.portal.util.PrefsPropsUtil;
093    import com.liferay.portal.util.PropsUtil;
094    import com.liferay.portal.util.PropsValues;
095    import com.liferay.portlet.PortalPreferences;
096    import com.liferay.portlet.PortletPreferencesFactoryUtil;
097    import com.liferay.portlet.PortletURLImpl;
098    import com.liferay.portlet.exportimport.configuration.ExportImportConfigurationConstants;
099    import com.liferay.portlet.exportimport.configuration.ExportImportConfigurationSettingsMapFactory;
100    import com.liferay.portlet.exportimport.lar.PortletDataHandlerKeys;
101    import com.liferay.portlet.exportimport.model.ExportImportConfiguration;
102    import com.liferay.portlet.exportimport.service.ExportImportConfigurationLocalServiceUtil;
103    import com.liferay.portlet.exportimport.service.ExportImportLocalServiceUtil;
104    import com.liferay.sites.kernel.util.SitesUtil;
105    
106    import java.io.File;
107    import java.io.Serializable;
108    
109    import java.util.ArrayList;
110    import java.util.HashMap;
111    import java.util.LinkedHashMap;
112    import java.util.List;
113    import java.util.Locale;
114    import java.util.Map;
115    import java.util.TimeZone;
116    
117    import javax.portlet.PortletMode;
118    import javax.portlet.PortletRequest;
119    import javax.portlet.PortletURL;
120    
121    import javax.servlet.http.HttpServletRequest;
122    import javax.servlet.http.HttpServletResponse;
123    import javax.servlet.http.HttpSession;
124    
125    import org.apache.commons.lang.time.StopWatch;
126    
127    /**
128     * @author Brian Wing Shun Chan
129     * @author Felix Ventero
130     * @author Jorge Ferrer
131     */
132    public class ServicePreAction extends Action {
133    
134            public ServicePreAction() {
135                    initImportLARFiles();
136            }
137    
138            public ThemeDisplay initThemeDisplay(
139                            HttpServletRequest request, HttpServletResponse response)
140                    throws Exception {
141    
142                    HttpSession session = request.getSession();
143    
144                    // Company
145    
146                    Company company = PortalUtil.getCompany(request);
147    
148                    long companyId = company.getCompanyId();
149    
150                    // CDN host
151    
152                    String cdnHost = PortalUtil.getCDNHost(request);
153    
154                    String dynamicResourcesCDNHost = StringPool.BLANK;
155    
156                    boolean cdnDynamicResourceEnabled =
157                            PortalUtil.isCDNDynamicResourcesEnabled(request);
158    
159                    if (cdnDynamicResourceEnabled) {
160                            dynamicResourcesCDNHost = cdnHost;
161                    }
162    
163                    // Portal URL
164    
165                    String portalURL = PortalUtil.getPortalURL(request);
166    
167                    // Paths
168    
169                    String contextPath = PortalUtil.getPathContext();
170                    String friendlyURLPrivateGroupPath =
171                            PortalUtil.getPathFriendlyURLPrivateGroup();
172                    String friendlyURLPrivateUserPath =
173                            PortalUtil.getPathFriendlyURLPrivateUser();
174                    String friendlyURLPublicPath = PortalUtil.getPathFriendlyURLPublic();
175                    String imagePath = dynamicResourcesCDNHost.concat(
176                            PortalUtil.getPathImage());
177                    String mainPath = PortalUtil.getPathMain();
178    
179                    String i18nPath = (String)request.getAttribute(WebKeys.I18N_PATH);
180    
181                    if (Validator.isNotNull(i18nPath)) {
182                            if (Validator.isNotNull(contextPath)) {
183                                    String i18nContextPath = contextPath.concat(i18nPath);
184    
185                                    friendlyURLPrivateGroupPath = StringUtil.replaceFirst(
186                                            friendlyURLPrivateGroupPath, contextPath, i18nContextPath);
187                                    friendlyURLPrivateUserPath = StringUtil.replaceFirst(
188                                            friendlyURLPrivateUserPath, contextPath, i18nContextPath);
189                                    friendlyURLPublicPath = StringUtil.replaceFirst(
190                                            friendlyURLPublicPath, contextPath, i18nContextPath);
191                                    mainPath = StringUtil.replaceFirst(
192                                            mainPath, contextPath, i18nContextPath);
193                            }
194                            else {
195                                    friendlyURLPrivateGroupPath = i18nPath.concat(
196                                            friendlyURLPrivateGroupPath);
197                                    friendlyURLPrivateUserPath = i18nPath.concat(
198                                            friendlyURLPrivateUserPath);
199                                    friendlyURLPublicPath = i18nPath.concat(friendlyURLPublicPath);
200                                    mainPath = i18nPath.concat(mainPath);
201                            }
202                    }
203    
204                    // Company logo
205    
206                    StringBundler sb = new StringBundler(5);
207    
208                    sb.append(imagePath);
209                    sb.append("/company_logo?img_id=");
210                    sb.append(company.getLogoId());
211                    sb.append("&t=");
212                    sb.append(WebServerServletTokenUtil.getToken(company.getLogoId()));
213    
214                    String companyLogo = sb.toString();
215    
216                    int companyLogoHeight = 0;
217                    int companyLogoWidth = 0;
218    
219                    Image companyLogoImage = null;
220    
221                    if (company.getLogoId() > 0) {
222                            companyLogoImage = ImageLocalServiceUtil.getCompanyLogo(
223                                    company.getLogoId());
224                    }
225                    else {
226                            companyLogoImage = ImageToolUtil.getDefaultCompanyLogo();
227                    }
228    
229                    if (companyLogoImage != null) {
230                            companyLogoHeight = companyLogoImage.getHeight();
231                            companyLogoWidth = companyLogoImage.getWidth();
232                    }
233    
234                    String realCompanyLogo = companyLogo;
235                    int realCompanyLogoHeight = companyLogoHeight;
236                    int realCompanyLogoWidth = companyLogoWidth;
237    
238                    // User
239    
240                    User user = null;
241    
242                    try {
243                            user = PortalUtil.initUser(request);
244                    }
245                    catch (NoSuchUserException nsue) {
246                            return null;
247                    }
248    
249                    boolean signedIn = !user.isDefaultUser();
250    
251                    if (PropsValues.BROWSER_CACHE_DISABLED ||
252                            (PropsValues.BROWSER_CACHE_SIGNED_IN_DISABLED && signedIn)) {
253    
254                            response.setDateHeader(HttpHeaders.EXPIRES, 0);
255                            response.setHeader(
256                                    HttpHeaders.CACHE_CONTROL,
257                                    HttpHeaders.CACHE_CONTROL_NO_CACHE_VALUE);
258                            response.setHeader(
259                                    HttpHeaders.PRAGMA, HttpHeaders.PRAGMA_NO_CACHE_VALUE);
260                    }
261    
262                    User realUser = user;
263    
264                    Long realUserId = (Long)session.getAttribute(WebKeys.USER_ID);
265    
266                    if (realUserId != null) {
267                            if (user.getUserId() != realUserId.longValue()) {
268                                    realUser = UserLocalServiceUtil.getUserById(
269                                            realUserId.longValue());
270                            }
271                    }
272    
273                    String doAsUserId = ParamUtil.getString(request, "doAsUserId");
274                    String doAsUserLanguageId = ParamUtil.getString(
275                            request, "doAsUserLanguageId");
276                    long doAsGroupId = ParamUtil.getLong(request, "doAsGroupId");
277    
278                    long refererGroupId = ParamUtil.getLong(request, "refererGroupId");
279    
280                    long refererPlid = ParamUtil.getLong(request, "refererPlid");
281    
282                    if (LayoutLocalServiceUtil.fetchLayout(refererPlid) == null) {
283                            refererPlid = 0;
284                    }
285    
286                    // Permission checker
287    
288                    PermissionChecker permissionChecker =
289                            PermissionCheckerFactoryUtil.create(user);
290    
291                    PermissionThreadLocal.setPermissionChecker(permissionChecker);
292    
293                    // Cookie support
294    
295                    try {
296    
297                            // LEP-4069
298    
299                            CookieKeys.validateSupportCookie(request);
300                    }
301                    catch (Exception e) {
302                            CookieKeys.addSupportCookie(request, response);
303                    }
304    
305                    // Time zone
306    
307                    TimeZone timeZone = user.getTimeZone();
308    
309                    if (timeZone == null) {
310                            timeZone = company.getTimeZone();
311                    }
312    
313                    // Layouts
314    
315                    if (signedIn) {
316                            updateUserLayouts(user);
317                    }
318    
319                    Layout layout = null;
320                    List<Layout> layouts = null;
321    
322                    long plid = ParamUtil.getLong(request, "p_l_id");
323    
324                    boolean viewableSourceGroup = true;
325    
326                    if (plid > 0) {
327                            layout = LayoutLocalServiceUtil.getLayout(plid);
328                    }
329                    else {
330                            long groupId = ParamUtil.getLong(request, "groupId");
331                            boolean privateLayout = ParamUtil.getBoolean(
332                                    request, "privateLayout");
333                            long layoutId = ParamUtil.getLong(request, "layoutId");
334    
335                            if ((groupId > 0) && (layoutId > 0)) {
336                                    layout = LayoutLocalServiceUtil.getLayout(
337                                            groupId, privateLayout, layoutId);
338                            }
339                    }
340    
341                    if (layout != null) {
342                            Group layoutGroup = layout.getGroup();
343    
344                            if (layoutGroup.isUser() &&
345                                    ((layout.isPrivateLayout() &&
346                                      !PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED) ||
347                                     (layout.isPublicLayout() &&
348                                      !PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED))) {
349    
350                                    User layoutUser = UserLocalServiceUtil.getUserById(
351                                            companyId, layoutGroup.getClassPK());
352    
353                                    updateUserLayouts(layoutUser);
354    
355                                    layout = LayoutLocalServiceUtil.fetchLayout(layout.getPlid());
356                            }
357                    }
358    
359                    if (layout != null) {
360                            long sourceGroupId = ParamUtil.getLong(request, "p_v_l_s_g_id");
361    
362                            if ((sourceGroupId > 0) && (sourceGroupId != layout.getGroupId())) {
363                                    Group sourceGroup = GroupLocalServiceUtil.getGroup(
364                                            sourceGroupId);
365    
366                                    if (layout.isTypeControlPanel() || layout.isPublicLayout() ||
367                                            SitesUtil.isUserGroupLayoutSetViewable(
368                                                    permissionChecker, layout.getGroup())) {
369    
370                                            layout = new VirtualLayout(layout, sourceGroup);
371                                    }
372                                    else {
373                                            viewableSourceGroup = false;
374                                    }
375                            }
376                    }
377    
378                    String ppid = ParamUtil.getString(request, "p_p_id");
379    
380                    Boolean redirectToDefaultLayout = (Boolean)request.getAttribute(
381                            WebKeys.REDIRECT_TO_DEFAULT_LAYOUT);
382    
383                    if (redirectToDefaultLayout == null) {
384                            redirectToDefaultLayout = Boolean.FALSE;
385                    }
386    
387                    if (layout != null) {
388                            Group group = layout.getGroup();
389    
390                            if (!signedIn && PropsValues.AUTH_FORWARD_BY_REDIRECT) {
391                                    request.setAttribute(WebKeys.REQUESTED_LAYOUT, layout);
392                            }
393    
394                            boolean viewableGroup = hasAccessPermission(
395                                    permissionChecker, layout, doAsGroupId, true);
396                            boolean viewableStaging =
397                                    !group.isControlPanel() &&
398                                    GroupPermissionUtil.contains(
399                                            permissionChecker, group, ActionKeys.VIEW_STAGING);
400    
401                            if (viewableStaging) {
402                                    layouts = LayoutLocalServiceUtil.getLayouts(
403                                            layout.getGroupId(), layout.isPrivateLayout(),
404                                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
405                            }
406                            else if ((!viewableGroup || !viewableSourceGroup) &&
407                                             group.isStagingGroup()) {
408    
409                                    layout = null;
410                            }
411                            else if (!isLoginRequest(request) &&
412                                             (!viewableGroup || !viewableSourceGroup ||
413                                              (!redirectToDefaultLayout &&
414                                               !hasAccessPermission(
415                                                       permissionChecker, layout, doAsGroupId, false)))) {
416    
417                                    if (user.isDefaultUser() &&
418                                            PropsValues.AUTH_LOGIN_PROMPT_ENABLED) {
419    
420                                            throw new PrincipalException.MustBeAuthenticated(
421                                                    user.getUserId());
422                                    }
423    
424                                    sb = new StringBundler(6);
425    
426                                    sb.append("User ");
427                                    sb.append(user.getUserId());
428                                    sb.append(" is not allowed to access the ");
429                                    sb.append(layout.isPrivateLayout() ? "private": "public");
430                                    sb.append(" pages of group ");
431                                    sb.append(layout.getGroupId());
432    
433                                    if (_log.isWarnEnabled()) {
434                                            _log.warn(sb.toString());
435                                    }
436    
437                                    throw new NoSuchLayoutException(sb.toString());
438                            }
439                            else if (isLoginRequest(request) && !viewableGroup) {
440                                    layout = null;
441                            }
442                            else if (group.isLayoutPrototype()) {
443                                    layouts = new ArrayList<>();
444                            }
445                            else {
446                                    layouts = LayoutLocalServiceUtil.getLayouts(
447                                            layout.getGroupId(), layout.isPrivateLayout(),
448                                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
449    
450                                    if (!group.isControlPanel()) {
451                                            doAsGroupId = 0;
452                                    }
453                            }
454                    }
455    
456                    List<Layout> unfilteredLayouts = layouts;
457    
458                    LayoutComposite viewableLayoutComposite = null;
459    
460                    if (layout == null) {
461                            viewableLayoutComposite = getDefaultViewableLayoutComposite(
462                                    request, user, permissionChecker, doAsGroupId, signedIn);
463    
464                            request.setAttribute(WebKeys.LAYOUT_DEFAULT, Boolean.TRUE);
465                    }
466                    else {
467                            viewableLayoutComposite = getViewableLayoutComposite(
468                                    request, user, permissionChecker, layout, layouts, doAsGroupId);
469                    }
470    
471                    String layoutSetLogo = null;
472    
473                    layout = viewableLayoutComposite.getLayout();
474                    layouts = viewableLayoutComposite.getLayouts();
475    
476                    Group group = null;
477    
478                    if (layout != null) {
479                            group = layout.getGroup();
480    
481                            if (!group.isControlPanel()) {
482                                    rememberVisitedGroupIds(request, group.getGroupId());
483                            }
484                    }
485    
486                    LayoutTypePortlet layoutTypePortlet = null;
487    
488                    layouts = mergeAdditionalLayouts(
489                            request, user, permissionChecker, layout, layouts, doAsGroupId);
490    
491                    LayoutSet layoutSet = null;
492    
493                    boolean hasAddLayoutLayoutPermission = false;
494                    boolean hasCustomizeLayoutPermission = false;
495                    boolean hasDeleteLayoutPermission = false;
496                    boolean hasUpdateLayoutPermission = false;
497    
498                    boolean customizedView = SessionParamUtil.getBoolean(
499                            request, "customized_view", true);
500    
501                    if (layout != null) {
502                            LayoutType layoutType = layout.getLayoutType();
503    
504                            LayoutTypeAccessPolicy layoutTypeAccessPolicy =
505                                    layoutType.getLayoutTypeAccessPolicy();
506    
507                            hasAddLayoutLayoutPermission =
508                                    layoutTypeAccessPolicy.isAddLayoutAllowed(
509                                            permissionChecker, layout);
510                            hasCustomizeLayoutPermission =
511                                    layoutTypeAccessPolicy.isCustomizeLayoutAllowed(
512                                            permissionChecker, layout);
513                            hasDeleteLayoutPermission =
514                                    layoutTypeAccessPolicy.isDeleteLayoutAllowed(
515                                            permissionChecker, layout);
516                            hasUpdateLayoutPermission =
517                                    layoutTypeAccessPolicy.isUpdateLayoutAllowed(
518                                            permissionChecker, layout);
519    
520                            layoutSet = layout.getLayoutSet();
521    
522                            if (company.isSiteLogo()) {
523                                    long logoId = 0;
524    
525                                    if (layoutSet.isLogo()) {
526                                            logoId = layoutSet.getLogoId();
527    
528                                            if (logoId == 0) {
529                                                    logoId = layoutSet.getLiveLogoId();
530                                            }
531                                    }
532                                    else {
533                                            LayoutSet siblingLayoutSet =
534                                                    LayoutSetLocalServiceUtil.getLayoutSet(
535                                                            layout.getGroupId(), !layout.isPrivateLayout());
536    
537                                            if (siblingLayoutSet.isLogo()) {
538                                                    logoId = siblingLayoutSet.getLogoId();
539                                            }
540                                    }
541    
542                                    if (logoId > 0) {
543                                            sb = new StringBundler(5);
544    
545                                            sb.append(imagePath);
546                                            sb.append("/layout_set_logo?img_id=");
547                                            sb.append(logoId);
548                                            sb.append("&t=");
549                                            sb.append(WebServerServletTokenUtil.getToken(logoId));
550    
551                                            layoutSetLogo = sb.toString();
552    
553                                            Image layoutSetLogoImage =
554                                                    ImageLocalServiceUtil.getCompanyLogo(logoId);
555    
556                                            companyLogo = layoutSetLogo;
557                                            companyLogoHeight = layoutSetLogoImage.getHeight();
558                                            companyLogoWidth = layoutSetLogoImage.getWidth();
559                                    }
560                            }
561    
562                            plid = layout.getPlid();
563    
564                            // Updates to shared layouts are not reflected until the next time
565                            // the user logs in because group layouts are cached in the session
566    
567                            layout = (Layout)layout.clone();
568    
569                            layoutTypePortlet = (LayoutTypePortlet)layout.getLayoutType();
570    
571                            boolean customizable = layoutTypePortlet.isCustomizable();
572    
573                            if (!customizable ||
574                                    group.isLayoutPrototype() || group.isLayoutSetPrototype() ||
575                                    group.isStagingGroup()) {
576    
577                                    customizedView = false;
578                            }
579    
580                            layoutTypePortlet.setCustomizedView(customizedView);
581                            layoutTypePortlet.setUpdatePermission(hasUpdateLayoutPermission);
582    
583                            if (signedIn && customizable && customizedView &&
584                                    hasCustomizeLayoutPermission) {
585    
586                                    PortalPreferences portalPreferences =
587                                            PortletPreferencesFactoryUtil.getPortalPreferences(
588                                                    user.getUserId(), true);
589    
590                                    layoutTypePortlet.setPortalPreferences(portalPreferences);
591                            }
592    
593                            LayoutClone layoutClone = LayoutCloneFactory.getInstance();
594    
595                            if (layoutClone != null) {
596                                    String typeSettings = layoutClone.get(request, plid);
597    
598                                    if (typeSettings != null) {
599                                            UnicodeProperties typeSettingsProperties =
600                                                    new UnicodeProperties(true);
601    
602                                            typeSettingsProperties.load(typeSettings);
603    
604                                            String stateMax = typeSettingsProperties.getProperty(
605                                                    LayoutTypePortletConstants.STATE_MAX);
606                                            String stateMin = typeSettingsProperties.getProperty(
607                                                    LayoutTypePortletConstants.STATE_MIN);
608                                            String modeAbout = typeSettingsProperties.getProperty(
609                                                    LayoutTypePortletConstants.MODE_ABOUT);
610                                            String modeConfig = typeSettingsProperties.getProperty(
611                                                    LayoutTypePortletConstants.MODE_CONFIG);
612                                            String modeEdit = typeSettingsProperties.getProperty(
613                                                    LayoutTypePortletConstants.MODE_EDIT);
614                                            String modeEditDefaults =
615                                                    typeSettingsProperties.getProperty(
616                                                            LayoutTypePortletConstants.MODE_EDIT_DEFAULTS);
617                                            String modeEditGuest = typeSettingsProperties.getProperty(
618                                                    LayoutTypePortletConstants.MODE_EDIT_GUEST);
619                                            String modeHelp = typeSettingsProperties.getProperty(
620                                                    LayoutTypePortletConstants.MODE_HELP);
621                                            String modePreview = typeSettingsProperties.getProperty(
622                                                    LayoutTypePortletConstants.MODE_PREVIEW);
623                                            String modePrint = typeSettingsProperties.getProperty(
624                                                    LayoutTypePortletConstants.MODE_PRINT);
625    
626                                            layoutTypePortlet.setStateMax(stateMax);
627                                            layoutTypePortlet.setStateMin(stateMin);
628                                            layoutTypePortlet.setModeAbout(modeAbout);
629                                            layoutTypePortlet.setModeConfig(modeConfig);
630                                            layoutTypePortlet.setModeEdit(modeEdit);
631                                            layoutTypePortlet.setModeEditDefaults(modeEditDefaults);
632                                            layoutTypePortlet.setModeEditGuest(modeEditGuest);
633                                            layoutTypePortlet.setModeHelp(modeHelp);
634                                            layoutTypePortlet.setModePreview(modePreview);
635                                            layoutTypePortlet.setModePrint(modePrint);
636                                    }
637                            }
638    
639                            request.setAttribute(WebKeys.LAYOUT, layout);
640                            request.setAttribute(WebKeys.LAYOUTS, layouts);
641                    }
642    
643                    // Locale
644    
645                    String i18nLanguageId = (String)request.getAttribute(
646                            WebKeys.I18N_LANGUAGE_ID);
647    
648                    Locale locale = PortalUtil.getLocale(request, response, true);
649    
650                    // Scope
651    
652                    long scopeGroupId = PortalUtil.getScopeGroupId(request);
653    
654                    if (group.isInheritContent()) {
655                            scopeGroupId = group.getParentGroupId();
656                    }
657    
658                    if ((scopeGroupId <= 0) && (doAsGroupId > 0)) {
659                            scopeGroupId = doAsGroupId;
660                    }
661    
662                    long siteGroupId = 0;
663    
664                    if (layout != null) {
665                            if (layout.isTypeControlPanel()) {
666                                    siteGroupId = PortalUtil.getSiteGroupId(scopeGroupId);
667                            }
668                            else {
669                                    siteGroupId = PortalUtil.getSiteGroupId(layout.getGroupId());
670                            }
671                    }
672    
673                    // Theme and color scheme
674    
675                    Theme theme = null;
676                    ColorScheme colorScheme = null;
677    
678                    boolean wapTheme = BrowserSnifferUtil.isWap(request);
679    
680                    if ((layout != null) &&
681                            (layout.isTypeControlPanel() || group.isControlPanel())) {
682    
683                            String themeId = PrefsPropsUtil.getString(
684                                    companyId, PropsKeys.CONTROL_PANEL_LAYOUT_REGULAR_THEME_ID);
685                            String colorSchemeId =
686                                    ColorSchemeFactoryUtil.getDefaultRegularColorSchemeId();
687    
688                            theme = ThemeLocalServiceUtil.getTheme(
689                                    companyId, themeId, wapTheme);
690                            colorScheme = ThemeLocalServiceUtil.getColorScheme(
691                                    companyId, theme.getThemeId(), colorSchemeId, wapTheme);
692    
693                            if (!wapTheme && theme.isWapTheme()) {
694                                    theme = ThemeLocalServiceUtil.getTheme(
695                                            companyId,
696                                            PropsValues.CONTROL_PANEL_LAYOUT_REGULAR_THEME_ID, false);
697                                    colorScheme = ThemeLocalServiceUtil.getColorScheme(
698                                            companyId, theme.getThemeId(), colorSchemeId, false);
699                            }
700    
701                            request.setAttribute(WebKeys.THEME, theme);
702                            request.setAttribute(WebKeys.COLOR_SCHEME, colorScheme);
703                    }
704    
705                    boolean themeCssFastLoad = PropsValues.THEME_CSS_FAST_LOAD;
706    
707                    if (PropsValues.THEME_CSS_FAST_LOAD_CHECK_REQUEST_PARAMETER) {
708                            themeCssFastLoad = SessionParamUtil.getBoolean(
709                                    request, "css_fast_load", PropsValues.THEME_CSS_FAST_LOAD);
710                    }
711    
712                    boolean themeImagesFastLoad = PropsValues.THEME_IMAGES_FAST_LOAD;
713    
714                    if (PropsValues.THEME_IMAGES_FAST_LOAD_CHECK_REQUEST_PARAMETER) {
715                            SessionParamUtil.getBoolean(
716                                    request, "images_fast_load",
717                                    PropsValues.THEME_IMAGES_FAST_LOAD);
718                    }
719    
720                    boolean themeJsBarebone = PropsValues.JAVASCRIPT_BAREBONE_ENABLED;
721    
722                    if (themeJsBarebone) {
723                            if (signedIn) {
724                                    themeJsBarebone = false;
725                            }
726                    }
727    
728                    boolean themeJsFastLoad = SessionParamUtil.getBoolean(
729                            request, "js_fast_load", PropsValues.JAVASCRIPT_FAST_LOAD);
730    
731                    String lifecycle = ParamUtil.getString(request, "p_p_lifecycle", "0");
732    
733                    lifecycle = ParamUtil.getString(request, "p_t_lifecycle", lifecycle);
734    
735                    boolean isolated = ParamUtil.getBoolean(request, "p_p_isolated");
736    
737                    String facebookCanvasPageURL = (String)request.getAttribute(
738                            WebKeys.FACEBOOK_CANVAS_PAGE_URL);
739    
740                    boolean widget = false;
741    
742                    Boolean widgetObj = (Boolean)request.getAttribute(WebKeys.WIDGET);
743    
744                    if (widgetObj != null) {
745                            widget = widgetObj.booleanValue();
746                    }
747    
748                    // Theme display
749    
750                    ThemeDisplay themeDisplay = ThemeDisplayFactory.create();
751    
752                    themeDisplay.setRequest(request);
753                    themeDisplay.setResponse(response);
754    
755                    // Set attributes first that other methods (getCDNBaseURL and
756                    // setLookAndFeel) depend on
757    
758                    themeDisplay.setCDNHost(cdnHost);
759                    themeDisplay.setCDNDynamicResourcesHost(dynamicResourcesCDNHost);
760                    themeDisplay.setFacebookCanvasPageURL(facebookCanvasPageURL);
761                    themeDisplay.setPortalURL(portalURL);
762                    themeDisplay.setRefererPlid(refererPlid);
763                    themeDisplay.setSecure(request.isSecure());
764                    themeDisplay.setServerName(request.getServerName());
765                    themeDisplay.setServerPort(request.getServerPort());
766                    themeDisplay.setWidget(widget);
767    
768                    themeDisplay.setCompany(company);
769                    themeDisplay.setCompanyLogo(companyLogo);
770                    themeDisplay.setCompanyLogoHeight(companyLogoHeight);
771                    themeDisplay.setCompanyLogoWidth(companyLogoWidth);
772                    themeDisplay.setDoAsGroupId(doAsGroupId);
773                    themeDisplay.setDoAsUserId(doAsUserId);
774                    themeDisplay.setDoAsUserLanguageId(doAsUserLanguageId);
775                    themeDisplay.setI18nLanguageId(i18nLanguageId);
776                    themeDisplay.setI18nPath(i18nPath);
777                    themeDisplay.setIsolated(isolated);
778                    themeDisplay.setLanguageId(LocaleUtil.toLanguageId(locale));
779                    themeDisplay.setLayout(layout);
780                    themeDisplay.setLayouts(layouts);
781                    themeDisplay.setLayoutSet(layoutSet);
782                    themeDisplay.setLayoutSetLogo(layoutSetLogo);
783                    themeDisplay.setLayoutTypePortlet(layoutTypePortlet);
784                    themeDisplay.setLifecycle(lifecycle);
785                    themeDisplay.setLifecycleAction(lifecycle.equals("1"));
786                    themeDisplay.setLifecycleEvent(lifecycle.equals("3"));
787                    themeDisplay.setLifecycleRender(lifecycle.equals("0"));
788                    themeDisplay.setLifecycleResource(lifecycle.equals("2"));
789                    themeDisplay.setLocale(locale);
790                    themeDisplay.setLookAndFeel(theme, colorScheme);
791                    themeDisplay.setPathApplet(contextPath.concat("/applets"));
792                    themeDisplay.setPathCms(contextPath.concat("/cms"));
793                    themeDisplay.setPathContext(contextPath);
794                    themeDisplay.setPathFlash(contextPath.concat("/flash"));
795                    themeDisplay.setPathFriendlyURLPrivateGroup(
796                            friendlyURLPrivateGroupPath);
797                    themeDisplay.setPathFriendlyURLPrivateUser(friendlyURLPrivateUserPath);
798                    themeDisplay.setPathFriendlyURLPublic(friendlyURLPublicPath);
799                    themeDisplay.setPathImage(imagePath);
800                    themeDisplay.setPathJavaScript(
801                            PortalWebResourcesUtil.getContextPath(
802                                    PortalWebResourceConstants.RESOURCE_TYPE_JS));
803                    themeDisplay.setPathMain(mainPath);
804                    themeDisplay.setPathSound(contextPath.concat("/html/sound"));
805                    themeDisplay.setPermissionChecker(permissionChecker);
806                    themeDisplay.setPlid(plid);
807                    themeDisplay.setPpid(ppid);
808                    themeDisplay.setRealCompanyLogo(realCompanyLogo);
809                    themeDisplay.setRealCompanyLogoHeight(realCompanyLogoHeight);
810                    themeDisplay.setRealCompanyLogoWidth(realCompanyLogoWidth);
811                    themeDisplay.setRealUser(realUser);
812                    themeDisplay.setRefererGroupId(refererGroupId);
813                    themeDisplay.setScopeGroupId(scopeGroupId);
814                    themeDisplay.setSignedIn(signedIn);
815                    themeDisplay.setSiteDefaultLocale(
816                            PortalUtil.getSiteDefaultLocale(siteGroupId));
817                    themeDisplay.setSiteGroupId(siteGroupId);
818                    themeDisplay.setStateExclusive(LiferayWindowState.isExclusive(request));
819                    themeDisplay.setStateMaximized(LiferayWindowState.isMaximized(request));
820                    themeDisplay.setStatePopUp(LiferayWindowState.isPopUp(request));
821                    themeDisplay.setThemeCssFastLoad(themeCssFastLoad);
822                    themeDisplay.setThemeImagesFastLoad(themeImagesFastLoad);
823                    themeDisplay.setThemeJsBarebone(themeJsBarebone);
824                    themeDisplay.setThemeJsFastLoad(themeJsFastLoad);
825                    themeDisplay.setTimeZone(timeZone);
826                    themeDisplay.setUnfilteredLayouts(unfilteredLayouts);
827                    themeDisplay.setUser(user);
828    
829                    // Icons
830    
831                    boolean showControlPanelIcon = false;
832    
833                    if (signedIn &&
834                            PortalPermissionUtil.contains(
835                                    permissionChecker, ActionKeys.VIEW_CONTROL_PANEL)) {
836    
837                            showControlPanelIcon = true;
838                    }
839    
840                    themeDisplay.setShowControlPanelIcon(showControlPanelIcon);
841    
842                    themeDisplay.setShowHomeIcon(true);
843                    themeDisplay.setShowMyAccountIcon(signedIn);
844                    themeDisplay.setShowPageSettingsIcon(hasDeleteLayoutPermission);
845                    themeDisplay.setShowPortalIcon(true);
846                    themeDisplay.setShowSignInIcon(!signedIn);
847    
848                    boolean showSignOutIcon = signedIn;
849    
850                    if (themeDisplay.isImpersonated()) {
851                            showSignOutIcon = false;
852                    }
853    
854                    themeDisplay.setShowSignOutIcon(showSignOutIcon);
855    
856                    themeDisplay.setShowStagingIcon(false);
857    
858                    boolean showSiteAdministrationIcon = false;
859    
860                    if (signedIn &&
861                            GroupPermissionUtil.contains(
862                                    permissionChecker, group,
863                                    ActionKeys.VIEW_SITE_ADMINISTRATION)) {
864    
865                            showSiteAdministrationIcon = true;
866                    }
867    
868                    themeDisplay.setShowSiteAdministrationIcon(showSiteAdministrationIcon);
869    
870                    // Session
871    
872                    if (PropsValues.SESSION_ENABLE_URL_WITH_SESSION_ID &&
873                            !CookieKeys.hasSessionId(request)) {
874    
875                            themeDisplay.setAddSessionIdToURL(true);
876                            themeDisplay.setSessionId(session.getId());
877                    }
878    
879                    // URLs
880    
881                    String urlControlPanel = friendlyURLPrivateGroupPath.concat(
882                            GroupConstants.CONTROL_PANEL_FRIENDLY_URL);
883    
884                    if (Validator.isNotNull(doAsUserId)) {
885                            urlControlPanel = HttpUtil.addParameter(
886                                    urlControlPanel, "doAsUserId", doAsUserId);
887                    }
888    
889                    if (refererGroupId > 0) {
890                            urlControlPanel = HttpUtil.addParameter(
891                                    urlControlPanel, "refererGroupId", refererGroupId);
892                    }
893                    else if (scopeGroupId > 0) {
894                            Layout refererLayout = LayoutLocalServiceUtil.fetchLayout(plid);
895    
896                            if (refererLayout != null) {
897                                    Group refererLayoutGroup = refererLayout.getGroup();
898    
899                                    if (refererLayoutGroup.isUserGroup()) {
900                                            urlControlPanel = HttpUtil.addParameter(
901                                                    urlControlPanel, "refererGroupId", scopeGroupId);
902                                    }
903                            }
904                    }
905    
906                    if (refererPlid > 0) {
907                            urlControlPanel = HttpUtil.addParameter(
908                                    urlControlPanel, "refererPlid", refererPlid);
909                    }
910                    else if (plid > 0) {
911                            urlControlPanel = HttpUtil.addParameter(
912                                    urlControlPanel, "refererPlid", plid);
913                    }
914    
915                    if (themeDisplay.isAddSessionIdToURL()) {
916                            urlControlPanel = PortalUtil.getURLWithSessionId(
917                                    urlControlPanel, session.getId());
918                    }
919    
920                    themeDisplay.setURLControlPanel(urlControlPanel);
921    
922                    String currentURL = PortalUtil.getCurrentURL(request);
923    
924                    themeDisplay.setURLCurrent(currentURL);
925    
926                    String urlHome = PortalUtil.getHomeURL(request);
927    
928                    themeDisplay.setURLHome(urlHome);
929    
930                    if (layout != null) {
931                            if (layout.isTypePortlet()) {
932                                    boolean freeformLayout =
933                                            layoutTypePortlet.getLayoutTemplateId().equals("freeform");
934    
935                                    themeDisplay.setFreeformLayout(freeformLayout);
936    
937                                    if (hasUpdateLayoutPermission) {
938                                            themeDisplay.setShowLayoutTemplatesIcon(true);
939    
940                                            if (!group.isUser()) {
941                                                    themeDisplay.setShowPageCustomizationIcon(true);
942                                            }
943                                    }
944                            }
945    
946                            if (hasUpdateLayoutPermission) {
947                                    themeDisplay.setShowPageSettingsIcon(true);
948    
949                                    boolean site = group.isSite();
950    
951                                    if (!site && group.isStagingGroup()) {
952                                            Group liveGroup = group.getLiveGroup();
953    
954                                            site = liveGroup.isSite();
955                                    }
956    
957                                    if (site &&
958                                            GroupPermissionUtil.contains(
959                                                    permissionChecker, scopeGroupId,
960                                                    ActionKeys.ASSIGN_MEMBERS)) {
961    
962                                            themeDisplay.setShowManageSiteMembershipsIcon(true);
963                                    }
964                                    else {
965                                            themeDisplay.setShowManageSiteMembershipsIcon(false);
966                                    }
967                            }
968    
969                            Group scopeGroup = GroupLocalServiceUtil.getGroup(scopeGroupId);
970    
971                            boolean hasAddLayoutGroupPermission = GroupPermissionUtil.contains(
972                                    permissionChecker, scopeGroup, ActionKeys.ADD_LAYOUT);
973                            boolean hasManageLayoutsGroupPermission =
974                                    GroupPermissionUtil.contains(
975                                            permissionChecker, scopeGroup, ActionKeys.MANAGE_LAYOUTS);
976                            boolean hasManageStagingPermission = GroupPermissionUtil.contains(
977                                    permissionChecker, scopeGroup, ActionKeys.MANAGE_STAGING);
978                            boolean hasPublishStagingPermission = GroupPermissionUtil.contains(
979                                    permissionChecker, scopeGroup, ActionKeys.PUBLISH_STAGING);
980                            boolean hasUpdateGroupPermission = GroupPermissionUtil.contains(
981                                    permissionChecker, scopeGroup, ActionKeys.UPDATE);
982                            boolean hasViewStagingPermission = GroupPermissionUtil.contains(
983                                    permissionChecker, scopeGroup, ActionKeys.VIEW_STAGING);
984    
985                            if (!group.isControlPanel() && !group.isUser() &&
986                                    !group.isUserGroup() && hasUpdateGroupPermission) {
987    
988                                    themeDisplay.setShowSiteSettingsIcon(true);
989                            }
990    
991                            if (!group.isLayoutPrototype() &&
992                                    (hasAddLayoutGroupPermission || hasAddLayoutLayoutPermission ||
993                                     hasManageLayoutsGroupPermission || hasUpdateGroupPermission)) {
994    
995                                    themeDisplay.setShowSiteMapSettingsIcon(true);
996                            }
997    
998                            if (group.hasStagingGroup() && !group.isStagingGroup()) {
999                                    themeDisplay.setShowLayoutTemplatesIcon(false);
1000                                    themeDisplay.setURLPublishToLive(null);
1001                            }
1002    
1003                            if (group.isControlPanel()) {
1004                                    themeDisplay.setShowPageSettingsIcon(false);
1005                                    themeDisplay.setURLPublishToLive(null);
1006                            }
1007    
1008                            // LEP-4987
1009    
1010                            if (group.isStaged() || group.isStagingGroup()) {
1011                                    if (hasManageStagingPermission || hasPublishStagingPermission ||
1012                                            hasUpdateLayoutPermission || hasViewStagingPermission) {
1013    
1014                                            themeDisplay.setShowStagingIcon(true);
1015                                    }
1016    
1017                                    if (hasPublishStagingPermission) {
1018                                            PortletURL publishToLiveURL = new PortletURLImpl(
1019                                                    request, PortletKeys.EXPORT_IMPORT, plid,
1020                                                    PortletRequest.RENDER_PHASE);
1021    
1022                                            publishToLiveURL.setParameter(
1023                                                    "mvcRenderCommandName", "publishLayouts");
1024    
1025                                            if (layout.isPrivateLayout()) {
1026                                                    publishToLiveURL.setParameter("tabs1", "private-pages");
1027                                            }
1028                                            else {
1029                                                    publishToLiveURL.setParameter("tabs1", "public-pages");
1030                                            }
1031    
1032                                            publishToLiveURL.setParameter(
1033                                                    "groupId", String.valueOf(scopeGroupId));
1034                                            publishToLiveURL.setParameter(
1035                                                    "selPlid", String.valueOf(plid));
1036                                            publishToLiveURL.setPortletMode(PortletMode.VIEW);
1037                                            publishToLiveURL.setWindowState(
1038                                                    LiferayWindowState.EXCLUSIVE);
1039    
1040                                            themeDisplay.setURLPublishToLive(publishToLiveURL);
1041                                    }
1042                            }
1043                    }
1044    
1045                    if (!user.isActive() ||
1046                            (PrefsPropsUtil.getBoolean(
1047                                    companyId, PropsKeys.TERMS_OF_USE_REQUIRED) &&
1048                             !user.isAgreedToTermsOfUse())) {
1049    
1050                            themeDisplay.setShowMyAccountIcon(false);
1051                            themeDisplay.setShowPageSettingsIcon(false);
1052                    }
1053    
1054                    if ((layout != null) && layout.isLayoutPrototypeLinkActive()) {
1055                            themeDisplay.setShowPageCustomizationIcon(false);
1056                    }
1057    
1058                    if (group.isLayoutPrototype()) {
1059                            themeDisplay.setShowHomeIcon(false);
1060                            themeDisplay.setShowManageSiteMembershipsIcon(false);
1061                            themeDisplay.setShowMyAccountIcon(false);
1062                            themeDisplay.setShowPageCustomizationIcon(false);
1063                            themeDisplay.setShowPageSettingsIcon(true);
1064                            themeDisplay.setShowPortalIcon(false);
1065                            themeDisplay.setShowSignInIcon(false);
1066                            themeDisplay.setShowSignOutIcon(false);
1067                            themeDisplay.setShowSiteAdministrationIcon(false);
1068                            themeDisplay.setShowSiteSettingsIcon(false);
1069                            themeDisplay.setShowStagingIcon(false);
1070                    }
1071    
1072                    if (group.isLayoutSetPrototype()) {
1073                            themeDisplay.setShowPageCustomizationIcon(false);
1074                            themeDisplay.setShowSiteSettingsIcon(false);
1075                    }
1076    
1077                    if (group.hasStagingGroup() && !group.isStagingGroup()) {
1078                            themeDisplay.setShowLayoutTemplatesIcon(false);
1079                            themeDisplay.setShowPageCustomizationIcon(false);
1080                            themeDisplay.setShowSiteMapSettingsIcon(false);
1081                            themeDisplay.setShowSiteSettingsIcon(false);
1082                    }
1083    
1084                    themeDisplay.setURLPortal(portalURL.concat(contextPath));
1085    
1086                    boolean secure = false;
1087    
1088                    if (PropsValues.COMPANY_SECURITY_AUTH_REQUIRES_HTTPS ||
1089                            request.isSecure()) {
1090    
1091                            secure = true;
1092                    }
1093    
1094                    String securePortalURL = PortalUtil.getPortalURL(request, secure);
1095    
1096                    String urlSignIn = securePortalURL.concat(mainPath).concat(
1097                            _PATH_PORTAL_LOGIN);
1098    
1099                    if (layout != null) {
1100                            urlSignIn = HttpUtil.addParameter(
1101                                    urlSignIn, "p_l_id", layout.getPlid());
1102                    }
1103    
1104                    themeDisplay.setURLSignIn(urlSignIn);
1105    
1106                    themeDisplay.setURLSignOut(mainPath.concat(_PATH_PORTAL_LOGOUT));
1107    
1108                    return themeDisplay;
1109            }
1110    
1111            @Override
1112            public void run(HttpServletRequest request, HttpServletResponse response)
1113                    throws ActionException {
1114    
1115                    StopWatch stopWatch = new StopWatch();
1116    
1117                    stopWatch.start();
1118    
1119                    try {
1120                            servicePre(request, response);
1121                    }
1122                    catch (Exception e) {
1123                            throw new ActionException(e);
1124                    }
1125    
1126                    if (_log.isDebugEnabled()) {
1127                            _log.debug("Running takes " + stopWatch.getTime() + " ms");
1128                    }
1129            }
1130    
1131            protected void addDefaultLayoutsByLAR(
1132                            long userId, long groupId, boolean privateLayout, File larFile)
1133                    throws PortalException {
1134    
1135                    User user = UserLocalServiceUtil.getUser(userId);
1136    
1137                    Map<String, String[]> parameterMap = new HashMap<>();
1138    
1139                    parameterMap.put(
1140                            PortletDataHandlerKeys.PERMISSIONS,
1141                            new String[] {Boolean.TRUE.toString()});
1142                    parameterMap.put(
1143                            PortletDataHandlerKeys.PORTLET_ARCHIVED_SETUPS_ALL,
1144                            new String[] {Boolean.TRUE.toString()});
1145                    parameterMap.put(
1146                            PortletDataHandlerKeys.PORTLET_CONFIGURATION,
1147                            new String[] {Boolean.TRUE.toString()});
1148                    parameterMap.put(
1149                            PortletDataHandlerKeys.PORTLET_CONFIGURATION_ALL,
1150                            new String[] {Boolean.TRUE.toString()});
1151                    parameterMap.put(
1152                            PortletDataHandlerKeys.PORTLET_DATA,
1153                            new String[] {Boolean.TRUE.toString()});
1154                    parameterMap.put(
1155                            PortletDataHandlerKeys.PORTLET_DATA_ALL,
1156                            new String[] {Boolean.TRUE.toString()});
1157                    parameterMap.put(
1158                            PortletDataHandlerKeys.PORTLET_DATA_CONTROL_DEFAULT,
1159                            new String[] {Boolean.TRUE.toString()});
1160                    parameterMap.put(
1161                            PortletDataHandlerKeys.PORTLET_SETUP_ALL,
1162                            new String[] {Boolean.TRUE.toString()});
1163                    parameterMap.put(
1164                            PortletDataHandlerKeys.PORTLET_USER_PREFERENCES_ALL,
1165                            new String[] {Boolean.TRUE.toString()});
1166                    parameterMap.put(
1167                            PortletDataHandlerKeys.THEME_REFERENCE,
1168                            new String[] {Boolean.TRUE.toString()});
1169    
1170                    Map<String, Serializable> importLayoutSettingsMap =
1171                            ExportImportConfigurationSettingsMapFactory.
1172                                    buildImportLayoutSettingsMap(
1173                                            user, groupId, privateLayout, null, parameterMap);
1174    
1175                    ExportImportConfiguration exportImportConfiguration =
1176                            ExportImportConfigurationLocalServiceUtil.
1177                                    addDraftExportImportConfiguration(
1178                                            user.getUserId(),
1179                                            ExportImportConfigurationConstants.TYPE_IMPORT_LAYOUT,
1180                                            importLayoutSettingsMap);
1181    
1182                    ExportImportLocalServiceUtil.importLayouts(
1183                            exportImportConfiguration, larFile);
1184            }
1185    
1186            protected void addDefaultUserPrivateLayoutByProperties(
1187                            long userId, long groupId)
1188                    throws PortalException {
1189    
1190                    String friendlyURL = getFriendlyURL(
1191                            PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_FRIENDLY_URL);
1192    
1193                    ServiceContext serviceContext = new ServiceContext();
1194    
1195                    Layout layout = LayoutLocalServiceUtil.addLayout(
1196                            userId, groupId, true, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID,
1197                            PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_NAME, StringPool.BLANK,
1198                            StringPool.BLANK, LayoutConstants.TYPE_PORTLET, false, friendlyURL,
1199                            serviceContext);
1200    
1201                    LayoutTypePortlet layoutTypePortlet =
1202                            (LayoutTypePortlet)layout.getLayoutType();
1203    
1204                    layoutTypePortlet.setLayoutTemplateId(
1205                            0, PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_TEMPLATE_ID, false);
1206    
1207                    LayoutTemplate layoutTemplate = layoutTypePortlet.getLayoutTemplate();
1208    
1209                    for (String columnId : layoutTemplate.getColumns()) {
1210                            String keyPrefix = PropsKeys.DEFAULT_USER_PRIVATE_LAYOUT_PREFIX;
1211    
1212                            String portletIds = PropsUtil.get(keyPrefix.concat(columnId));
1213    
1214                            layoutTypePortlet.addPortletIds(
1215                                    0, StringUtil.split(portletIds), columnId, false);
1216                    }
1217    
1218                    LayoutLocalServiceUtil.updateLayout(
1219                            layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
1220                            layout.getTypeSettings());
1221    
1222                    boolean updateLayoutSet = false;
1223    
1224                    LayoutSet layoutSet = layout.getLayoutSet();
1225    
1226                    if (Validator.isNotNull(
1227                                    PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_REGULAR_THEME_ID)) {
1228    
1229                            layoutSet.setThemeId(
1230                                    PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_REGULAR_THEME_ID);
1231    
1232                            updateLayoutSet = true;
1233                    }
1234    
1235                    if (Validator.isNotNull(
1236                                    PropsValues.
1237                                            DEFAULT_USER_PRIVATE_LAYOUT_REGULAR_COLOR_SCHEME_ID)) {
1238    
1239                            layoutSet.setColorSchemeId(
1240                                    PropsValues.
1241                                            DEFAULT_USER_PRIVATE_LAYOUT_REGULAR_COLOR_SCHEME_ID);
1242    
1243                            updateLayoutSet = true;
1244                    }
1245    
1246                    if (Validator.isNotNull(
1247                                    PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_WAP_THEME_ID)) {
1248    
1249                            layoutSet.setWapThemeId(
1250                                    PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_WAP_THEME_ID);
1251    
1252                            updateLayoutSet = true;
1253                    }
1254    
1255                    if (Validator.isNotNull(
1256                                    PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_WAP_COLOR_SCHEME_ID)) {
1257    
1258                            layoutSet.setWapColorSchemeId(
1259                                    PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_WAP_COLOR_SCHEME_ID);
1260    
1261                            updateLayoutSet = true;
1262                    }
1263    
1264                    if (updateLayoutSet) {
1265                            LayoutSetLocalServiceUtil.updateLayoutSet(layoutSet);
1266                    }
1267            }
1268    
1269            protected void addDefaultUserPrivateLayouts(User user)
1270                    throws PortalException {
1271    
1272                    Group group = user.getGroup();
1273    
1274                    if (privateLARFile != null) {
1275                            addDefaultLayoutsByLAR(
1276                                    user.getUserId(), group.getGroupId(), true, privateLARFile);
1277                    }
1278                    else {
1279                            addDefaultUserPrivateLayoutByProperties(
1280                                    user.getUserId(), group.getGroupId());
1281                    }
1282            }
1283    
1284            protected void addDefaultUserPublicLayoutByProperties(
1285                            long userId, long groupId)
1286                    throws PortalException {
1287    
1288                    String friendlyURL = getFriendlyURL(
1289                            PropsValues.DEFAULT_USER_PUBLIC_LAYOUT_FRIENDLY_URL);
1290    
1291                    ServiceContext serviceContext = new ServiceContext();
1292    
1293                    Layout layout = LayoutLocalServiceUtil.addLayout(
1294                            userId, groupId, false, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID,
1295                            PropsValues.DEFAULT_USER_PUBLIC_LAYOUT_NAME, StringPool.BLANK,
1296                            StringPool.BLANK, LayoutConstants.TYPE_PORTLET, false, friendlyURL,
1297                            serviceContext);
1298    
1299                    LayoutTypePortlet layoutTypePortlet =
1300                            (LayoutTypePortlet)layout.getLayoutType();
1301    
1302                    layoutTypePortlet.setLayoutTemplateId(
1303                            0, PropsValues.DEFAULT_USER_PUBLIC_LAYOUT_TEMPLATE_ID, false);
1304    
1305                    LayoutTemplate layoutTemplate = layoutTypePortlet.getLayoutTemplate();
1306    
1307                    for (String columnId : layoutTemplate.getColumns()) {
1308                            String keyPrefix = PropsKeys.DEFAULT_USER_PUBLIC_LAYOUT_PREFIX;
1309    
1310                            String portletIds = PropsUtil.get(keyPrefix.concat(columnId));
1311    
1312                            layoutTypePortlet.addPortletIds(
1313                                    0, StringUtil.split(portletIds), columnId, false);
1314                    }
1315    
1316                    LayoutLocalServiceUtil.updateLayout(
1317                            layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
1318                            layout.getTypeSettings());
1319    
1320                    boolean updateLayoutSet = false;
1321    
1322                    LayoutSet layoutSet = layout.getLayoutSet();
1323    
1324                    if (Validator.isNotNull(
1325                                    PropsValues.DEFAULT_USER_PUBLIC_LAYOUT_REGULAR_THEME_ID)) {
1326    
1327                            layoutSet.setThemeId(
1328                                    PropsValues.DEFAULT_USER_PUBLIC_LAYOUT_REGULAR_THEME_ID);
1329    
1330                            updateLayoutSet = true;
1331                    }
1332    
1333                    if (Validator.isNotNull(
1334                                    PropsValues.
1335                                            DEFAULT_USER_PUBLIC_LAYOUT_REGULAR_COLOR_SCHEME_ID)) {
1336    
1337                            layoutSet.setColorSchemeId(
1338                                    PropsValues.DEFAULT_USER_PUBLIC_LAYOUT_REGULAR_COLOR_SCHEME_ID);
1339    
1340                            updateLayoutSet = true;
1341                    }
1342    
1343                    if (Validator.isNotNull(
1344                                    PropsValues.DEFAULT_USER_PUBLIC_LAYOUT_WAP_THEME_ID)) {
1345    
1346                            layoutSet.setWapThemeId(
1347                                    PropsValues.DEFAULT_USER_PUBLIC_LAYOUT_WAP_THEME_ID);
1348    
1349                            updateLayoutSet = true;
1350                    }
1351    
1352                    if (Validator.isNotNull(
1353                                    PropsValues.DEFAULT_USER_PUBLIC_LAYOUT_WAP_COLOR_SCHEME_ID)) {
1354    
1355                            layoutSet.setWapColorSchemeId(
1356                                    PropsValues.DEFAULT_USER_PUBLIC_LAYOUT_WAP_COLOR_SCHEME_ID);
1357    
1358                            updateLayoutSet = true;
1359                    }
1360    
1361                    if (updateLayoutSet) {
1362                            LayoutSetLocalServiceUtil.updateLayoutSet(layoutSet);
1363                    }
1364            }
1365    
1366            protected void addDefaultUserPublicLayouts(User user)
1367                    throws PortalException {
1368    
1369                    Group userGroup = user.getGroup();
1370    
1371                    if (publicLARFile != null) {
1372                            addDefaultLayoutsByLAR(
1373                                    user.getUserId(), userGroup.getGroupId(), false, publicLARFile);
1374                    }
1375                    else {
1376                            addDefaultUserPublicLayoutByProperties(
1377                                    user.getUserId(), userGroup.getGroupId());
1378                    }
1379            }
1380    
1381            protected void deleteDefaultUserPrivateLayouts(User user)
1382                    throws PortalException {
1383    
1384                    Group group = user.getGroup();
1385    
1386                    ServiceContext serviceContext = new ServiceContext();
1387    
1388                    LayoutLocalServiceUtil.deleteLayouts(
1389                            group.getGroupId(), true, serviceContext);
1390            }
1391    
1392            protected void deleteDefaultUserPublicLayouts(User user)
1393                    throws PortalException {
1394    
1395                    Group userGroup = user.getGroup();
1396    
1397                    ServiceContext serviceContext = new ServiceContext();
1398    
1399                    LayoutLocalServiceUtil.deleteLayouts(
1400                            userGroup.getGroupId(), false, serviceContext);
1401            }
1402    
1403            protected LayoutComposite getDefaultUserPersonalSiteLayoutComposite(
1404                    User user) {
1405    
1406                    Layout layout = null;
1407    
1408                    Group group = user.getGroup();
1409    
1410                    List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
1411                            group.getGroupId(), true, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
1412    
1413                    if (layouts.isEmpty()) {
1414                            layouts = LayoutLocalServiceUtil.getLayouts(
1415                                    group.getGroupId(), false,
1416                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
1417                    }
1418    
1419                    if (!layouts.isEmpty()) {
1420                            layout = layouts.get(0);
1421                    }
1422    
1423                    return new LayoutComposite(layout, layouts);
1424            }
1425    
1426            protected LayoutComposite getDefaultUserSitesLayoutComposite(
1427                            final User user)
1428                    throws PortalException {
1429    
1430                    final LinkedHashMap<String, Object> groupParams = new LinkedHashMap<>();
1431    
1432                    groupParams.put("usersGroups", Long.valueOf(user.getUserId()));
1433    
1434                    int count = GroupLocalServiceUtil.searchCount(
1435                            user.getCompanyId(), null, null, groupParams);
1436    
1437                    IntervalActionProcessor<LayoutComposite> intervalActionProcessor =
1438                            new IntervalActionProcessor<>(count);
1439    
1440                    intervalActionProcessor.setPerformIntervalActionMethod(
1441                            new IntervalActionProcessor.PerformIntervalActionMethod
1442                                    <LayoutComposite>() {
1443    
1444                                    @Override
1445                                    public LayoutComposite performIntervalAction(
1446                                            int start, int end) {
1447    
1448                                            List<Group> groups = GroupLocalServiceUtil.search(
1449                                                    user.getCompanyId(), null, null, groupParams, start,
1450                                                    end);
1451    
1452                                            for (Group group : groups) {
1453                                                    List<Layout> layouts =
1454                                                            LayoutLocalServiceUtil.getLayouts(
1455                                                                    group.getGroupId(), true,
1456                                                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
1457    
1458                                                    if (layouts.isEmpty()) {
1459                                                            layouts = LayoutLocalServiceUtil.getLayouts(
1460                                                                    group.getGroupId(), false,
1461                                                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
1462                                                    }
1463    
1464                                                    if (!layouts.isEmpty()) {
1465                                                            return new LayoutComposite(layouts.get(0), layouts);
1466                                                    }
1467                                            }
1468    
1469                                            return null;
1470                                    }
1471    
1472                            });
1473    
1474                    LayoutComposite layoutComposite =
1475                            intervalActionProcessor.performIntervalActions();
1476    
1477                    if (layoutComposite == null) {
1478                            return new LayoutComposite(null, new ArrayList<Layout>());
1479                    }
1480    
1481                    return layoutComposite;
1482            }
1483    
1484            protected LayoutComposite getDefaultViewableLayoutComposite(
1485                            HttpServletRequest request, User user,
1486                            PermissionChecker permissionChecker, long doAsGroupId,
1487                            boolean signedIn)
1488                    throws PortalException {
1489    
1490                    LayoutComposite defaultLayoutComposite =
1491                            getDefaultVirtualHostLayoutComposite(request);
1492    
1493                    defaultLayoutComposite = getViewableLayoutComposite(
1494                            request, user, permissionChecker, defaultLayoutComposite,
1495                            doAsGroupId);
1496    
1497                    if (defaultLayoutComposite.getLayouts() != null) {
1498                            return defaultLayoutComposite;
1499                    }
1500    
1501                    if (signedIn) {
1502                            defaultLayoutComposite = getDefaultUserPersonalSiteLayoutComposite(
1503                                    user);
1504    
1505                            if (defaultLayoutComposite.getLayout() == null) {
1506                                    defaultLayoutComposite = getDefaultUserSitesLayoutComposite(
1507                                            user);
1508                            }
1509    
1510                            defaultLayoutComposite = getViewableLayoutComposite(
1511                                    request, user, permissionChecker, defaultLayoutComposite,
1512                                    doAsGroupId);
1513    
1514                            if (defaultLayoutComposite.getLayouts() != null) {
1515                                    return defaultLayoutComposite;
1516                            }
1517                    }
1518    
1519                    defaultLayoutComposite = getGuestSiteLayoutComposite(user);
1520    
1521                    return getViewableLayoutComposite(
1522                            request, user, permissionChecker, defaultLayoutComposite,
1523                            doAsGroupId);
1524            }
1525    
1526            protected LayoutComposite getDefaultVirtualHostLayoutComposite(
1527                            HttpServletRequest request)
1528                    throws PortalException {
1529    
1530                    Layout layout = null;
1531                    List<Layout> layouts = null;
1532    
1533                    LayoutSet layoutSet = (LayoutSet)request.getAttribute(
1534                            WebKeys.VIRTUAL_HOST_LAYOUT_SET);
1535    
1536                    if (layoutSet != null) {
1537                            layouts = LayoutLocalServiceUtil.getLayouts(
1538                                    layoutSet.getGroupId(), layoutSet.isPrivateLayout(),
1539                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
1540    
1541                            Group group = null;
1542    
1543                            if (!layouts.isEmpty()) {
1544                                    layout = layouts.get(0);
1545    
1546                                    group = layout.getGroup();
1547                            }
1548    
1549                            if ((layout != null) && layout.isPrivateLayout()) {
1550                                    layouts = LayoutLocalServiceUtil.getLayouts(
1551                                            group.getGroupId(), false,
1552                                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
1553    
1554                                    if (!layouts.isEmpty()) {
1555                                            layout = layouts.get(0);
1556                                    }
1557                                    else {
1558                                            group = null;
1559                                            layout = null;
1560                                    }
1561                            }
1562    
1563                            if ((group != null) && group.isStagingGroup()) {
1564                                    Group liveGroup = group.getLiveGroup();
1565    
1566                                    layouts = LayoutLocalServiceUtil.getLayouts(
1567                                            liveGroup.getGroupId(), false,
1568                                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
1569    
1570                                    if (!layouts.isEmpty()) {
1571                                            layout = layouts.get(0);
1572                                    }
1573                                    else {
1574                                            layout = null;
1575                                    }
1576                            }
1577                    }
1578    
1579                    return new LayoutComposite(layout, layouts);
1580            }
1581    
1582            protected String getFriendlyURL(String friendlyURL) {
1583                    friendlyURL = GetterUtil.getString(friendlyURL);
1584    
1585                    return FriendlyURLNormalizerUtil.normalize(friendlyURL);
1586            }
1587    
1588            protected LayoutComposite getGuestSiteLayoutComposite(User user)
1589                    throws PortalException {
1590    
1591                    Layout layout = null;
1592                    List<Layout> layouts = null;
1593    
1594                    Group guestGroup = GroupLocalServiceUtil.getGroup(
1595                            user.getCompanyId(), GroupConstants.GUEST);
1596    
1597                    layouts = LayoutLocalServiceUtil.getLayouts(
1598                            guestGroup.getGroupId(), false,
1599                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
1600    
1601                    if (!layouts.isEmpty()) {
1602                            layout = layouts.get(0);
1603                    }
1604    
1605                    return new LayoutComposite(layout, layouts);
1606            }
1607    
1608            protected LayoutComposite getViewableLayoutComposite(
1609                            HttpServletRequest request, User user,
1610                            PermissionChecker permissionChecker, Layout layout,
1611                            List<Layout> layouts, long doAsGroupId)
1612                    throws PortalException {
1613    
1614                    if ((layouts == null) || layouts.isEmpty()) {
1615                            return new LayoutComposite(layout, layouts);
1616                    }
1617    
1618                    Group group = layout.getGroup();
1619    
1620                    boolean hasViewLayoutPermission = false;
1621                    boolean hasViewStagingPermission =
1622                            (group.isStagingGroup() || group.isStagedRemotely()) &&
1623                             !group.isControlPanel() &&
1624                             GroupPermissionUtil.contains(
1625                                     permissionChecker, group, ActionKeys.VIEW_STAGING);
1626    
1627                    if (hasAccessPermission(
1628                                    permissionChecker, layout, doAsGroupId, false) ||
1629                            hasViewStagingPermission) {
1630    
1631                            hasViewLayoutPermission = true;
1632                    }
1633    
1634                    List<Layout> accessibleLayouts = new ArrayList<>();
1635    
1636                    for (int i = 0; i < layouts.size(); i++) {
1637                            Layout curLayout = layouts.get(i);
1638    
1639                            if (!curLayout.isHidden() &&
1640                                    (hasAccessPermission(
1641                                            permissionChecker, curLayout, doAsGroupId, false) ||
1642                                     hasViewStagingPermission)) {
1643    
1644                                    if (accessibleLayouts.isEmpty() && !hasViewLayoutPermission) {
1645                                            layout = curLayout;
1646                                    }
1647    
1648                                    accessibleLayouts.add(curLayout);
1649                            }
1650                    }
1651    
1652                    if (accessibleLayouts.isEmpty()) {
1653                            layouts = null;
1654    
1655                            if (!isLoginRequest(request) && !hasViewLayoutPermission) {
1656                                    if (user.isDefaultUser() &&
1657                                            PropsValues.AUTH_LOGIN_PROMPT_ENABLED) {
1658    
1659                                            throw new PrincipalException.MustBeAuthenticated(
1660                                                    String.valueOf(user.getUserId()));
1661                                    }
1662    
1663                                    SessionErrors.add(
1664                                            request, LayoutPermissionException.class.getName());
1665                            }
1666                    }
1667                    else {
1668                            layouts = accessibleLayouts;
1669                    }
1670    
1671                    return new LayoutComposite(layout, layouts);
1672            }
1673    
1674            protected LayoutComposite getViewableLayoutComposite(
1675                            HttpServletRequest request, User user,
1676                            PermissionChecker permissionChecker,
1677                            LayoutComposite defaultLayoutComposite, long doAsGroupId)
1678                    throws PortalException {
1679    
1680                    Layout layout = defaultLayoutComposite.getLayout();
1681                    List<Layout> layouts = defaultLayoutComposite.getLayouts();
1682    
1683                    return getViewableLayoutComposite(
1684                            request, user, permissionChecker, layout, layouts, doAsGroupId);
1685            }
1686    
1687            protected boolean hasAccessPermission(
1688                            PermissionChecker permissionChecker, Layout layout,
1689                            long doAsGroupId, boolean checkViewableGroup)
1690                    throws PortalException {
1691    
1692                    return LayoutPermissionUtil.contains(
1693                            permissionChecker, layout, checkViewableGroup, ActionKeys.VIEW);
1694            }
1695    
1696            protected Boolean hasPowerUserRole(User user) throws Exception {
1697                    return RoleLocalServiceUtil.hasUserRole(
1698                            user.getUserId(), user.getCompanyId(), RoleConstants.POWER_USER,
1699                            true);
1700            }
1701    
1702            protected void initImportLARFiles() {
1703                    String privateLARFileName =
1704                            PropsValues.DEFAULT_USER_PRIVATE_LAYOUTS_LAR;
1705    
1706                    if (_log.isDebugEnabled()) {
1707                            _log.debug("Reading private LAR file " + privateLARFileName);
1708                    }
1709    
1710                    if (Validator.isNotNull(privateLARFileName)) {
1711                            privateLARFile = new File(privateLARFileName);
1712    
1713                            if (!privateLARFile.exists()) {
1714                                    _log.error(
1715                                            "Private LAR file " + privateLARFile + " does not exist");
1716    
1717                                    privateLARFile = null;
1718                            }
1719                            else {
1720                                    if (_log.isDebugEnabled()) {
1721                                            _log.debug("Using private LAR file " + privateLARFileName);
1722                                    }
1723                            }
1724                    }
1725    
1726                    String publicLARFileName = PropsValues.DEFAULT_USER_PUBLIC_LAYOUTS_LAR;
1727    
1728                    if (_log.isDebugEnabled()) {
1729                            _log.debug("Reading public LAR file " + publicLARFileName);
1730                    }
1731    
1732                    if (Validator.isNotNull(publicLARFileName)) {
1733                            publicLARFile = new File(publicLARFileName);
1734    
1735                            if (!publicLARFile.exists()) {
1736                                    _log.error(
1737                                            "Public LAR file " + publicLARFile + " does not exist");
1738    
1739                                    publicLARFile = null;
1740                            }
1741                            else {
1742                                    if (_log.isDebugEnabled()) {
1743                                            _log.debug("Using public LAR file " + publicLARFileName);
1744                                    }
1745                            }
1746                    }
1747            }
1748    
1749            /**
1750             * Returns <code>true</code> if the request URI's path starts with the
1751             * portal's default login path <code>c/portal/login</code>.
1752             *
1753             * @param  request the servlet request for the page, which can be a result
1754             *         of a redirect
1755             * @return <code>true</code> if the request is a login request;
1756             *         <code>false</code> otherwise
1757             */
1758            protected boolean isLoginRequest(HttpServletRequest request) {
1759                    String requestURI = request.getRequestURI();
1760    
1761                    String mainPath = PortalUtil.getPathMain();
1762    
1763                    if (requestURI.startsWith(mainPath.concat(_PATH_PORTAL_LOGIN))) {
1764                            return true;
1765                    }
1766                    else {
1767                            return false;
1768                    }
1769            }
1770    
1771            /**
1772             * @deprecated As of 6.1.0
1773             */
1774            @Deprecated
1775            protected boolean isViewableCommunity(
1776                            User user, long groupId, boolean privateLayout,
1777                            PermissionChecker permissionChecker)
1778                    throws PortalException {
1779    
1780                    return LayoutPermissionUtil.contains(
1781                            permissionChecker, groupId, privateLayout, 0, ActionKeys.VIEW);
1782            }
1783    
1784            /**
1785             * @deprecated As of 6.1.0
1786             */
1787            @Deprecated
1788            protected boolean isViewableGroup(
1789                            User user, long groupId, boolean privateLayout, long layoutId,
1790                            String controlPanelCategory, PermissionChecker permissionChecker)
1791                    throws PortalException {
1792    
1793                    return LayoutPermissionUtil.contains(
1794                            permissionChecker, groupId, privateLayout, layoutId,
1795                            ActionKeys.VIEW);
1796            }
1797    
1798            protected List<Layout> mergeAdditionalLayouts(
1799                            HttpServletRequest request, User user,
1800                            PermissionChecker permissionChecker, Layout layout,
1801                            List<Layout> layouts, long doAsGroupId)
1802                    throws PortalException {
1803    
1804                    if ((layout == null) || layout.isPrivateLayout()) {
1805                            return layouts;
1806                    }
1807    
1808                    long layoutGroupId = layout.getGroupId();
1809    
1810                    Group guestGroup = GroupLocalServiceUtil.getGroup(
1811                            user.getCompanyId(), GroupConstants.GUEST);
1812    
1813                    if (layoutGroupId != guestGroup.getGroupId()) {
1814                            Group layoutGroup = GroupLocalServiceUtil.getGroup(layoutGroupId);
1815    
1816                            UnicodeProperties typeSettingsProperties =
1817                                    layoutGroup.getTypeSettingsProperties();
1818    
1819                            boolean mergeGuestPublicPages = GetterUtil.getBoolean(
1820                                    typeSettingsProperties.getProperty("mergeGuestPublicPages"));
1821    
1822                            if (!mergeGuestPublicPages) {
1823                                    return layouts;
1824                            }
1825    
1826                            List<Layout> guestLayouts = LayoutLocalServiceUtil.getLayouts(
1827                                    guestGroup.getGroupId(), false,
1828                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
1829    
1830                            LayoutComposite viewableLayoutComposite =
1831                                    getViewableLayoutComposite(
1832                                            request, user, permissionChecker, layout, guestLayouts,
1833                                            doAsGroupId);
1834    
1835                            guestLayouts = viewableLayoutComposite.getLayouts();
1836    
1837                            if (layouts == null) {
1838                                    return guestLayouts;
1839                            }
1840    
1841                            layouts.addAll(0, guestLayouts);
1842                    }
1843                    else {
1844                            HttpSession session = request.getSession();
1845    
1846                            Long previousGroupId = (Long)session.getAttribute(
1847                                    WebKeys.VISITED_GROUP_ID_PREVIOUS);
1848    
1849                            if ((previousGroupId != null) &&
1850                                    (previousGroupId.longValue() != layoutGroupId)) {
1851    
1852                                    Group previousGroup = null;
1853    
1854                                    try {
1855                                            previousGroup = GroupLocalServiceUtil.getGroup(
1856                                                    previousGroupId.longValue());
1857                                    }
1858                                    catch (NoSuchGroupException nsge) {
1859                                            if (_log.isWarnEnabled()) {
1860                                                    _log.warn(nsge);
1861                                            }
1862    
1863                                            return layouts;
1864                                    }
1865    
1866                                    UnicodeProperties typeSettingsProperties =
1867                                            previousGroup.getTypeSettingsProperties();
1868    
1869                                    boolean mergeGuestPublicPages = GetterUtil.getBoolean(
1870                                            typeSettingsProperties.getProperty(
1871                                                    "mergeGuestPublicPages"));
1872    
1873                                    if (!mergeGuestPublicPages) {
1874                                            return layouts;
1875                                    }
1876    
1877                                    List<Layout> previousLayouts =
1878                                            LayoutLocalServiceUtil.getLayouts(
1879                                                    previousGroupId.longValue(), false,
1880                                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
1881    
1882                                    LayoutComposite viewableLayoutComposite =
1883                                            getViewableLayoutComposite(
1884                                                    request, user, permissionChecker, layout,
1885                                                    previousLayouts, doAsGroupId);
1886    
1887                                    previousLayouts = viewableLayoutComposite.getLayouts();
1888    
1889                                    if (previousLayouts != null) {
1890                                            layouts.addAll(previousLayouts);
1891                                    }
1892                            }
1893                    }
1894    
1895                    return layouts;
1896            }
1897    
1898            protected void rememberVisitedGroupIds(
1899                    HttpServletRequest request, long currentGroupId) {
1900    
1901                    String requestURI = GetterUtil.getString(request.getRequestURI());
1902    
1903                    if (!requestURI.endsWith(_PATH_PORTAL_LAYOUT)) {
1904                            return;
1905                    }
1906    
1907                    HttpSession session = request.getSession();
1908    
1909                    Long recentGroupId = (Long)session.getAttribute(
1910                            WebKeys.VISITED_GROUP_ID_RECENT);
1911    
1912                    Long previousGroupId = (Long)session.getAttribute(
1913                            WebKeys.VISITED_GROUP_ID_PREVIOUS);
1914    
1915                    if (recentGroupId == null) {
1916                            recentGroupId = Long.valueOf(currentGroupId);
1917    
1918                            session.setAttribute(
1919                                    WebKeys.VISITED_GROUP_ID_RECENT, recentGroupId);
1920                    }
1921                    else if (recentGroupId.longValue() != currentGroupId) {
1922                            previousGroupId = Long.valueOf(recentGroupId.longValue());
1923    
1924                            recentGroupId = Long.valueOf(currentGroupId);
1925    
1926                            session.setAttribute(
1927                                    WebKeys.VISITED_GROUP_ID_RECENT, recentGroupId);
1928    
1929                            session.setAttribute(
1930                                    WebKeys.VISITED_GROUP_ID_PREVIOUS, previousGroupId);
1931                    }
1932    
1933                    if (_log.isDebugEnabled()) {
1934                            _log.debug("Current group id " + currentGroupId);
1935                            _log.debug("Recent group id " + recentGroupId);
1936                            _log.debug("Previous group id " + previousGroupId);
1937                    }
1938            }
1939    
1940            protected void servicePre(
1941                            HttpServletRequest request, HttpServletResponse response)
1942                    throws Exception {
1943    
1944                    ThemeDisplay themeDisplay = initThemeDisplay(request, response);
1945    
1946                    if (themeDisplay == null) {
1947                            return;
1948                    }
1949    
1950                    request.setAttribute(WebKeys.THEME_DISPLAY, themeDisplay);
1951    
1952                    // Service context
1953    
1954                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
1955                            request);
1956    
1957                    ServiceContextThreadLocal.pushServiceContext(serviceContext);
1958    
1959                    // Ajaxable render
1960    
1961                    if (PropsValues.LAYOUT_AJAX_RENDER_ENABLE) {
1962                            boolean portletAjaxRender = ParamUtil.getBoolean(
1963                                    request, "p_p_ajax", true);
1964    
1965                            request.setAttribute(
1966                                    WebKeys.PORTLET_AJAX_RENDER, portletAjaxRender);
1967                    }
1968    
1969                    // Parallel render
1970    
1971                    if (PropsValues.LAYOUT_PARALLEL_RENDER_ENABLE &&
1972                            ServerDetector.isTomcat()) {
1973    
1974                            boolean portletParallelRender = ParamUtil.getBoolean(
1975                                    request, "p_p_parallel", true);
1976    
1977                            request.setAttribute(
1978                                    WebKeys.PORTLET_PARALLEL_RENDER, portletParallelRender);
1979                    }
1980            }
1981    
1982            protected void updateUserLayouts(User user) throws Exception {
1983                    Boolean hasPowerUserRole = null;
1984    
1985                    // Private layouts
1986    
1987                    boolean addDefaultUserPrivateLayouts = false;
1988    
1989                    if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED &&
1990                            PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_AUTO_CREATE) {
1991    
1992                            addDefaultUserPrivateLayouts = true;
1993    
1994                            if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_POWER_USER_REQUIRED) {
1995                                    if (hasPowerUserRole == null) {
1996                                            hasPowerUserRole = hasPowerUserRole(user);
1997                                    }
1998    
1999                                    if (!hasPowerUserRole.booleanValue()) {
2000                                            addDefaultUserPrivateLayouts = false;
2001                                    }
2002                            }
2003                    }
2004    
2005                    Boolean hasPrivateLayouts = null;
2006    
2007                    if (addDefaultUserPrivateLayouts) {
2008                            hasPrivateLayouts = LayoutLocalServiceUtil.hasLayouts(
2009                                    user, true, false);
2010    
2011                            if (!hasPrivateLayouts) {
2012                                    addDefaultUserPrivateLayouts(user);
2013                            }
2014                    }
2015    
2016                    boolean deleteDefaultUserPrivateLayouts = false;
2017    
2018                    if (!PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED) {
2019                            deleteDefaultUserPrivateLayouts = true;
2020                    }
2021                    else if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_POWER_USER_REQUIRED) {
2022                            if (hasPowerUserRole == null) {
2023                                    hasPowerUserRole = hasPowerUserRole(user);
2024                            }
2025    
2026                            if (!hasPowerUserRole.booleanValue()) {
2027                                    deleteDefaultUserPrivateLayouts = true;
2028                            }
2029                    }
2030    
2031                    if (deleteDefaultUserPrivateLayouts) {
2032                            if (hasPrivateLayouts == null) {
2033                                    hasPrivateLayouts = LayoutLocalServiceUtil.hasLayouts(
2034                                            user, true, false);
2035                            }
2036    
2037                            if (hasPrivateLayouts) {
2038                                    deleteDefaultUserPrivateLayouts(user);
2039                            }
2040                    }
2041    
2042                    // Public pages
2043    
2044                    boolean addDefaultUserPublicLayouts = false;
2045    
2046                    if (PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED &&
2047                            PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_AUTO_CREATE) {
2048    
2049                            addDefaultUserPublicLayouts = true;
2050    
2051                            if (PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_POWER_USER_REQUIRED) {
2052                                    if (hasPowerUserRole == null) {
2053                                            hasPowerUserRole = hasPowerUserRole(user);
2054                                    }
2055    
2056                                    if (!hasPowerUserRole.booleanValue()) {
2057                                            addDefaultUserPublicLayouts = false;
2058                                    }
2059                            }
2060                    }
2061    
2062                    Boolean hasPublicLayouts = null;
2063    
2064                    if (addDefaultUserPublicLayouts) {
2065                            hasPublicLayouts = LayoutLocalServiceUtil.hasLayouts(
2066                                    user, false, false);
2067    
2068                            if (!hasPublicLayouts) {
2069                                    addDefaultUserPublicLayouts(user);
2070                            }
2071                    }
2072    
2073                    boolean deleteDefaultUserPublicLayouts = false;
2074    
2075                    if (!PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED) {
2076                            deleteDefaultUserPublicLayouts = true;
2077                    }
2078                    else if (PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_POWER_USER_REQUIRED) {
2079                            if (hasPowerUserRole == null) {
2080                                    hasPowerUserRole = hasPowerUserRole(user);
2081                            }
2082    
2083                            if (!hasPowerUserRole.booleanValue()) {
2084                                    deleteDefaultUserPublicLayouts = true;
2085                            }
2086                    }
2087    
2088                    if (deleteDefaultUserPublicLayouts) {
2089                            if (hasPublicLayouts == null) {
2090                                    hasPublicLayouts = LayoutLocalServiceUtil.hasLayouts(
2091                                            user, false, false);
2092                            }
2093    
2094                            if (hasPublicLayouts) {
2095                                    deleteDefaultUserPublicLayouts(user);
2096                            }
2097                    }
2098            }
2099    
2100            protected File privateLARFile;
2101            protected File publicLARFile;
2102    
2103            protected class LayoutComposite {
2104    
2105                    protected LayoutComposite(Layout layout, List<Layout> layouts) {
2106                            _layout = layout;
2107                            _layouts = layouts;
2108                    }
2109    
2110                    protected Layout getLayout() {
2111                            return _layout;
2112                    }
2113    
2114                    protected List<Layout> getLayouts() {
2115                            return _layouts;
2116                    }
2117    
2118                    private final Layout _layout;
2119                    private final List<Layout> _layouts;
2120    
2121            }
2122    
2123            private static final String _PATH_PORTAL_LAYOUT = "/portal/layout";
2124    
2125            private static final String _PATH_PORTAL_LOGIN = "/portal/login";
2126    
2127            private static final String _PATH_PORTAL_LOGOUT = "/portal/logout";
2128    
2129            private static final Log _log = LogFactoryUtil.getLog(
2130                    ServicePreAction.class);
2131    
2132    }