001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.portlet.LiferayPortletMode;
020    import com.liferay.portal.kernel.util.JavaConstants;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.model.Layout;
023    import com.liferay.portal.model.LayoutConstants;
024    import com.liferay.portal.model.LayoutTypePortlet;
025    import com.liferay.portal.model.Portlet;
026    import com.liferay.portal.model.PortletConstants;
027    import com.liferay.portal.model.PortletPreferencesIds;
028    import com.liferay.portal.security.auth.PrincipalException;
029    import com.liferay.portal.security.permission.ActionKeys;
030    import com.liferay.portal.security.permission.PermissionChecker;
031    import com.liferay.portal.security.permission.PermissionThreadLocal;
032    import com.liferay.portal.service.PortletLocalServiceUtil;
033    import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
034    import com.liferay.portal.service.UserLocalServiceUtil;
035    import com.liferay.portal.service.permission.LayoutPermissionUtil;
036    import com.liferay.portal.theme.ThemeDisplay;
037    import com.liferay.portal.util.PortalUtil;
038    import com.liferay.portal.util.PortletKeys;
039    import com.liferay.portal.util.WebKeys;
040    
041    import javax.portlet.PortletPreferences;
042    import javax.portlet.PortletRequest;
043    import javax.portlet.PreferencesValidator;
044    
045    import javax.servlet.http.HttpServletRequest;
046    import javax.servlet.http.HttpSession;
047    
048    /**
049     * @author Brian Wing Shun Chan
050     */
051    public class PortletPreferencesFactoryImpl
052            implements PortletPreferencesFactory {
053    
054            public PortletPreferences getLayoutPortletSetup(
055                            Layout layout, String portletId)
056                    throws SystemException {
057    
058                    long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
059                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
060    
061                    return PortletPreferencesLocalServiceUtil.getPreferences(
062                            layout.getCompanyId(), ownerId, ownerType, layout.getPlid(),
063                            portletId);
064            }
065    
066            public PortalPreferences getPortalPreferences(HttpServletRequest request)
067                    throws SystemException {
068    
069                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
070                            WebKeys.THEME_DISPLAY);
071    
072                    long ownerId = themeDisplay.getUserId();
073                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
074                    long plid = PortletKeys.PREFS_PLID_SHARED;
075                    String portletId = PortletKeys.LIFERAY_PORTAL;
076    
077                    PortalPreferences portalPrefs = null;
078    
079                    if (themeDisplay.isSignedIn()) {
080                            PortletPreferencesImpl preferencesImpl = (PortletPreferencesImpl)
081                                    PortletPreferencesLocalServiceUtil.getPreferences(
082                                            themeDisplay.getCompanyId(), ownerId, ownerType, plid,
083                                            portletId);
084    
085                            portalPrefs = new PortalPreferencesImpl(
086                                    preferencesImpl, themeDisplay.isSignedIn());
087                    }
088                    else {
089                            HttpSession session = request.getSession();
090    
091                            portalPrefs = (PortalPreferences)session.getAttribute(
092                                    WebKeys.PORTAL_PREFERENCES);
093    
094                            if (portalPrefs == null) {
095                                    PortletPreferencesImpl preferencesImpl =
096                                            (PortletPreferencesImpl)
097                                                    PortletPreferencesLocalServiceUtil.getPreferences(
098                                                            themeDisplay.getCompanyId(), ownerId, ownerType,
099                                                            plid, portletId);
100    
101                                    preferencesImpl =
102                                            (PortletPreferencesImpl)preferencesImpl.clone();
103    
104                                    portalPrefs = new PortalPreferencesImpl(
105                                            preferencesImpl, themeDisplay.isSignedIn());
106    
107                                    session.setAttribute(WebKeys.PORTAL_PREFERENCES, portalPrefs);
108                            }
109                    }
110    
111                    return portalPrefs;
112            }
113    
114            public PortalPreferences getPortalPreferences(PortletRequest portletRequest)
115                    throws SystemException {
116    
117                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
118                            portletRequest);
119    
120                    return getPortalPreferences(request);
121            }
122    
123            public PortletPreferences getPortletPreferences(
124                            HttpServletRequest request, String portletId)
125                    throws PortalException, SystemException {
126    
127                    PortletPreferencesIds portletPreferencesIds = getPortletPreferencesIds(
128                            request, portletId);
129    
130                    return PortletPreferencesLocalServiceUtil.getPreferences(
131                            portletPreferencesIds);
132            }
133    
134            public PortletPreferencesIds getPortletPreferencesIds(
135                            HttpServletRequest request, String portletId)
136                    throws PortalException, SystemException {
137    
138                    Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
139    
140                    return getPortletPreferencesIds(request, layout, portletId);
141            }
142    
143            public PortletPreferencesIds getPortletPreferencesIds(
144                            HttpServletRequest request, Layout selLayout, String portletId)
145                    throws PortalException, SystemException {
146    
147                    // Below is a list of  the possible combinations, where we specify the
148                    // the owner id, the layout id, portlet id, and the function.
149    
150                    // liferay.com.1, SHARED, PORTAL, preference is scoped per user across
151                    // the entire portal
152    
153                    // COMPANY.liferay.com, SHARED, 56_INSTANCE_abcd, preference is scoped
154                    // per portlet and company and is shared across all layouts
155    
156                    // GROUP.10, SHARED, 56_INSTANCE_abcd, preference is scoped per portlet
157                    // and group and is shared across all layouts
158    
159                    // USER.liferay.com.1, SHARED, 56_INSTANCE_abcd, preference is scoped
160                    // per portlet and user and is shared across all layouts
161    
162                    // PUB.10, 3, 56_INSTANCE_abcd, preference is scoped per portlet, group,
163                    // and layout
164    
165                    // PUB.10.USER.liferay.com.1, 3, 56_INSTANCE_abcd, preference is scoped
166                    // per portlet, user, and layout
167    
168                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
169                            WebKeys.THEME_DISPLAY);
170    
171                    Layout layout = themeDisplay.getLayout();
172                    LayoutTypePortlet layoutTypePortlet =
173                            themeDisplay.getLayoutTypePortlet();
174                    PermissionChecker permissionChecker =
175                            PermissionThreadLocal.getPermissionChecker();
176    
177                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
178                            themeDisplay.getCompanyId(), portletId);
179    
180                    long ownerId = 0;
181                    int ownerType = 0;
182                    long plid = 0;
183    
184                    boolean modeEditGuest = false;
185    
186                    String portletMode = ParamUtil.getString(request, "p_p_mode");
187    
188                    if (portletMode.equals(LiferayPortletMode.EDIT_GUEST.toString()) ||
189                            ((layoutTypePortlet != null) &&
190                             (layoutTypePortlet.hasModeEditGuestPortletId(portletId)))) {
191    
192                            modeEditGuest = true;
193                    }
194    
195                    if (modeEditGuest) {
196                            boolean hasUpdateLayoutPermission = LayoutPermissionUtil.contains(
197                                    permissionChecker, layout, ActionKeys.UPDATE);
198    
199                            if (!layout.isPrivateLayout() && hasUpdateLayoutPermission) {
200                            }
201                            else {
202    
203                                    // Only users with the correct permissions can update guest
204                                    // preferences
205    
206                                    throw new PrincipalException();
207                            }
208                    }
209    
210                    if (portlet.isPreferencesCompanyWide()) {
211                            ownerId = themeDisplay.getCompanyId();
212                            ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
213                            plid = PortletKeys.PREFS_PLID_SHARED;
214                            portletId = PortletConstants.getRootPortletId(portletId);
215                    }
216                    else {
217                            if (portlet.isPreferencesUniquePerLayout()) {
218                                    ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
219                                    ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
220                                    plid = selLayout.getPlid();
221    
222                                    if (portlet.isPreferencesOwnedByGroup()) {
223                                    }
224                                    else {
225                                            long userId = PortalUtil.getUserId(request);
226    
227                                            if ((userId <= 0) || modeEditGuest) {
228                                                    userId = UserLocalServiceUtil.getDefaultUserId(
229                                                            themeDisplay.getCompanyId());
230                                            }
231    
232                                            ownerId = userId;
233                                            ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
234                                    }
235                            }
236                            else {
237                                    plid = PortletKeys.PREFS_PLID_SHARED;
238    
239                                    if (portlet.isPreferencesOwnedByGroup()) {
240                                            ownerId = themeDisplay.getScopeGroupId();
241                                            ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
242                                            portletId = PortletConstants.getRootPortletId(portletId);
243                                    }
244                                    else {
245                                            long userId = PortalUtil.getUserId(request);
246    
247                                            if ((userId <= 0) || modeEditGuest) {
248                                                    userId = UserLocalServiceUtil.getDefaultUserId(
249                                                            themeDisplay.getCompanyId());
250                                            }
251    
252                                            ownerId = userId;
253                                            ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
254                                    }
255                            }
256                    }
257    
258                    return new PortletPreferencesIds(
259                            themeDisplay.getCompanyId(), ownerId, ownerType, plid, portletId);
260            }
261    
262            public PortletPreferences getPortletSetup(
263                            Layout layout, String portletId, String defaultPreferences)
264                    throws SystemException {
265    
266                    return getPortletSetup(
267                            LayoutConstants.DEFAULT_PLID, layout, portletId,
268                            defaultPreferences);
269            }
270    
271            public PortletPreferences getPortletSetup(
272                            HttpServletRequest request, String portletId)
273                    throws SystemException {
274    
275                    return getPortletSetup(request, portletId, null);
276            }
277    
278            public PortletPreferences getPortletSetup(
279                            HttpServletRequest request, String portletId,
280                            String defaultPreferences)
281                    throws SystemException {
282    
283                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
284                            WebKeys.THEME_DISPLAY);
285    
286                    return getPortletSetup(
287                            themeDisplay.getScopeGroupId(), themeDisplay.getLayout(), portletId,
288                            defaultPreferences);
289            }
290    
291            public PortletPreferences getPortletSetup(PortletRequest portletRequest)
292                    throws SystemException {
293    
294                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
295                            portletRequest);
296                    String portletId = PortalUtil.getPortletId(portletRequest);
297    
298                    return getPortletSetup(request, portletId);
299            }
300    
301            public PortletPreferences getPortletSetup(
302                            PortletRequest portletRequest, String portletId)
303                    throws SystemException {
304    
305                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
306                            portletRequest);
307    
308                    return getPortletSetup(request, portletId);
309            }
310    
311            public PortletPreferences getPreferences(HttpServletRequest request) {
312                    PortletRequest portletRequest = (PortletRequest)request.getAttribute(
313                            JavaConstants.JAVAX_PORTLET_REQUEST);
314    
315                    PortletPreferences preferences = null;
316    
317                    if (portletRequest != null) {
318                            PortletPreferencesWrapper preferencesWrapper =
319                                    (PortletPreferencesWrapper)portletRequest.getPreferences();
320    
321                            preferences = preferencesWrapper.getPreferencesImpl();
322                    }
323    
324                    return preferences;
325            }
326    
327            public PreferencesValidator getPreferencesValidator(Portlet portlet) {
328                    return PortalUtil.getPreferencesValidator(portlet);
329            }
330    
331            protected PortletPreferences getPortletSetup(
332                            long scopeGroupId, Layout layout, String portletId,
333                            String defaultPreferences)
334                    throws SystemException {
335    
336                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
337                            layout.getCompanyId(), portletId);
338    
339                    boolean uniquePerLayout = false;
340                    boolean uniquePerGroup = false;
341    
342                    if (portlet.isPreferencesCompanyWide()) {
343                            portletId = PortletConstants.getRootPortletId(portletId);
344                    }
345                    else {
346                            if (portlet.isPreferencesUniquePerLayout()) {
347                                    uniquePerLayout = true;
348    
349                                    if (portlet.isPreferencesOwnedByGroup()) {
350                                            uniquePerGroup = true;
351                                    }
352                            }
353                            else {
354                                    if (portlet.isPreferencesOwnedByGroup()) {
355                                            uniquePerGroup = true;
356                                            portletId = PortletConstants.getRootPortletId(portletId);
357                                    }
358                            }
359                    }
360    
361                    long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
362                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
363                    long plid = layout.getPlid();
364    
365                    if (!uniquePerLayout) {
366                            plid = PortletKeys.PREFS_PLID_SHARED;
367    
368                            if (uniquePerGroup) {
369                                    if (scopeGroupId > LayoutConstants.DEFAULT_PLID) {
370                                            ownerId = scopeGroupId;
371                                    }
372                                    else {
373                                            ownerId = layout.getGroupId();
374                                    }
375    
376                                    ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
377                            }
378                            else {
379                                    ownerId = layout.getCompanyId();
380                                    ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
381                            }
382                    }
383    
384                    return PortletPreferencesLocalServiceUtil.getPreferences(
385                            layout.getCompanyId(), ownerId, ownerType, plid, portletId,
386                            defaultPreferences);
387            }
388    
389    }