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