1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class PortletPreferencesFactoryImpl
68      implements PortletPreferencesFactory {
69  
70      public PortletPreferences getLayoutPortletSetup(
71              Layout layout, String portletId)
72          throws 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 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 preferencesImpl = (PortletPreferencesImpl)
97                  PortletPreferencesLocalServiceUtil.getPreferences(
98                      themeDisplay.getCompanyId(), ownerId, ownerType, plid,
99                      portletId);
100 
101             portalPrefs = new PortalPreferencesImpl(
102                 preferencesImpl, 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 preferencesImpl =
112                     (PortletPreferencesImpl)
113                         PortletPreferencesLocalServiceUtil.getPreferences(
114                             themeDisplay.getCompanyId(), ownerId, ownerType,
115                             plid, portletId);
116 
117                 preferencesImpl =
118                     (PortletPreferencesImpl)preferencesImpl.clone();
119 
120                 portalPrefs = new PortalPreferencesImpl(
121                     preferencesImpl, themeDisplay.isSignedIn());
122 
123                 session.setAttribute(WebKeys.PORTAL_PREFERENCES, portalPrefs);
124             }
125         }
126 
127         return portalPrefs;
128     }
129 
130     public PortalPreferences getPortalPreferences(PortletRequest portletRequest)
131         throws SystemException {
132 
133         HttpServletRequest request = PortalUtil.getHttpServletRequest(
134             portletRequest);
135 
136         return getPortalPreferences(request);
137     }
138 
139     public PortletPreferences getPortletPreferences(
140             HttpServletRequest request, String portletId)
141         throws PortalException, SystemException {
142 
143         PortletPreferencesIds portletPreferencesIds = getPortletPreferencesIds(
144             request, portletId);
145 
146         return PortletPreferencesLocalServiceUtil.getPreferences(
147             portletPreferencesIds);
148     }
149 
150     public PortletPreferencesIds getPortletPreferencesIds(
151             HttpServletRequest request, String portletId)
152         throws PortalException, SystemException {
153 
154         Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
155 
156         return getPortletPreferencesIds(request, layout, portletId);
157     }
158 
159     public PortletPreferencesIds getPortletPreferencesIds(
160             HttpServletRequest request, Layout selLayout, String portletId)
161         throws PortalException, SystemException {
162 
163         // Below is a list of  the possible combinations, where we specify the
164         // the owner id, the layout id, portlet id, and the function.
165 
166         // liferay.com.1, SHARED, PORTAL, preference is scoped per user across
167         // the entire portal
168 
169         // COMPANY.liferay.com, SHARED, 56_INSTANCE_abcd, preference is scoped
170         // per portlet and company and is shared across all layouts
171 
172         // GROUP.10, SHARED, 56_INSTANCE_abcd, preference is scoped per portlet
173         // and group and is shared across all layouts
174 
175         // USER.liferay.com.1, SHARED, 56_INSTANCE_abcd, preference is scoped
176         // per portlet and user and is shared across all layouts
177 
178         // PUB.10, 3, 56_INSTANCE_abcd, preference is scoped per portlet, group,
179         // and layout
180 
181         // PUB.10.USER.liferay.com.1, 3, 56_INSTANCE_abcd, preference is scoped
182         // per portlet, user, and layout
183 
184         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
185             WebKeys.THEME_DISPLAY);
186 
187         Layout layout = themeDisplay.getLayout();
188         LayoutTypePortlet layoutTypePortlet =
189             themeDisplay.getLayoutTypePortlet();
190         PermissionChecker permissionChecker =
191             PermissionThreadLocal.getPermissionChecker();
192 
193         Portlet portlet = PortletLocalServiceUtil.getPortletById(
194             themeDisplay.getCompanyId(), portletId);
195 
196         long ownerId = 0;
197         int ownerType = 0;
198         long plid = 0;
199 
200         boolean modeEditGuest = false;
201 
202         String portletMode = ParamUtil.getString(request, "p_p_mode");
203 
204         if (portletMode.equals(LiferayPortletMode.EDIT_GUEST.toString()) ||
205             ((layoutTypePortlet != null) &&
206              (layoutTypePortlet.hasModeEditGuestPortletId(portletId)))) {
207 
208             modeEditGuest = true;
209         }
210 
211         if (modeEditGuest) {
212             boolean hasUpdateLayoutPermission = LayoutPermissionUtil.contains(
213                 permissionChecker, layout, ActionKeys.UPDATE);
214 
215             if (!layout.isPrivateLayout() && hasUpdateLayoutPermission) {
216             }
217             else {
218 
219                 // Only users with the correct permissions can update guest
220                 // preferences
221 
222                 throw new PrincipalException();
223             }
224         }
225 
226         if (portlet.isPreferencesCompanyWide()) {
227             ownerId = themeDisplay.getCompanyId();
228             ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
229             plid = PortletKeys.PREFS_PLID_SHARED;
230             portletId = PortletConstants.getRootPortletId(portletId);
231         }
232         else {
233             if (portlet.isPreferencesUniquePerLayout()) {
234                 ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
235                 ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
236                 plid = selLayout.getPlid();
237 
238                 if (portlet.isPreferencesOwnedByGroup()) {
239                 }
240                 else {
241                     long userId = PortalUtil.getUserId(request);
242 
243                     if ((userId <= 0) || modeEditGuest) {
244                         userId = UserLocalServiceUtil.getDefaultUserId(
245                             themeDisplay.getCompanyId());
246                     }
247 
248                     ownerId = userId;
249                     ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
250                 }
251             }
252             else {
253                 plid = PortletKeys.PREFS_PLID_SHARED;
254 
255                 if (portlet.isPreferencesOwnedByGroup()) {
256                     ownerId = selLayout.getGroupId();
257                     ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
258                     portletId = PortletConstants.getRootPortletId(portletId);
259                 }
260                 else {
261                     long userId = PortalUtil.getUserId(request);
262 
263                     if ((userId <= 0) || modeEditGuest) {
264                         userId = UserLocalServiceUtil.getDefaultUserId(
265                             themeDisplay.getCompanyId());
266                     }
267 
268                     ownerId = userId;
269                     ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
270                 }
271             }
272         }
273 
274         return new PortletPreferencesIds(
275             themeDisplay.getCompanyId(), ownerId, ownerType, plid, portletId);
276     }
277 
278     public PortletPreferences getPortletSetup(
279             Layout layout, String portletId, String defaultPreferences)
280         throws SystemException {
281 
282         return getPortletSetup(
283             LayoutConstants.DEFAULT_PLID, layout, portletId,
284             defaultPreferences);
285     }
286 
287     public PortletPreferences getPortletSetup(
288             HttpServletRequest request, String portletId)
289         throws SystemException {
290 
291         return getPortletSetup(request, portletId, null);
292     }
293 
294     public PortletPreferences getPortletSetup(
295             HttpServletRequest request, String portletId,
296             String defaultPreferences)
297         throws SystemException {
298 
299         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
300             WebKeys.THEME_DISPLAY);
301 
302         return getPortletSetup(
303             themeDisplay.getScopeGroupId(), themeDisplay.getLayout(), portletId,
304             defaultPreferences);
305     }
306 
307     public PortletPreferences getPortletSetup(PortletRequest portletRequest)
308         throws SystemException {
309 
310         HttpServletRequest request = PortalUtil.getHttpServletRequest(
311             portletRequest);
312         String portletId = PortalUtil.getPortletId(portletRequest);
313 
314         return getPortletSetup(request, portletId);
315     }
316 
317     public PortletPreferences getPortletSetup(
318             PortletRequest portletRequest, String portletId)
319         throws SystemException {
320 
321         HttpServletRequest request = PortalUtil.getHttpServletRequest(
322             portletRequest);
323 
324         return getPortletSetup(request, portletId);
325     }
326 
327     public PortletPreferences getPreferences(HttpServletRequest request) {
328         PortletRequest portletRequest = (PortletRequest)request.getAttribute(
329             JavaConstants.JAVAX_PORTLET_REQUEST);
330 
331         PortletPreferences preferences = null;
332 
333         if (portletRequest != null) {
334             PortletPreferencesWrapper preferencesWrapper =
335                 (PortletPreferencesWrapper)portletRequest.getPreferences();
336 
337             preferences = preferencesWrapper.getPreferencesImpl();
338         }
339 
340         return preferences;
341     }
342 
343     public PreferencesValidator getPreferencesValidator(Portlet portlet) {
344         PortletApp portletApp = portlet.getPortletApp();
345 
346         if (portletApp.isWARFile()) {
347             PortletBag portletBag = PortletBagPool.get(
348                 portlet.getRootPortletId());
349 
350             return portletBag.getPreferencesValidatorInstance();
351         }
352         else {
353             PreferencesValidator preferencesValidator = null;
354 
355             if (Validator.isNotNull(portlet.getPreferencesValidator())) {
356                 preferencesValidator =
357                     (PreferencesValidator)InstancePool.get(
358                         portlet.getPreferencesValidator());
359             }
360 
361             return preferencesValidator;
362         }
363     }
364 
365     protected PortletPreferences getPortletSetup(
366             long scopeGroupId, Layout layout, String portletId,
367             String defaultPreferences)
368         throws SystemException {
369 
370         Portlet portlet = PortletLocalServiceUtil.getPortletById(
371             layout.getCompanyId(), portletId);
372 
373         boolean uniquePerLayout = false;
374         boolean uniquePerGroup = false;
375 
376         if (portlet.isPreferencesCompanyWide()) {
377             portletId = PortletConstants.getRootPortletId(portletId);
378         }
379         else {
380             if (portlet.isPreferencesUniquePerLayout()) {
381                 uniquePerLayout = true;
382 
383                 if (portlet.isPreferencesOwnedByGroup()) {
384                     uniquePerGroup = true;
385                 }
386             }
387             else {
388                 if (portlet.isPreferencesOwnedByGroup()) {
389                     uniquePerGroup = true;
390                     portletId = PortletConstants.getRootPortletId(portletId);
391                 }
392             }
393         }
394 
395         long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
396         int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
397         long plid = layout.getPlid();
398 
399         if (!uniquePerLayout) {
400             plid = PortletKeys.PREFS_PLID_SHARED;
401 
402             if (uniquePerGroup) {
403                 if (scopeGroupId > LayoutConstants.DEFAULT_PLID) {
404                     ownerId = scopeGroupId;
405                 }
406                 else {
407                     ownerId = layout.getGroupId();
408                 }
409 
410                 ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
411             }
412             else {
413                 ownerId = layout.getCompanyId();
414                 ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
415             }
416         }
417 
418         return PortletPreferencesLocalServiceUtil.getPreferences(
419             layout.getCompanyId(), ownerId, ownerType, plid, portletId,
420             defaultPreferences);
421     }
422 
423 }