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