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