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