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