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