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