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.security.permission.ActionKeys;
29 import com.liferay.portal.kernel.security.permission.PermissionChecker;
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.PortletPreferencesIds;
38 import com.liferay.portal.security.auth.PrincipalException;
39 import com.liferay.portal.security.permission.PermissionThreadLocal;
40 import com.liferay.portal.service.PortletLocalServiceUtil;
41 import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
42 import com.liferay.portal.service.UserLocalServiceUtil;
43 import com.liferay.portal.service.permission.LayoutPermissionUtil;
44 import com.liferay.portal.servlet.PortletContextPool;
45 import com.liferay.portal.servlet.PortletContextWrapper;
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 }
222 else {
223 if (portlet.isPreferencesUniquePerLayout()) {
224 ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
225 ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
226 plid = selLayout.getPlid();
227
228 if (portlet.isPreferencesOwnedByGroup()) {
229 }
230 else {
231 long userId = PortalUtil.getUserId(req);
232
233 if ((userId <= 0) || modeEditGuest) {
234 userId = UserLocalServiceUtil.getDefaultUserId(
235 themeDisplay.getCompanyId());
236 }
237
238 ownerId = userId;
239 ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
240 }
241 }
242 else {
243 plid = PortletKeys.PREFS_PLID_SHARED;
244
245 if (portlet.isPreferencesOwnedByGroup()) {
246 ownerId = selLayout.getGroupId();
247 ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
248 }
249 else {
250 long userId = PortalUtil.getUserId(req);
251
252 if ((userId <= 0) || modeEditGuest) {
253 userId = UserLocalServiceUtil.getDefaultUserId(
254 themeDisplay.getCompanyId());
255 }
256
257 ownerId = userId;
258 ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;
259 }
260 }
261 }
262
263 return new PortletPreferencesIds(
264 themeDisplay.getCompanyId(), ownerId, ownerType, plid, portletId);
265 }
266
267 public PortletPreferences getPortletSetup(
268 Layout layout, String portletId)
269 throws PortalException, SystemException {
270
271 long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
272 int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
273
274 return PortletPreferencesLocalServiceUtil.getPreferences(
275 layout.getCompanyId(), ownerId, ownerType, layout.getPlid(),
276 portletId);
277 }
278
279 public PortletPreferences getPortletSetup(
280 HttpServletRequest req, String portletId, boolean uniquePerLayout,
281 boolean uniquePerGroup)
282 throws PortalException, SystemException {
283
284 return getPortletSetup(
285 req, portletId, uniquePerLayout, uniquePerGroup, null);
286 }
287
288 public PortletPreferences getPortletSetup(
289 HttpServletRequest req, String portletId, boolean uniquePerLayout,
290 boolean uniquePerGroup, String defaultPreferences)
291 throws PortalException, SystemException {
292
293 Layout layout = (Layout)req.getAttribute(WebKeys.LAYOUT);
294
295 return getPortletSetup(
296 layout, portletId, uniquePerLayout, uniquePerGroup,
297 defaultPreferences);
298 }
299
300 public PortletPreferences getPortletSetup(
301 Layout layout, String portletId, boolean uniquePerLayout,
302 boolean uniquePerGroup, String defaultPreferences)
303 throws PortalException, SystemException {
304
305 long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
306 int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
307 long plid = layout.getPlid();
308
309 if (!uniquePerLayout) {
310 plid = PortletKeys.PREFS_PLID_SHARED;
311
312 if (uniquePerGroup) {
313 ownerId = layout.getGroupId();
314 ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
315 }
316 else {
317 ownerId = layout.getCompanyId();
318 ownerType = PortletKeys.PREFS_OWNER_TYPE_COMPANY;
319 }
320 }
321
322 return PortletPreferencesLocalServiceUtil.getPreferences(
323 layout.getCompanyId(), ownerId, ownerType, plid, portletId,
324 defaultPreferences);
325 }
326
327 public PortletPreferences getPortletSetup(
328 ActionRequest req, String portletId, boolean uniquePerLayout,
329 boolean uniquePerGroup)
330 throws PortalException, SystemException {
331
332 HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
333
334 return getPortletSetup(
335 httpReq, portletId, uniquePerLayout, uniquePerGroup);
336 }
337
338 public PortletPreferences getPortletSetup(
339 RenderRequest req, String portletId, boolean uniquePerLayout,
340 boolean uniquePerGroup)
341 throws PortalException, SystemException {
342
343 HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
344
345 return getPortletSetup(
346 httpReq, portletId, uniquePerLayout, uniquePerGroup);
347 }
348
349 public PortletPreferences getPreferences(HttpServletRequest req) {
350 RenderRequest renderRequest = (RenderRequest)req.getAttribute(
351 JavaConstants.JAVAX_PORTLET_REQUEST);
352
353 PortletPreferences prefs = null;
354
355 if (renderRequest != null) {
356 PortletPreferencesWrapper prefsWrapper =
357 (PortletPreferencesWrapper)renderRequest.getPreferences();
358
359 prefs = prefsWrapper.getPreferencesImpl();
360 }
361
362 return prefs;
363 }
364
365 public PreferencesValidator getPreferencesValidator(Portlet portlet) {
366 if (portlet.isWARFile()) {
367 PortletContextWrapper pcw =
368 PortletContextPool.get(portlet.getRootPortletId());
369
370 return pcw.getPreferencesValidatorInstance();
371 }
372 else {
373 PreferencesValidator prefsValidator = null;
374
375 if (Validator.isNotNull(portlet.getPreferencesValidator())) {
376 prefsValidator =
377 (PreferencesValidator)InstancePool.get(
378 portlet.getPreferencesValidator());
379 }
380
381 return prefsValidator;
382 }
383 }
384
385 }