001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.events;
016    
017    import com.liferay.portal.LayoutPermissionException;
018    import com.liferay.portal.NoSuchGroupException;
019    import com.liferay.portal.NoSuchLayoutException;
020    import com.liferay.portal.NoSuchUserException;
021    import com.liferay.portal.kernel.dao.orm.QueryUtil;
022    import com.liferay.portal.kernel.events.Action;
023    import com.liferay.portal.kernel.events.ActionException;
024    import com.liferay.portal.kernel.exception.PortalException;
025    import com.liferay.portal.kernel.exception.SystemException;
026    import com.liferay.portal.kernel.language.LanguageUtil;
027    import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
028    import com.liferay.portal.kernel.log.Log;
029    import com.liferay.portal.kernel.log.LogFactoryUtil;
030    import com.liferay.portal.kernel.portlet.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.getOrganizationId();
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 (organizationId ==
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                                    LayoutSet layoutSet = layout.getLayoutSet();
1246    
1247                                    if (layoutSet.isLogo()) {
1248                                            logoId = layoutSet.getLogoId();
1249                                    }
1250                                    else {
1251                                            LayoutSet siblingLayoutSet =
1252                                                    LayoutSetLocalServiceUtil.getLayoutSet(
1253                                                            layout.getGroupId(), !layout.isPrivateLayout());
1254    
1255                                            if (siblingLayoutSet.isLogo()) {
1256                                                    logoId = siblingLayoutSet.getLogoId();
1257                                            }
1258                                    }
1259    
1260                                    if (logoId > 0) {
1261                                            sb = new StringBundler(5);
1262    
1263                                            sb.append(imagePath);
1264                                            sb.append("/layout_set_logo?img_id=");
1265                                            sb.append(logoId);
1266                                            sb.append("&t=");
1267                                            sb.append(ImageServletTokenUtil.getToken(logoId));
1268    
1269                                            layoutSetLogo = sb.toString();
1270    
1271                                            Image layoutSetLogoImage =
1272                                                    ImageLocalServiceUtil.getCompanyLogo(logoId);
1273    
1274                                            companyLogo = layoutSetLogo;
1275                                            companyLogoHeight = layoutSetLogoImage.getHeight();
1276                                            companyLogoWidth = layoutSetLogoImage.getWidth();
1277                                    }
1278                            }
1279    
1280                            plid = layout.getPlid();
1281    
1282                            // Updates to shared layouts are not reflected until the next time
1283                            // the user logs in because group layouts are cached in the session
1284    
1285                            layout = (Layout)((LayoutImpl)layout).clone();
1286    
1287                            layoutTypePortlet = (LayoutTypePortlet)layout.getLayoutType();
1288    
1289                            LayoutClone layoutClone = LayoutCloneFactory.getInstance();
1290    
1291                            if (layoutClone != null) {
1292                                    String typeSettings = layoutClone.get(request, plid);
1293    
1294                                    if (typeSettings != null) {
1295                                            UnicodeProperties props = new UnicodeProperties(true);
1296    
1297                                            props.load(typeSettings);
1298    
1299                                            String stateMax = props.getProperty(
1300                                                    LayoutTypePortletConstants.STATE_MAX);
1301                                            String stateMin = props.getProperty(
1302                                                    LayoutTypePortletConstants.STATE_MIN);
1303                                            String modeAbout = props.getProperty(
1304                                                    LayoutTypePortletConstants.MODE_ABOUT);
1305                                            String modeConfig = props.getProperty(
1306                                                    LayoutTypePortletConstants.MODE_CONFIG);
1307                                            String modeEdit = props.getProperty(
1308                                                    LayoutTypePortletConstants.MODE_EDIT);
1309                                            String modeEditDefaults = props.getProperty(
1310                                                    LayoutTypePortletConstants.MODE_EDIT_DEFAULTS);
1311                                            String modeEditGuest = props.getProperty(
1312                                                    LayoutTypePortletConstants.MODE_EDIT_GUEST);
1313                                            String modeHelp = props.getProperty(
1314                                                    LayoutTypePortletConstants.MODE_HELP);
1315                                            String modePreview = props.getProperty(
1316                                                    LayoutTypePortletConstants.MODE_PREVIEW);
1317                                            String modePrint = props.getProperty(
1318                                                    LayoutTypePortletConstants.MODE_PRINT);
1319    
1320                                            layoutTypePortlet.setStateMax(stateMax);
1321                                            layoutTypePortlet.setStateMin(stateMin);
1322                                            layoutTypePortlet.setModeAbout(modeAbout);
1323                                            layoutTypePortlet.setModeConfig(modeConfig);
1324                                            layoutTypePortlet.setModeEdit(modeEdit);
1325                                            layoutTypePortlet.setModeEditDefaults(modeEditDefaults);
1326                                            layoutTypePortlet.setModeEditGuest(modeEditGuest);
1327                                            layoutTypePortlet.setModeHelp(modeHelp);
1328                                            layoutTypePortlet.setModePreview(modePreview);
1329                                            layoutTypePortlet.setModePrint(modePrint);
1330                                    }
1331                            }
1332    
1333                            request.setAttribute(WebKeys.LAYOUT, layout);
1334                            request.setAttribute(WebKeys.LAYOUTS, layouts);
1335    
1336                            if (layout.isPrivateLayout()) {
1337                                    permissionChecker.setCheckGuest(false);
1338                            }
1339                    }
1340    
1341                    // Scope
1342    
1343                    long scopeGroupId = PortalUtil.getScopeGroupId(request);
1344                    long parentGroupId = PortalUtil.getParentGroupId(scopeGroupId);
1345    
1346                    // Theme and color scheme
1347    
1348                    Theme theme = null;
1349                    ColorScheme colorScheme = null;
1350    
1351                    boolean wapTheme = BrowserSnifferUtil.isWap(request);
1352    
1353                    if ((layout != null) &&
1354                            group.isControlPanel()) {
1355    
1356                            String themeId = PrefsPropsUtil.getString(
1357                                    companyId, PropsKeys.CONTROL_PANEL_LAYOUT_REGULAR_THEME_ID);
1358                            String colorSchemeId =
1359                                    ColorSchemeImpl.getDefaultRegularColorSchemeId();
1360    
1361                            theme = ThemeLocalServiceUtil.getTheme(
1362                                    companyId, themeId, wapTheme);
1363                            colorScheme = ThemeLocalServiceUtil.getColorScheme(
1364                                    companyId, theme.getThemeId(), colorSchemeId, wapTheme);
1365    
1366                            if (!wapTheme && theme.isWapTheme()) {
1367                                    theme = ThemeLocalServiceUtil.getTheme(
1368                                            companyId,
1369                                            PropsValues.CONTROL_PANEL_LAYOUT_REGULAR_THEME_ID, false);
1370                                    colorScheme = ThemeLocalServiceUtil.getColorScheme(
1371                                            companyId, theme.getThemeId(), colorSchemeId, false);
1372                            }
1373                    }
1374                    else if (layout != null) {
1375                            if (wapTheme) {
1376                                    theme = layout.getWapTheme();
1377                                    colorScheme = layout.getWapColorScheme();
1378                            }
1379                            else {
1380                                    theme = layout.getTheme();
1381                                    colorScheme = layout.getColorScheme();
1382                            }
1383                    }
1384                    else {
1385                            String themeId = null;
1386                            String colorSchemeId = null;
1387    
1388                            if (wapTheme) {
1389                                    themeId = ThemeImpl.getDefaultWapThemeId(companyId);
1390                                    colorSchemeId = ColorSchemeImpl.getDefaultWapColorSchemeId();
1391                            }
1392                            else {
1393                                    themeId = ThemeImpl.getDefaultRegularThemeId(companyId);
1394                                    colorSchemeId =
1395                                            ColorSchemeImpl.getDefaultRegularColorSchemeId();
1396                            }
1397    
1398                            theme = ThemeLocalServiceUtil.getTheme(
1399                                    companyId, themeId, wapTheme);
1400                            colorScheme = ThemeLocalServiceUtil.getColorScheme(
1401                                    companyId, theme.getThemeId(), colorSchemeId, wapTheme);
1402                    }
1403    
1404                    request.setAttribute(WebKeys.THEME, theme);
1405                    request.setAttribute(WebKeys.COLOR_SCHEME, colorScheme);
1406    
1407                    boolean themeCssFastLoad = SessionParamUtil.getBoolean(
1408                            request, "css_fast_load", PropsValues.THEME_CSS_FAST_LOAD);
1409                    boolean themeImagesFastLoad = SessionParamUtil.getBoolean(
1410                            request, "images_fast_load", PropsValues.THEME_IMAGES_FAST_LOAD);
1411    
1412                    boolean themeJsBarebone = PropsValues.JAVASCRIPT_BAREBONE_ENABLED;
1413    
1414                    if (themeJsBarebone) {
1415                            if (signedIn) {
1416                                    themeJsBarebone = false;
1417                            }
1418                    }
1419    
1420                    boolean themeJsFastLoad = SessionParamUtil.getBoolean(
1421                            request, "js_fast_load", PropsValues.JAVASCRIPT_FAST_LOAD);
1422    
1423                    String lifecycle = ParamUtil.getString(request, "p_p_lifecycle", "0");
1424                    boolean isolated = ParamUtil.getBoolean(request, "p_p_isolated");
1425    
1426                    String facebookCanvasPageURL = (String)request.getAttribute(
1427                            WebKeys.FACEBOOK_CANVAS_PAGE_URL);
1428    
1429                    boolean widget = false;
1430    
1431                    Boolean widgetObj = (Boolean)request.getAttribute(WebKeys.WIDGET);
1432    
1433                    if (widgetObj != null) {
1434                            widget = widgetObj.booleanValue();
1435                    }
1436    
1437                    // Theme display
1438    
1439                    ThemeDisplay themeDisplay = ThemeDisplayFactory.create();
1440    
1441                    // Set the CDN host, portal URL, and Facebook application ID first
1442                    // because other methods (setLookAndFeel) depend on them being set
1443    
1444                    themeDisplay.setCDNHost(cdnHost);
1445                    themeDisplay.setPortalURL(portalURL);
1446                    themeDisplay.setFacebookCanvasPageURL(facebookCanvasPageURL);
1447                    themeDisplay.setWidget(widget);
1448    
1449                    themeDisplay.setCompany(company);
1450                    themeDisplay.setCompanyLogo(companyLogo);
1451                    themeDisplay.setCompanyLogoHeight(companyLogoHeight);
1452                    themeDisplay.setCompanyLogoWidth(companyLogoWidth);
1453                    themeDisplay.setRealCompanyLogo(realCompanyLogo);
1454                    themeDisplay.setRealCompanyLogoHeight(realCompanyLogoHeight);
1455                    themeDisplay.setRealCompanyLogoWidth(realCompanyLogoWidth);
1456                    themeDisplay.setUser(user);
1457                    themeDisplay.setRealUser(realUser);
1458                    themeDisplay.setDoAsUserId(doAsUserId);
1459                    themeDisplay.setDoAsUserLanguageId(doAsUserLanguageId);
1460                    themeDisplay.setDoAsGroupId(doAsGroupId);
1461                    themeDisplay.setRefererPlid(refererPlid);
1462                    themeDisplay.setLayoutSetLogo(layoutSetLogo);
1463                    themeDisplay.setLayout(layout);
1464                    themeDisplay.setLayouts(layouts);
1465                    themeDisplay.setPlid(plid);
1466                    themeDisplay.setLayoutTypePortlet(layoutTypePortlet);
1467                    themeDisplay.setScopeGroupId(scopeGroupId);
1468                    themeDisplay.setParentGroupId(parentGroupId);
1469                    themeDisplay.setSignedIn(signedIn);
1470                    themeDisplay.setPermissionChecker(permissionChecker);
1471                    themeDisplay.setLocale(locale);
1472                    themeDisplay.setLanguageId(LocaleUtil.toLanguageId(locale));
1473                    themeDisplay.setI18nLanguageId(i18nLanguageId);
1474                    themeDisplay.setI18nPath(i18nPath);
1475                    themeDisplay.setTimeZone(timeZone);
1476                    themeDisplay.setLookAndFeel(contextPath, theme, colorScheme);
1477                    themeDisplay.setThemeCssFastLoad(themeCssFastLoad);
1478                    themeDisplay.setThemeImagesFastLoad(themeImagesFastLoad);
1479                    themeDisplay.setThemeJsBarebone(themeJsBarebone);
1480                    themeDisplay.setThemeJsFastLoad(themeJsFastLoad);
1481                    themeDisplay.setServerName(request.getServerName());
1482                    themeDisplay.setServerPort(request.getServerPort());
1483                    themeDisplay.setSecure(request.isSecure());
1484                    themeDisplay.setLifecycle(lifecycle);
1485                    themeDisplay.setLifecycleAction(lifecycle.equals("1"));
1486                    themeDisplay.setLifecycleRender(lifecycle.equals("0"));
1487                    themeDisplay.setLifecycleResource(lifecycle.equals("2"));
1488                    themeDisplay.setStateExclusive(LiferayWindowState.isExclusive(request));
1489                    themeDisplay.setStateMaximized(LiferayWindowState.isMaximized(request));
1490                    themeDisplay.setStatePopUp(LiferayWindowState.isPopUp(request));
1491                    themeDisplay.setIsolated(isolated);
1492                    themeDisplay.setPathApplet(contextPath.concat("/applets"));
1493                    themeDisplay.setPathCms(contextPath.concat("/cms"));
1494                    themeDisplay.setPathContext(contextPath);
1495                    themeDisplay.setPathFlash(contextPath.concat("/flash"));
1496                    themeDisplay.setPathFriendlyURLPrivateGroup(
1497                            friendlyURLPrivateGroupPath);
1498                    themeDisplay.setPathFriendlyURLPrivateUser(friendlyURLPrivateUserPath);
1499                    themeDisplay.setPathFriendlyURLPublic(friendlyURLPublicPath);
1500                    themeDisplay.setPathImage(imagePath);
1501                    themeDisplay.setPathJavaScript(
1502                            cdnHost.concat(contextPath).concat("/html/js"));
1503                    themeDisplay.setPathMain(mainPath);
1504                    themeDisplay.setPathSound(contextPath.concat("/html/sound"));
1505    
1506                    // URLs
1507    
1508                    themeDisplay.setShowAddContentIcon(false);
1509                    themeDisplay.setShowControlPanelIcon(signedIn);
1510                    themeDisplay.setShowHomeIcon(true);
1511                    themeDisplay.setShowMyAccountIcon(signedIn);
1512                    themeDisplay.setShowPageSettingsIcon(false);
1513                    themeDisplay.setShowPortalIcon(true);
1514                    themeDisplay.setShowSignInIcon(!signedIn);
1515                    themeDisplay.setShowSignOutIcon(signedIn);
1516                    themeDisplay.setShowStagingIcon(false);
1517    
1518                    String urlControlPanel = friendlyURLPrivateGroupPath.concat(
1519                            GroupConstants.CONTROL_PANEL_FRIENDLY_URL);
1520    
1521                    if (Validator.isNotNull(doAsUserId)) {
1522                            urlControlPanel = HttpUtil.addParameter(
1523                                    urlControlPanel, "doAsUserId", doAsUserId);
1524                    }
1525    
1526                    if (scopeGroupId > 0) {
1527                            urlControlPanel = HttpUtil.addParameter(
1528                                    urlControlPanel, "doAsGroupId", scopeGroupId);
1529                    }
1530    
1531                    if (refererPlid > 0) {
1532                            urlControlPanel = HttpUtil.addParameter(
1533                                    urlControlPanel, "refererPlid", refererPlid);
1534                    }
1535                    else if (plid > 0) {
1536                            urlControlPanel = HttpUtil.addParameter(
1537                                    urlControlPanel, "refererPlid", plid);
1538                    }
1539    
1540                    themeDisplay.setURLControlPanel(urlControlPanel);
1541    
1542                    PortletURL createAccountURL = new PortletURLImpl(
1543                            request, PortletKeys.LOGIN, plid, PortletRequest.ACTION_PHASE);
1544    
1545                    createAccountURL.setWindowState(WindowState.MAXIMIZED);
1546                    createAccountURL.setPortletMode(PortletMode.VIEW);
1547    
1548                    createAccountURL.setParameter("saveLastPath", "0");
1549                    createAccountURL.setParameter(
1550                            "struts_action", "/login/create_account");
1551    
1552                    themeDisplay.setURLCreateAccount(createAccountURL);
1553    
1554                    String currentURL = PortalUtil.getCurrentURL(request);
1555    
1556                    themeDisplay.setURLCurrent(currentURL);
1557    
1558                    String urlHome = PortalUtil.getHomeURL(request);
1559    
1560                    themeDisplay.setURLHome(urlHome);
1561    
1562                    if (layout != null) {
1563                            if (layout.isTypePortlet()) {
1564                                    boolean freeformLayout =
1565                                            layoutTypePortlet.getLayoutTemplateId().equals(
1566                                                    "freeform");
1567    
1568                                    themeDisplay.setFreeformLayout(freeformLayout);
1569    
1570                                    boolean hasUpdateLayoutPermission =
1571                                            LayoutPermissionUtil.contains(
1572                                                    permissionChecker, layout, ActionKeys.UPDATE);
1573    
1574                                    if (hasUpdateLayoutPermission) {
1575                                            themeDisplay.setShowAddContentIconPermission(true);
1576    
1577                                            if (!LiferayWindowState.isMaximized(request)) {
1578                                                    themeDisplay.setShowAddContentIcon(true);
1579                                            }
1580    
1581                                            themeDisplay.setShowLayoutTemplatesIcon(true);
1582    
1583                                            themeDisplay.setURLAddContent(
1584                                                    "Liferay.LayoutConfiguration.toggle('".concat(
1585                                                            PortletKeys.LAYOUT_CONFIGURATION).concat("');"));
1586    
1587                                            themeDisplay.setURLLayoutTemplates(
1588                                                    "Liferay.LayoutConfiguration.showTemplates();");
1589                                    }
1590                            }
1591    
1592                            boolean hasManageLayoutsPermission =
1593                                    GroupPermissionUtil.contains(
1594                                            permissionChecker, scopeGroupId, ActionKeys.MANAGE_LAYOUTS);
1595    
1596                            if (group.isUser()) {
1597                                    if ((layout.isPrivateLayout() &&
1598                                             !PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_MODIFIABLE) ||
1599                                            (layout.isPublicLayout() &&
1600                                             !PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_MODIFIABLE)) {
1601    
1602                                            hasManageLayoutsPermission = false;
1603                                    }
1604                            }
1605    
1606                            if (hasManageLayoutsPermission) {
1607                                    themeDisplay.setShowPageSettingsIcon(true);
1608    
1609                                    PortletURL pageSettingsURL = new PortletURLImpl(
1610                                            request, PortletKeys.LAYOUT_MANAGEMENT, plid,
1611                                            PortletRequest.RENDER_PHASE);
1612    
1613                                    pageSettingsURL.setWindowState(WindowState.MAXIMIZED);
1614                                    pageSettingsURL.setPortletMode(PortletMode.VIEW);
1615    
1616                                    pageSettingsURL.setParameter(
1617                                            "struts_action", "/layout_management/edit_pages");
1618    
1619                                    if (layout.isPrivateLayout()) {
1620                                            pageSettingsURL.setParameter("tabs1", "private-pages");
1621                                    }
1622                                    else {
1623                                            pageSettingsURL.setParameter("tabs1", "public-pages");
1624                                    }
1625    
1626                                    pageSettingsURL.setParameter("redirect", currentURL);
1627                                    pageSettingsURL.setParameter(
1628                                            "groupId", String.valueOf(scopeGroupId));
1629                                    pageSettingsURL.setParameter("selPlid", String.valueOf(plid));
1630    
1631                                    themeDisplay.setURLPageSettings(pageSettingsURL);
1632                            }
1633    
1634                            if (group.hasStagingGroup() && !group.isStagingGroup()) {
1635                                    themeDisplay.setShowAddContentIcon(false);
1636                                    themeDisplay.setShowLayoutTemplatesIcon(false);
1637                                    themeDisplay.setShowPageSettingsIcon(false);
1638                                    themeDisplay.setURLPublishToLive(null);
1639                            }
1640    
1641                            if (group.isControlPanel()) {
1642                                    themeDisplay.setShowPageSettingsIcon(false);
1643                                    themeDisplay.setURLPublishToLive(null);
1644                            }
1645    
1646                            // LEP-4987
1647    
1648                            if (group.isStaged() || group.isStagingGroup()) {
1649                                    boolean hasApproveProposalPermission =
1650                                            GroupPermissionUtil.contains(
1651                                                    permissionChecker, scopeGroupId,
1652                                                    ActionKeys.APPROVE_PROPOSAL);
1653    
1654                                    boolean hasPublishStagingPermission =
1655                                            GroupPermissionUtil.contains(
1656                                                    permissionChecker, scopeGroupId,
1657                                                    ActionKeys.PUBLISH_STAGING);
1658    
1659                                    if (hasApproveProposalPermission ||
1660                                            hasManageLayoutsPermission || hasPublishStagingPermission) {
1661    
1662                                            themeDisplay.setShowStagingIcon(true);
1663                                    }
1664    
1665                                    if (hasPublishStagingPermission) {
1666                                            PortletURL publishToLiveURL = new PortletURLImpl(
1667                                                    request, PortletKeys.LAYOUT_MANAGEMENT, plid,
1668                                                    PortletRequest.RENDER_PHASE);
1669    
1670                                            publishToLiveURL.setWindowState(
1671                                                    LiferayWindowState.EXCLUSIVE);
1672                                            publishToLiveURL.setPortletMode(PortletMode.VIEW);
1673    
1674                                            publishToLiveURL.setParameter(
1675                                                    "struts_action", "/layout_management/export_pages");
1676    
1677                                            if (layout.isPrivateLayout()) {
1678                                                    publishToLiveURL.setParameter("tabs1", "private-pages");
1679                                            }
1680                                            else {
1681                                                    publishToLiveURL.setParameter("tabs1", "public-pages");
1682                                            }
1683    
1684                                            publishToLiveURL.setParameter("pagesRedirect", currentURL);
1685                                            publishToLiveURL.setParameter(
1686                                                    "groupId", String.valueOf(scopeGroupId));
1687                                            publishToLiveURL.setParameter(
1688                                                    "selPlid", String.valueOf(plid));
1689    
1690                                            themeDisplay.setURLPublishToLive(publishToLiveURL);
1691                                    }
1692                            }
1693    
1694                            String myAccountNamespace = PortalUtil.getPortletNamespace(
1695                                    PortletKeys.MY_ACCOUNT);
1696    
1697                            String myAccountRedirect = ParamUtil.getString(
1698                                    request, myAccountNamespace.concat("backURL"), currentURL);
1699    
1700                            Group controlPanelGroup = GroupLocalServiceUtil.getGroup(
1701                                    companyId, GroupConstants.CONTROL_PANEL);
1702    
1703                            long controlPanelPlid = LayoutLocalServiceUtil.getDefaultPlid(
1704                                    controlPanelGroup.getGroupId(), true);
1705    
1706                            PortletURLImpl myAccountURL = new PortletURLImpl(
1707                                    request, PortletKeys.MY_ACCOUNT, controlPanelPlid,
1708                                    PortletRequest.RENDER_PHASE);
1709    
1710                            myAccountURL.setWindowState(WindowState.MAXIMIZED);
1711                            myAccountURL.setPortletMode(PortletMode.VIEW);
1712                            myAccountURL.setRefererPlid(plid);
1713    
1714                            myAccountURL.setParameter("struts_action", "/my_account/edit_user");
1715                            myAccountURL.setParameter("backURL", myAccountRedirect);
1716    
1717                            themeDisplay.setURLMyAccount(myAccountURL);
1718                    }
1719    
1720                    if ((!user.isActive()) ||
1721                            (PrefsPropsUtil.getBoolean(
1722                                    companyId, PropsKeys.TERMS_OF_USE_REQUIRED) &&
1723                             !user.isAgreedToTermsOfUse())) {
1724    
1725                            themeDisplay.setShowAddContentIcon(false);
1726                            themeDisplay.setShowMyAccountIcon(false);
1727                            themeDisplay.setShowPageSettingsIcon(false);
1728                    }
1729    
1730                    if (group.isLayoutPrototype()) {
1731                            themeDisplay.setShowControlPanelIcon(false);
1732                            themeDisplay.setShowHomeIcon(false);
1733                            themeDisplay.setShowMyAccountIcon(false);
1734                            themeDisplay.setShowPageSettingsIcon(true);
1735                            themeDisplay.setShowPortalIcon(false);
1736                            themeDisplay.setShowSignInIcon(false);
1737                            themeDisplay.setShowSignOutIcon(false);
1738                            themeDisplay.setShowStagingIcon(false);
1739                    }
1740    
1741                    themeDisplay.setURLPortal(portalURL.concat(contextPath));
1742    
1743                    String urlSignIn = mainPath.concat("/portal/login");
1744    
1745                    if (layout != null) {
1746                            urlSignIn = HttpUtil.addParameter(
1747                                    urlSignIn, "p_l_id", layout.getPlid());
1748                    }
1749    
1750                    themeDisplay.setURLSignIn(urlSignIn);
1751    
1752                    themeDisplay.setURLSignOut(mainPath.concat("/portal/logout"));
1753    
1754                    PortletURL updateManagerURL = new PortletURLImpl(
1755                            request, PortletKeys.UPDATE_MANAGER, plid,
1756                            PortletRequest.RENDER_PHASE);
1757    
1758                    updateManagerURL.setWindowState(WindowState.MAXIMIZED);
1759                    updateManagerURL.setPortletMode(PortletMode.VIEW);
1760    
1761                    updateManagerURL.setParameter("struts_action", "/update_manager/view");
1762    
1763                    themeDisplay.setURLUpdateManager(updateManagerURL);
1764    
1765                    request.setAttribute(WebKeys.THEME_DISPLAY, themeDisplay);
1766    
1767                    // Parallel render
1768    
1769                    boolean parallelRenderEnable = true;
1770    
1771                    if (layout != null) {
1772                            List<String> portletIds = layoutTypePortlet.getPortletIds();
1773    
1774                            if (portletIds.size() == 1) {
1775                                    String portletId = portletIds.get(0);
1776    
1777                                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
1778                                            portletId);
1779    
1780                                    if ((portlet != null) && !portlet.isAjaxable()) {
1781                                            parallelRenderEnable = false;
1782                                    }
1783                            }
1784                    }
1785    
1786                    Boolean parallelRenderEnableObj = Boolean.valueOf(ParamUtil.getBoolean(
1787                            request, "p_p_parallel", parallelRenderEnable));
1788    
1789                    request.setAttribute(
1790                            WebKeys.PORTLET_PARALLEL_RENDER, parallelRenderEnableObj);
1791            }
1792    
1793            protected void updateUserLayouts(User user) throws Exception {
1794                    Boolean hasPowerUserRole = null;
1795    
1796                    // Private layouts
1797    
1798                    boolean addDefaultUserPrivateLayouts = false;
1799    
1800                    if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED &&
1801                            PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_AUTO_CREATE) {
1802    
1803                            addDefaultUserPrivateLayouts = true;
1804    
1805                            if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_POWER_USER_REQUIRED) {
1806                                    if (hasPowerUserRole == null) {
1807                                            hasPowerUserRole = hasPowerUserRole(user);
1808                                    }
1809    
1810                                    if (!hasPowerUserRole.booleanValue()) {
1811                                            addDefaultUserPrivateLayouts = false;
1812                                    }
1813                            }
1814                    }
1815    
1816                    if (addDefaultUserPrivateLayouts && !user.hasPrivateLayouts()) {
1817                            addDefaultUserPrivateLayouts(user);
1818                    }
1819    
1820                    boolean deleteDefaultUserPrivateLayouts = false;
1821    
1822                    if (!PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED) {
1823                            deleteDefaultUserPrivateLayouts = true;
1824                    }
1825                    else if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_POWER_USER_REQUIRED) {
1826                            if (hasPowerUserRole == null) {
1827                                    hasPowerUserRole = hasPowerUserRole(user);
1828                            }
1829    
1830                            if (!hasPowerUserRole.booleanValue()) {
1831                                    deleteDefaultUserPrivateLayouts = true;
1832                            }
1833                    }
1834    
1835                    if (deleteDefaultUserPrivateLayouts && user.hasPrivateLayouts()) {
1836                            deleteDefaultUserPrivateLayouts(user);
1837                    }
1838    
1839                    // Public pages
1840    
1841                    boolean addDefaultUserPublicLayouts = false;
1842    
1843                    if (PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED &&
1844                            PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_AUTO_CREATE) {
1845    
1846                            addDefaultUserPublicLayouts = true;
1847    
1848                            if (PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_POWER_USER_REQUIRED) {
1849                                    if (hasPowerUserRole == null) {
1850                                            hasPowerUserRole = hasPowerUserRole(user);
1851                                    }
1852    
1853                                    if (!hasPowerUserRole.booleanValue()) {
1854                                            addDefaultUserPublicLayouts = false;
1855                                    }
1856                            }
1857                    }
1858    
1859                    if (addDefaultUserPublicLayouts && !user.hasPublicLayouts()) {
1860                            addDefaultUserPublicLayouts(user);
1861                    }
1862    
1863                    boolean deleteDefaultUserPublicLayouts = false;
1864    
1865                    if (!PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED) {
1866                            deleteDefaultUserPublicLayouts = true;
1867                    }
1868                    else if (PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_POWER_USER_REQUIRED) {
1869                            if (hasPowerUserRole == null) {
1870                                    hasPowerUserRole = hasPowerUserRole(user);
1871                            }
1872    
1873                            if (!hasPowerUserRole.booleanValue()) {
1874                                    deleteDefaultUserPublicLayouts = true;
1875                            }
1876                    }
1877    
1878                    if (deleteDefaultUserPublicLayouts && user.hasPublicLayouts()) {
1879                            deleteDefaultUserPublicLayouts(user);
1880                    }
1881            }
1882    
1883            protected File privateLARFile;
1884            protected File publicLARFile;
1885    
1886            private static final String _PATH_PORTAL_LAYOUT = "/portal/layout";
1887    
1888            private static Log _log = LogFactoryUtil.getLog(ServicePreAction.class);
1889    
1890    }