1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.portlet.LiferayPortletMode;
28  import com.liferay.portal.kernel.util.InstancePool;
29  import com.liferay.portal.kernel.util.JavaConstants;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.model.Layout;
33  import com.liferay.portal.model.LayoutTypePortlet;
34  import com.liferay.portal.model.Portlet;
35  import com.liferay.portal.model.PortletApp;
36  import com.liferay.portal.model.PortletConstants;
37  import com.liferay.portal.model.PortletPreferencesIds;
38  import com.liferay.portal.security.auth.PrincipalException;
39  import com.liferay.portal.security.permission.ActionKeys;
40  import com.liferay.portal.security.permission.PermissionChecker;
41  import com.liferay.portal.security.permission.PermissionThreadLocal;
42  import com.liferay.portal.service.PortletLocalServiceUtil;
43  import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
44  import com.liferay.portal.service.UserLocalServiceUtil;
45  import com.liferay.portal.service.permission.LayoutPermissionUtil;
46  import com.liferay.portal.theme.ThemeDisplay;
47  import com.liferay.portal.util.PortalUtil;
48  import com.liferay.portal.util.PortletKeys;
49  import com.liferay.portal.util.WebKeys;
50  
51  import javax.portlet.ActionRequest;
52  import javax.portlet.PortletPreferences;
53  import javax.portlet.PortletRequest;
54  import javax.portlet.PreferencesValidator;
55  import javax.portlet.RenderRequest;
56  
57  import javax.servlet.http.HttpServletRequest;
58  import javax.servlet.http.HttpSession;
59  
60  /**
61   * <a href="PortletPreferencesFactoryImpl.java.html"><b><i>View Source</i></b>
62   * </a>
63   *
64   * @author Brian Wing Shun Chan
65   *
66   */
67  public class PortletPreferencesFactoryImpl
68      implements PortletPreferencesFactory {
69  
70      public PortletPreferences getLayoutPortletSetup(
71              Layout layout, String portletId)
72          throws PortalException, SystemException {
73  
74          long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
75          int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
76  
77          return PortletPreferencesLocalServiceUtil.getPreferences(
78              layout.getCompanyId(), ownerId, ownerType, layout.getPlid(),
79              portletId);
80      }
81  
82      public PortalPreferences getPortalPreferences(HttpServletRequest request)
83          throws PortalException, SystemException {
84  
85          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
86              WebKeys.THEME_DISPLAY);
87  
88          long ownerId = themeDisplay.getUserId();
89          int ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
90          long plid = PortletKeys.PREFS_PLID_SHARED;
91          String portletId = PortletKeys.LIFERAY_PORTAL;
92  
93          PortalPreferences portalPrefs = null;
94  
95          if (themeDisplay.isSignedIn()) {
96              PortletPreferencesImpl prefsImpl = (PortletPreferencesImpl)
97                  PortletPreferencesLocalServiceUtil.getPreferences(
98                      themeDisplay.getCompanyId(), ownerId, ownerType, plid,
99                      portletId);
100 
101             portalPrefs = new PortalPreferencesImpl(
102                 prefsImpl, themeDisplay.isSignedIn());
103         }
104         else {
105             HttpSession session = request.getSession();
106 
107             portalPrefs = (PortalPreferences)session.getAttribute(
108                 WebKeys.PORTAL_PREFERENCES);
109 
110             if (portalPrefs == null) {
111                 PortletPreferencesImpl prefsImpl = (PortletPreferencesImpl)
112                     PortletPreferencesLocalServiceUtil.getPreferences(
113                         themeDisplay.getCompanyId(), ownerId, ownerType, plid,
114                         portletId);
115 
116                 prefsImpl = (PortletPreferencesImpl)prefsImpl.clone();
117 
118                 portalPrefs = new PortalPreferencesImpl(
119                     prefsImpl, themeDisplay.isSignedIn());
120 
121                 session.setAttribute(WebKeys.PORTAL_PREFERENCES, portalPrefs);
122             }
123         }
124 
125         return portalPrefs;
126     }
127 
128     public PortalPreferences getPortalPreferences(ActionRequest actionRequest)
129         throws PortalException, SystemException {
130 
131         HttpServletRequest request = PortalUtil.getHttpServletRequest(
132             actionRequest);
133 
134         return getPortalPreferences(request);
135     }
136 
137     public PortalPreferences getPortalPreferences(RenderRequest renderRequest)
138         throws PortalException, SystemException {
139 
140         HttpServletRequest request = PortalUtil.getHttpServletRequest(
141             renderRequest);
142 
143         return getPortalPreferences(request);
144     }
145 
146     public PortletPreferences getPortletPreferences(
147             HttpServletRequest request, String portletId)
148         throws PortalException, SystemException {
149 
150         PortletPreferencesIds portletPreferencesIds = getPortletPreferencesIds(
151             request, portletId);
152 
153         return PortletPreferencesLocalServiceUtil.getPreferences(
154             portletPreferencesIds);
155     }
156 
157     public PortletPreferencesIds getPortletPreferencesIds(
158             HttpServletRequest request, String portletId)
159         throws PortalException, SystemException {
160 
161         Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
162 
163         return getPortletPreferencesIds(request, layout, portletId);
164     }
165 
166     public PortletPreferencesIds getPortletPreferencesIds(
167             HttpServletRequest request, Layout selLayout, String portletId)
168         throws PortalException, SystemException {
169 
170         // Below is a list of  the possible combinations, where we specify the
171         // the owner id, the layout id, portlet id, and the function.
172 
173         // liferay.com.1, SHARED, PORTAL, preference is scoped per user across
174         // the entire portal
175 
176         // COMPANY.liferay.com, SHARED, 56_INSTANCE_abcd, preference is scoped
177         // per portlet and company and is shared across all layouts
178 
179         // GROUP.10, SHARED, 56_INSTANCE_abcd, preference is scoped per portlet
180         // and group and is shared across all layouts
181 
182         // USER.liferay.com.1, SHARED, 56_INSTANCE_abcd, preference is scoped
183         // per portlet and user and is shared across all layouts
184 
185         // PUB.10, 3, 56_INSTANCE_abcd, preference is scoped per portlet, group,
186         // and layout
187 
188         // PUB.10.USER.liferay.com.1, 3, 56_INSTANCE_abcd, preference is scoped
189         // per portlet, user, and layout
190 
191         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
192             WebKeys.THEME_DISPLAY);
193 
194         Layout layout = themeDisplay.getLayout();
195         LayoutTypePortlet layoutTypePortlet =
196             themeDisplay.getLayoutTypePortlet();
197         PermissionChecker permissionChecker =
198             PermissionThreadLocal.getPermissionChecker();
199 
200         Portlet portlet = PortletLocalServiceUtil.getPortletById(
201             themeDisplay.getCompanyId(), portletId);
202 
203         long ownerId = 0;
204         int ownerType = 0;
205         long plid = 0;
206 
207         boolean modeEditGuest = false;
208 
209         String portletMode = ParamUtil.getString(request, "p_p_mode");
210 
211         if (portletMode.equals(LiferayPortletMode.EDIT_GUEST.toString()) ||
212             layoutTypePortlet.hasModeEditGuestPortletId(portletId)) {
213 
214             modeEditGuest = true;
215         }
216 
217         if (modeEditGuest) {
218             boolean hasUpdateLayoutPermission = LayoutPermissionUtil.contains(
219                 permissionChecker, layout, ActionKeys.UPDATE);
220 
221             if (!layout.isPrivateLayout() && hasUpdateLayoutPermission) {
222             }
223             else {
224 
225                 // Only users with the correct permissions can update guest
226                 // preferences
227 
228                 throw new PrincipalException();
229             }
230         }
231 
232         if (portlet.isPreferencesCompanyWide()) {
233             ownerId = themeDisplay.getCompanyId();
234             ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
235             plid = PortletKeys.PREFS_PLID_SHARED;
236             portletId = PortletConstants.getRootPortletId(portletId);
237         }
238         else {
239             if (portlet.isPreferencesUniquePerLayout()) {
240                 ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
241                 ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
242                 plid = selLayout.getPlid();
243 
244                 if (portlet.isPreferencesOwnedByGroup()) {
245                 }
246                 else {
247                     long userId = PortalUtil.getUserId(request);
248 
249                     if ((userId <= 0) || modeEditGuest) {
250                         userId = UserLocalServiceUtil.getDefaultUserId(
251                             themeDisplay.getCompanyId());
252                     }
253 
254                     ownerId = userId;
255                     ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
256                 }
257             }
258             else {
259                 plid = PortletKeys.PREFS_PLID_SHARED;
260 
261                 if (portlet.isPreferencesOwnedByGroup()) {
262                     ownerId = selLayout.getGroupId();
263                     ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
264                     portletId = PortletConstants.getRootPortletId(portletId);
265                 }
266                 else {
267                     long userId = PortalUtil.getUserId(request);
268 
269                     if ((userId <= 0) || modeEditGuest) {
270                         userId = UserLocalServiceUtil.getDefaultUserId(
271                             themeDisplay.getCompanyId());
272                     }
273 
274                     ownerId = userId;
275                     ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
276                 }
277             }
278         }
279 
280         return new PortletPreferencesIds(
281             themeDisplay.getCompanyId(), ownerId, ownerType, plid, portletId);
282     }
283 
284     public PortletPreferences getPortletSetup(
285             Layout layout, String portletId, String defaultPreferences)
286         throws PortalException, SystemException {
287 
288         Portlet portlet = PortletLocalServiceUtil.getPortletById(
289             layout.getCompanyId(), portletId);
290 
291         boolean uniquePerLayout = false;
292         boolean uniquePerGroup = false;
293 
294         if (portlet.isPreferencesCompanyWide()) {
295             portletId = PortletConstants.getRootPortletId(portletId);
296         }
297         else {
298             if (portlet.isPreferencesUniquePerLayout()) {
299                 uniquePerLayout = true;
300 
301                 if (portlet.isPreferencesOwnedByGroup()) {
302                     uniquePerGroup = true;
303                 }
304             }
305             else {
306                 if (portlet.isPreferencesOwnedByGroup()) {
307                     uniquePerGroup = true;
308                     portletId = PortletConstants.getRootPortletId(portletId);
309                 }
310             }
311         }
312 
313         long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
314         int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
315         long plid = layout.getPlid();
316 
317         if (!uniquePerLayout) {
318             plid = PortletKeys.PREFS_PLID_SHARED;
319 
320             if (uniquePerGroup) {
321                 ownerId = layout.getGroupId();
322                 ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
323             }
324             else {
325                 ownerId = layout.getCompanyId();
326                 ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
327             }
328         }
329 
330         return PortletPreferencesLocalServiceUtil.getPreferences(
331             layout.getCompanyId(), ownerId, ownerType, plid, portletId,
332             defaultPreferences);
333     }
334 
335     public PortletPreferences getPortletSetup(
336             HttpServletRequest request, String portletId)
337         throws PortalException, SystemException {
338 
339         return getPortletSetup(request, portletId, null);
340     }
341 
342     public PortletPreferences getPortletSetup(
343             HttpServletRequest request, String portletId,
344             String defaultPreferences)
345         throws PortalException, SystemException {
346 
347         Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
348 
349         return getPortletSetup(layout, portletId, defaultPreferences);
350     }
351 
352     public PortletPreferences getPortletSetup(ActionRequest actionRequest)
353         throws PortalException, SystemException {
354 
355         HttpServletRequest request = PortalUtil.getHttpServletRequest(
356             actionRequest);
357         String portletId = PortalUtil.getPortletId(actionRequest);
358 
359         return getPortletSetup(request, portletId);
360     }
361 
362     public PortletPreferences getPortletSetup(
363             ActionRequest actionRequest, String portletId)
364         throws PortalException, SystemException {
365 
366         HttpServletRequest request = PortalUtil.getHttpServletRequest(
367             actionRequest);
368 
369         return getPortletSetup(request, portletId);
370     }
371 
372     public PortletPreferences getPortletSetup(RenderRequest renderRequest)
373         throws PortalException, SystemException {
374 
375         HttpServletRequest request = PortalUtil.getHttpServletRequest(
376             renderRequest);
377         String portletId = PortalUtil.getPortletId(renderRequest);
378 
379         return getPortletSetup(request, portletId);
380     }
381 
382     public PortletPreferences getPortletSetup(
383             RenderRequest renderRequest, String portletId)
384         throws PortalException, SystemException {
385 
386         HttpServletRequest request = PortalUtil.getHttpServletRequest(
387             renderRequest);
388 
389         return getPortletSetup(request, portletId);
390     }
391 
392     public PortletPreferences getPreferences(HttpServletRequest request) {
393         PortletRequest portletRequest = (PortletRequest)request.getAttribute(
394             JavaConstants.JAVAX_PORTLET_REQUEST);
395 
396         PortletPreferences prefs = null;
397 
398         if (portletRequest != null) {
399             PortletPreferencesWrapper prefsWrapper =
400                 (PortletPreferencesWrapper)portletRequest.getPreferences();
401 
402             prefs = prefsWrapper.getPreferencesImpl();
403         }
404 
405         return prefs;
406     }
407 
408     public PreferencesValidator getPreferencesValidator(Portlet portlet) {
409         PortletApp portletApp = portlet.getPortletApp();
410 
411         if (portletApp.isWARFile()) {
412             PortletBag portletBag = PortletBagPool.get(
413                 portlet.getRootPortletId());
414 
415             return portletBag.getPreferencesValidatorInstance();
416         }
417         else {
418             PreferencesValidator prefsValidator = null;
419 
420             if (Validator.isNotNull(portlet.getPreferencesValidator())) {
421                 prefsValidator =
422                     (PreferencesValidator)InstancePool.get(
423                         portlet.getPreferencesValidator());
424             }
425 
426             return prefsValidator;
427         }
428     }
429 
430 }