1
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
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
173
176
179
182
185
188
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
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 }