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