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