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