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