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