1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portlet;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.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                     long scopeGroupId = PortalUtil.getScopeGroupId(
244                         layout, portletId);
245 
246                     ownerId = scopeGroupId;
247                     ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
248                     portletId = PortletConstants.getRootPortletId(portletId);
249                 }
250                 else {
251                     long userId = PortalUtil.getUserId(request);
252 
253                     if ((userId <= 0) || modeEditGuest) {
254                         userId = UserLocalServiceUtil.getDefaultUserId(
255                             themeDisplay.getCompanyId());
256                     }
257 
258                     ownerId = userId;
259                     ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
260                 }
261             }
262         }
263 
264         return new PortletPreferencesIds(
265             themeDisplay.getCompanyId(), ownerId, ownerType, plid, portletId);
266     }
267 
268     public PortletPreferences getPortletSetup(
269             Layout layout, String portletId, String defaultPreferences)
270         throws SystemException {
271 
272         return getPortletSetup(
273             LayoutConstants.DEFAULT_PLID, layout, portletId,
274             defaultPreferences);
275     }
276 
277     public PortletPreferences getPortletSetup(
278             HttpServletRequest request, String portletId)
279         throws SystemException {
280 
281         return getPortletSetup(request, portletId, null);
282     }
283 
284     public PortletPreferences getPortletSetup(
285             HttpServletRequest request, String portletId,
286             String defaultPreferences)
287         throws SystemException {
288 
289         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
290             WebKeys.THEME_DISPLAY);
291 
292         long scopeGroupId = PortalUtil.getScopeGroupId(request, portletId);
293 
294         return getPortletSetup(
295             scopeGroupId, themeDisplay.getLayout(), portletId,
296             defaultPreferences);
297     }
298 
299     public PortletPreferences getPortletSetup(PortletRequest portletRequest)
300         throws SystemException {
301 
302         HttpServletRequest request = PortalUtil.getHttpServletRequest(
303             portletRequest);
304         String portletId = PortalUtil.getPortletId(portletRequest);
305 
306         return getPortletSetup(request, portletId);
307     }
308 
309     public PortletPreferences getPortletSetup(
310             PortletRequest portletRequest, String portletId)
311         throws SystemException {
312 
313         HttpServletRequest request = PortalUtil.getHttpServletRequest(
314             portletRequest);
315 
316         return getPortletSetup(request, portletId);
317     }
318 
319     public PortletPreferences getPreferences(HttpServletRequest request) {
320         PortletRequest portletRequest = (PortletRequest)request.getAttribute(
321             JavaConstants.JAVAX_PORTLET_REQUEST);
322 
323         PortletPreferences preferences = null;
324 
325         if (portletRequest != null) {
326             PortletPreferencesWrapper preferencesWrapper =
327                 (PortletPreferencesWrapper)portletRequest.getPreferences();
328 
329             preferences = preferencesWrapper.getPreferencesImpl();
330         }
331 
332         return preferences;
333     }
334 
335     public PreferencesValidator getPreferencesValidator(Portlet portlet) {
336         return PortalUtil.getPreferencesValidator(portlet);
337     }
338 
339     protected PortletPreferences getPortletSetup(
340             long scopeGroupId, Layout layout, String portletId,
341             String defaultPreferences)
342         throws SystemException {
343 
344         Portlet portlet = PortletLocalServiceUtil.getPortletById(
345             layout.getCompanyId(), portletId);
346 
347         boolean uniquePerLayout = false;
348         boolean uniquePerGroup = false;
349 
350         if (portlet.isPreferencesCompanyWide()) {
351             portletId = PortletConstants.getRootPortletId(portletId);
352         }
353         else {
354             if (portlet.isPreferencesUniquePerLayout()) {
355                 uniquePerLayout = true;
356 
357                 if (portlet.isPreferencesOwnedByGroup()) {
358                     uniquePerGroup = true;
359                 }
360             }
361             else {
362                 if (portlet.isPreferencesOwnedByGroup()) {
363                     uniquePerGroup = true;
364                     portletId = PortletConstants.getRootPortletId(portletId);
365                 }
366             }
367         }
368 
369         long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
370         int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
371         long plid = layout.getPlid();
372 
373         if (!uniquePerLayout) {
374             plid = PortletKeys.PREFS_PLID_SHARED;
375 
376             if (uniquePerGroup) {
377                 if (scopeGroupId > LayoutConstants.DEFAULT_PLID) {
378                     ownerId = scopeGroupId;
379                 }
380                 else {
381                     ownerId = layout.getGroupId();
382                 }
383 
384                 ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
385             }
386             else {
387                 ownerId = layout.getCompanyId();
388                 ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
389             }
390         }
391 
392         return PortletPreferencesLocalServiceUtil.getPreferences(
393             layout.getCompanyId(), ownerId, ownerType, plid, portletId,
394             defaultPreferences);
395     }
396 
397 }