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