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