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