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