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