1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.portlet.LiferayPortletMode;
20  import com.liferay.portal.kernel.util.JavaConstants;
21  import com.liferay.portal.kernel.util.ParamUtil;
22  import com.liferay.portal.model.Layout;
23  import com.liferay.portal.model.LayoutConstants;
24  import com.liferay.portal.model.LayoutTypePortlet;
25  import com.liferay.portal.model.Portlet;
26  import com.liferay.portal.model.PortletConstants;
27  import com.liferay.portal.model.PortletPreferencesIds;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.security.permission.ActionKeys;
30  import com.liferay.portal.security.permission.PermissionChecker;
31  import com.liferay.portal.security.permission.PermissionThreadLocal;
32  import com.liferay.portal.service.PortletLocalServiceUtil;
33  import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
34  import com.liferay.portal.service.UserLocalServiceUtil;
35  import com.liferay.portal.service.permission.LayoutPermissionUtil;
36  import com.liferay.portal.theme.ThemeDisplay;
37  import com.liferay.portal.util.PortalUtil;
38  import com.liferay.portal.util.PortletKeys;
39  import com.liferay.portal.util.WebKeys;
40  
41  import javax.portlet.PortletPreferences;
42  import javax.portlet.PortletRequest;
43  import javax.portlet.PreferencesValidator;
44  
45  import javax.servlet.http.HttpServletRequest;
46  import javax.servlet.http.HttpSession;
47  
48  /**
49   * <a href="PortletPreferencesFactoryImpl.java.html"><b><i>View Source</i></b>
50   * </a>
51   *
52   * @author Brian Wing Shun Chan
53   */
54  public class PortletPreferencesFactoryImpl
55      implements PortletPreferencesFactory {
56  
57      public PortletPreferences getLayoutPortletSetup(
58              Layout layout, String portletId)
59          throws SystemException {
60  
61          long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
62          int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
63  
64          return PortletPreferencesLocalServiceUtil.getPreferences(
65              layout.getCompanyId(), ownerId, ownerType, layout.getPlid(),
66              portletId);
67      }
68  
69      public PortalPreferences getPortalPreferences(HttpServletRequest request)
70          throws SystemException {
71  
72          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
73              WebKeys.THEME_DISPLAY);
74  
75          long ownerId = themeDisplay.getUserId();
76          int ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
77          long plid = PortletKeys.PREFS_PLID_SHARED;
78          String portletId = PortletKeys.LIFERAY_PORTAL;
79  
80          PortalPreferences portalPrefs = null;
81  
82          if (themeDisplay.isSignedIn()) {
83              PortletPreferencesImpl preferencesImpl = (PortletPreferencesImpl)
84                  PortletPreferencesLocalServiceUtil.getPreferences(
85                      themeDisplay.getCompanyId(), ownerId, ownerType, plid,
86                      portletId);
87  
88              portalPrefs = new PortalPreferencesImpl(
89                  preferencesImpl, themeDisplay.isSignedIn());
90          }
91          else {
92              HttpSession session = request.getSession();
93  
94              portalPrefs = (PortalPreferences)session.getAttribute(
95                  WebKeys.PORTAL_PREFERENCES);
96  
97              if (portalPrefs == null) {
98                  PortletPreferencesImpl preferencesImpl =
99                      (PortletPreferencesImpl)
100                         PortletPreferencesLocalServiceUtil.getPreferences(
101                             themeDisplay.getCompanyId(), ownerId, ownerType,
102                             plid, portletId);
103 
104                 preferencesImpl =
105                     (PortletPreferencesImpl)preferencesImpl.clone();
106 
107                 portalPrefs = new PortalPreferencesImpl(
108                     preferencesImpl, themeDisplay.isSignedIn());
109 
110                 session.setAttribute(WebKeys.PORTAL_PREFERENCES, portalPrefs);
111             }
112         }
113 
114         return portalPrefs;
115     }
116 
117     public PortalPreferences getPortalPreferences(PortletRequest portletRequest)
118         throws SystemException {
119 
120         HttpServletRequest request = PortalUtil.getHttpServletRequest(
121             portletRequest);
122 
123         return getPortalPreferences(request);
124     }
125 
126     public PortletPreferences getPortletPreferences(
127             HttpServletRequest request, String portletId)
128         throws PortalException, SystemException {
129 
130         PortletPreferencesIds portletPreferencesIds = getPortletPreferencesIds(
131             request, portletId);
132 
133         return PortletPreferencesLocalServiceUtil.getPreferences(
134             portletPreferencesIds);
135     }
136 
137     public PortletPreferencesIds getPortletPreferencesIds(
138             HttpServletRequest request, String portletId)
139         throws PortalException, SystemException {
140 
141         Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
142 
143         return getPortletPreferencesIds(request, layout, portletId);
144     }
145 
146     public PortletPreferencesIds getPortletPreferencesIds(
147             HttpServletRequest request, Layout selLayout, String portletId)
148         throws PortalException, SystemException {
149 
150         // Below is a list of  the possible combinations, where we specify the
151         // the owner id, the layout id, portlet id, and the function.
152 
153         // liferay.com.1, SHARED, PORTAL, preference is scoped per user across
154         // the entire portal
155 
156         // COMPANY.liferay.com, SHARED, 56_INSTANCE_abcd, preference is scoped
157         // per portlet and company and is shared across all layouts
158 
159         // GROUP.10, SHARED, 56_INSTANCE_abcd, preference is scoped per portlet
160         // and group and is shared across all layouts
161 
162         // USER.liferay.com.1, SHARED, 56_INSTANCE_abcd, preference is scoped
163         // per portlet and user and is shared across all layouts
164 
165         // PUB.10, 3, 56_INSTANCE_abcd, preference is scoped per portlet, group,
166         // and layout
167 
168         // PUB.10.USER.liferay.com.1, 3, 56_INSTANCE_abcd, preference is scoped
169         // per portlet, user, and layout
170 
171         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
172             WebKeys.THEME_DISPLAY);
173 
174         Layout layout = themeDisplay.getLayout();
175         LayoutTypePortlet layoutTypePortlet =
176             themeDisplay.getLayoutTypePortlet();
177         PermissionChecker permissionChecker =
178             PermissionThreadLocal.getPermissionChecker();
179 
180         Portlet portlet = PortletLocalServiceUtil.getPortletById(
181             themeDisplay.getCompanyId(), portletId);
182 
183         long ownerId = 0;
184         int ownerType = 0;
185         long plid = 0;
186 
187         boolean modeEditGuest = false;
188 
189         String portletMode = ParamUtil.getString(request, "p_p_mode");
190 
191         if (portletMode.equals(LiferayPortletMode.EDIT_GUEST.toString()) ||
192             ((layoutTypePortlet != null) &&
193              (layoutTypePortlet.hasModeEditGuestPortletId(portletId)))) {
194 
195             modeEditGuest = true;
196         }
197 
198         if (modeEditGuest) {
199             boolean hasUpdateLayoutPermission = LayoutPermissionUtil.contains(
200                 permissionChecker, layout, ActionKeys.UPDATE);
201 
202             if (!layout.isPrivateLayout() && hasUpdateLayoutPermission) {
203             }
204             else {
205 
206                 // Only users with the correct permissions can update guest
207                 // preferences
208 
209                 throw new PrincipalException();
210             }
211         }
212 
213         if (portlet.isPreferencesCompanyWide()) {
214             ownerId = themeDisplay.getCompanyId();
215             ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
216             plid = PortletKeys.PREFS_PLID_SHARED;
217             portletId = PortletConstants.getRootPortletId(portletId);
218         }
219         else {
220             if (portlet.isPreferencesUniquePerLayout()) {
221                 ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
222                 ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
223                 plid = selLayout.getPlid();
224 
225                 if (portlet.isPreferencesOwnedByGroup()) {
226                 }
227                 else {
228                     long userId = PortalUtil.getUserId(request);
229 
230                     if ((userId <= 0) || modeEditGuest) {
231                         userId = UserLocalServiceUtil.getDefaultUserId(
232                             themeDisplay.getCompanyId());
233                     }
234 
235                     ownerId = userId;
236                     ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
237                 }
238             }
239             else {
240                 plid = PortletKeys.PREFS_PLID_SHARED;
241 
242                 if (portlet.isPreferencesOwnedByGroup()) {
243                     ownerId = themeDisplay.getScopeGroupId();
244                     ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
245                     portletId = PortletConstants.getRootPortletId(portletId);
246                 }
247                 else {
248                     long userId = PortalUtil.getUserId(request);
249 
250                     if ((userId <= 0) || modeEditGuest) {
251                         userId = UserLocalServiceUtil.getDefaultUserId(
252                             themeDisplay.getCompanyId());
253                     }
254 
255                     ownerId = userId;
256                     ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
257                 }
258             }
259         }
260 
261         return new PortletPreferencesIds(
262             themeDisplay.getCompanyId(), ownerId, ownerType, plid, portletId);
263     }
264 
265     public PortletPreferences getPortletSetup(
266             Layout layout, String portletId, String defaultPreferences)
267         throws SystemException {
268 
269         return getPortletSetup(
270             LayoutConstants.DEFAULT_PLID, layout, portletId,
271             defaultPreferences);
272     }
273 
274     public PortletPreferences getPortletSetup(
275             HttpServletRequest request, String portletId)
276         throws SystemException {
277 
278         return getPortletSetup(request, portletId, null);
279     }
280 
281     public PortletPreferences getPortletSetup(
282             HttpServletRequest request, String portletId,
283             String defaultPreferences)
284         throws SystemException {
285 
286         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
287             WebKeys.THEME_DISPLAY);
288 
289         return getPortletSetup(
290             themeDisplay.getScopeGroupId(), themeDisplay.getLayout(), portletId,
291             defaultPreferences);
292     }
293 
294     public PortletPreferences getPortletSetup(PortletRequest portletRequest)
295         throws SystemException {
296 
297         HttpServletRequest request = PortalUtil.getHttpServletRequest(
298             portletRequest);
299         String portletId = PortalUtil.getPortletId(portletRequest);
300 
301         return getPortletSetup(request, portletId);
302     }
303 
304     public PortletPreferences getPortletSetup(
305             PortletRequest portletRequest, String portletId)
306         throws SystemException {
307 
308         HttpServletRequest request = PortalUtil.getHttpServletRequest(
309             portletRequest);
310 
311         return getPortletSetup(request, portletId);
312     }
313 
314     public PortletPreferences getPreferences(HttpServletRequest request) {
315         PortletRequest portletRequest = (PortletRequest)request.getAttribute(
316             JavaConstants.JAVAX_PORTLET_REQUEST);
317 
318         PortletPreferences preferences = null;
319 
320         if (portletRequest != null) {
321             PortletPreferencesWrapper preferencesWrapper =
322                 (PortletPreferencesWrapper)portletRequest.getPreferences();
323 
324             preferences = preferencesWrapper.getPreferencesImpl();
325         }
326 
327         return preferences;
328     }
329 
330     public PreferencesValidator getPreferencesValidator(Portlet portlet) {
331         return PortalUtil.getPreferencesValidator(portlet);
332     }
333 
334     protected PortletPreferences getPortletSetup(
335             long scopeGroupId, Layout layout, String portletId,
336             String defaultPreferences)
337         throws SystemException {
338 
339         Portlet portlet = PortletLocalServiceUtil.getPortletById(
340             layout.getCompanyId(), portletId);
341 
342         boolean uniquePerLayout = false;
343         boolean uniquePerGroup = false;
344 
345         if (portlet.isPreferencesCompanyWide()) {
346             portletId = PortletConstants.getRootPortletId(portletId);
347         }
348         else {
349             if (portlet.isPreferencesUniquePerLayout()) {
350                 uniquePerLayout = true;
351 
352                 if (portlet.isPreferencesOwnedByGroup()) {
353                     uniquePerGroup = true;
354                 }
355             }
356             else {
357                 if (portlet.isPreferencesOwnedByGroup()) {
358                     uniquePerGroup = true;
359                     portletId = PortletConstants.getRootPortletId(portletId);
360                 }
361             }
362         }
363 
364         long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
365         int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
366         long plid = layout.getPlid();
367 
368         if (!uniquePerLayout) {
369             plid = PortletKeys.PREFS_PLID_SHARED;
370 
371             if (uniquePerGroup) {
372                 if (scopeGroupId > LayoutConstants.DEFAULT_PLID) {
373                     ownerId = scopeGroupId;
374                 }
375                 else {
376                     ownerId = layout.getGroupId();
377                 }
378 
379                 ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
380             }
381             else {
382                 ownerId = layout.getCompanyId();
383                 ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
384             }
385         }
386 
387         return PortletPreferencesLocalServiceUtil.getPreferences(
388             layout.getCompanyId(), ownerId, ownerType, plid, portletId,
389             defaultPreferences);
390     }
391 
392 }