001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.service.permission;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.staging.permission.StagingPermissionUtil;
022    import com.liferay.portal.model.Group;
023    import com.liferay.portal.model.Layout;
024    import com.liferay.portal.model.LayoutTypePortlet;
025    import com.liferay.portal.model.Portlet;
026    import com.liferay.portal.model.PortletConstants;
027    import com.liferay.portal.security.auth.PrincipalException;
028    import com.liferay.portal.security.permission.ActionKeys;
029    import com.liferay.portal.security.permission.PermissionChecker;
030    import com.liferay.portal.security.permission.ResourceActionsUtil;
031    import com.liferay.portal.service.LayoutLocalServiceUtil;
032    import com.liferay.portal.service.PortletLocalServiceUtil;
033    import com.liferay.portal.util.PropsValues;
034    import com.liferay.portlet.sites.util.SitesUtil;
035    
036    import java.util.Collection;
037    import java.util.List;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     * @author Raymond Augé
042     */
043    public class PortletPermissionImpl implements PortletPermission {
044    
045            public static final boolean DEFAULT_STRICT = false;
046    
047            public void check(
048                            PermissionChecker permissionChecker, Layout layout,
049                            String portletId, String actionId)
050                    throws PortalException, SystemException {
051    
052                    if (!contains(
053                                    permissionChecker, 0, layout, portletId, actionId,
054                                    DEFAULT_STRICT)) {
055    
056                            throw new PrincipalException();
057                    }
058            }
059    
060            public void check(
061                            PermissionChecker permissionChecker, Layout layout,
062                            String portletId, String actionId, boolean strict)
063                    throws PortalException, SystemException {
064    
065                    if (!contains(
066                                    permissionChecker, 0, layout, portletId, actionId, strict)) {
067    
068                            throw new PrincipalException();
069                    }
070            }
071    
072            public void check(
073                            PermissionChecker permissionChecker, long groupId, Layout layout,
074                            String portletId, String actionId)
075                    throws PortalException, SystemException {
076    
077                    if (!contains(
078                                    permissionChecker, groupId, layout, portletId, actionId,
079                                    DEFAULT_STRICT)) {
080    
081                            throw new PrincipalException();
082                    }
083            }
084    
085            public void check(
086                            PermissionChecker permissionChecker, long groupId, Layout layout,
087                            String portletId, String actionId, boolean strict)
088                    throws PortalException, SystemException {
089    
090                    if (!contains(
091                                    permissionChecker, groupId, layout, portletId, actionId,
092                                    strict)) {
093    
094                            throw new PrincipalException();
095                    }
096            }
097    
098            public void check(
099                            PermissionChecker permissionChecker, long groupId, long plid,
100                            String portletId, String actionId)
101                    throws PortalException, SystemException {
102    
103                    check(
104                            permissionChecker, groupId, plid, portletId, actionId,
105                            DEFAULT_STRICT);
106            }
107    
108            public void check(
109                            PermissionChecker permissionChecker, long groupId, long plid,
110                            String portletId, String actionId, boolean strict)
111                    throws PortalException, SystemException {
112    
113                    if (!contains(
114                                    permissionChecker, groupId, plid, portletId, actionId,
115                                    strict)) {
116    
117                            throw new PrincipalException();
118                    }
119            }
120    
121            public void check(
122                            PermissionChecker permissionChecker, long plid, String portletId,
123                            String actionId)
124                    throws PortalException, SystemException {
125    
126                    check(permissionChecker, plid, portletId, actionId, DEFAULT_STRICT);
127            }
128    
129            public void check(
130                            PermissionChecker permissionChecker, long plid, String portletId,
131                            String actionId, boolean strict)
132                    throws PortalException, SystemException {
133    
134                    if (!contains(permissionChecker, plid, portletId, actionId, strict)) {
135                            throw new PrincipalException();
136                    }
137            }
138    
139            public void check(
140                            PermissionChecker permissionChecker, String portletId,
141                            String actionId)
142                    throws PortalException, SystemException {
143    
144                    if (!contains(permissionChecker, portletId, actionId)) {
145                            throw new PrincipalException();
146                    }
147            }
148    
149            public boolean contains(
150                            PermissionChecker permissionChecker, Layout layout, Portlet portlet,
151                            String actionId)
152                    throws PortalException, SystemException {
153    
154                    return contains(
155                            permissionChecker, layout, portlet, actionId, DEFAULT_STRICT);
156            }
157    
158            public boolean contains(
159                            PermissionChecker permissionChecker, Layout layout, Portlet portlet,
160                            String actionId, boolean strict)
161                    throws PortalException, SystemException {
162    
163                    return contains(
164                            permissionChecker, 0, layout, portlet, actionId, strict);
165            }
166    
167            public boolean contains(
168                            PermissionChecker permissionChecker, Layout layout,
169                            String portletId, String actionId)
170                    throws PortalException, SystemException {
171    
172                    return contains(
173                            permissionChecker, layout, portletId, actionId, DEFAULT_STRICT);
174            }
175    
176            public boolean contains(
177                            PermissionChecker permissionChecker, Layout layout,
178                            String portletId, String actionId, boolean strict)
179                    throws PortalException, SystemException {
180    
181                    return contains(
182                            permissionChecker, 0, layout, portletId, actionId, strict);
183            }
184    
185            public boolean contains(
186                            PermissionChecker permissionChecker, long groupId, Layout layout,
187                            Portlet portlet, String actionId)
188                    throws PortalException, SystemException {
189    
190                    return contains(
191                            permissionChecker, groupId, layout, portlet, actionId,
192                            DEFAULT_STRICT);
193            }
194    
195            public boolean contains(
196                            PermissionChecker permissionChecker, long groupId, Layout layout,
197                            Portlet portlet, String actionId, boolean strict)
198                    throws PortalException, SystemException {
199    
200                    if (portlet.isUndeployedPortlet()) {
201                            return false;
202                    }
203    
204                    boolean value = contains(
205                            permissionChecker, groupId, layout, portlet.getPortletId(),
206                            actionId, strict);
207    
208                    if (value) {
209                            return true;
210                    }
211                    else {
212                            if (portlet.isSystem() && actionId.equals(ActionKeys.VIEW)) {
213                                    return true;
214                            }
215                            else {
216                                    return false;
217                            }
218                    }
219            }
220    
221            public boolean contains(
222                            PermissionChecker permissionChecker, long groupId, Layout layout,
223                            String portletId, String actionId)
224                    throws PortalException, SystemException {
225    
226                    return contains(
227                            permissionChecker, groupId, layout, portletId, actionId,
228                            DEFAULT_STRICT);
229            }
230    
231            public boolean contains(
232                            PermissionChecker permissionChecker, long groupId, Layout layout,
233                            String portletId, String actionId, boolean strict)
234                    throws PortalException, SystemException {
235    
236                    String name = null;
237                    String primKey = null;
238    
239                    if (layout == null) {
240                            name = portletId;
241                            primKey = portletId;
242    
243                            return permissionChecker.hasPermission(
244                                    groupId, name, primKey, actionId);
245                    }
246    
247                    Group group = layout.getGroup();
248    
249                    groupId = group.getGroupId();
250    
251                    name = PortletConstants.getRootPortletId(portletId);
252                    primKey = getPrimaryKey(layout.getPlid(), portletId);
253    
254                    if (!group.isLayoutSetPrototype() &&
255                            SitesUtil.isLayoutLocked(layout) &&
256                            actionId.equals(ActionKeys.CONFIGURATION)) {
257    
258                            return false;
259                    }
260    
261                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
262                            permissionChecker, groupId, name, groupId, name, actionId);
263    
264                    if (hasPermission != null) {
265                            return hasPermission.booleanValue();
266                    }
267    
268                    if ((layout.isPrivateLayout() &&
269                             !PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_MODIFIABLE) ||
270                            (layout.isPublicLayout() &&
271                             !PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_MODIFIABLE)) {
272    
273                            if (actionId.equals(ActionKeys.CONFIGURATION) && group.isUser()) {
274                                    return false;
275                            }
276                    }
277    
278                    if (actionId.equals(ActionKeys.VIEW) && group.isControlPanel()) {
279                            return true;
280                    }
281    
282                    if (strict) {
283                            return permissionChecker.hasPermission(
284                                    groupId, name, primKey, actionId);
285                    }
286    
287                    LayoutTypePortlet layoutTypePortlet =
288                            (LayoutTypePortlet)layout.getLayoutType();
289    
290                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
291                            layout.getCompanyId(), portletId);
292    
293                    if (layoutTypePortlet.isCustomizedView() &&
294                            layoutTypePortlet.isPortletCustomizable(portletId) &&
295                            LayoutPermissionUtil.contains(
296                                    permissionChecker, layout, ActionKeys.CUSTOMIZE)) {
297    
298                            if (actionId.equals(ActionKeys.VIEW)) {
299                                    return true;
300                            }
301                            else if (actionId.equals(ActionKeys.CONFIGURATION) &&
302                                             portlet.isPreferencesUniquePerLayout()) {
303    
304                                    return true;
305                            }
306                    }
307    
308                    return permissionChecker.hasPermission(
309                            groupId, name, primKey, actionId);
310            }
311    
312            public boolean contains(
313                    PermissionChecker permissionChecker, long groupId, long plid,
314                    Collection<Portlet> portlets, String actionId) {
315    
316                    for (Portlet portlet : portlets) {
317                            if (permissionChecker.hasPermission(
318                                            groupId, portlet.getPortletId(), portlet.getPortletId(),
319                                            ActionKeys.ACCESS_IN_CONTROL_PANEL)) {
320    
321                                    return true;
322                            }
323                    }
324    
325                    return false;
326            }
327    
328            public boolean contains(
329                            PermissionChecker permissionChecker, long groupId, long plid,
330                            Portlet portlet, String actionId)
331                    throws PortalException, SystemException {
332    
333                    Layout layout = LayoutLocalServiceUtil.fetchLayout(plid);
334    
335                    return contains(
336                            permissionChecker, groupId, layout, portlet, actionId,
337                            DEFAULT_STRICT);
338            }
339    
340            public boolean contains(
341                            PermissionChecker permissionChecker, long groupId, long plid,
342                            Portlet portlet, String actionId, boolean strict)
343                    throws PortalException, SystemException {
344    
345                    Layout layout = LayoutLocalServiceUtil.fetchLayout(plid);
346    
347                    return contains(
348                            permissionChecker, groupId, layout, portlet, actionId, strict);
349            }
350    
351            public boolean contains(
352                            PermissionChecker permissionChecker, long groupId, long plid,
353                            String portletId, String actionId)
354                    throws PortalException, SystemException {
355    
356                    Layout layout = LayoutLocalServiceUtil.fetchLayout(plid);
357    
358                    return contains(
359                            permissionChecker, groupId, layout, portletId, actionId,
360                            DEFAULT_STRICT);
361            }
362    
363            public boolean contains(
364                            PermissionChecker permissionChecker, long groupId, long plid,
365                            String portletId, String actionId, boolean strict)
366                    throws PortalException, SystemException {
367    
368                    Layout layout = LayoutLocalServiceUtil.fetchLayout(plid);
369    
370                    return contains(
371                            permissionChecker, groupId, layout, portletId, actionId, strict);
372            }
373    
374            public boolean contains(
375                            PermissionChecker permissionChecker, long plid, Portlet portlet,
376                            String actionId)
377                    throws PortalException, SystemException {
378    
379                    Layout layout = LayoutLocalServiceUtil.fetchLayout(plid);
380    
381                    return contains(
382                            permissionChecker, layout, portlet, actionId, DEFAULT_STRICT);
383            }
384    
385            public boolean contains(
386                            PermissionChecker permissionChecker, long plid, Portlet portlet,
387                            String actionId, boolean strict)
388                    throws PortalException, SystemException {
389    
390                    Layout layout = LayoutLocalServiceUtil.fetchLayout(plid);
391    
392                    return contains(
393                            permissionChecker, 0, layout, portlet, actionId, strict);
394            }
395    
396            public boolean contains(
397                            PermissionChecker permissionChecker, long plid, String portletId,
398                            String actionId)
399                    throws PortalException, SystemException {
400    
401                    Layout layout = LayoutLocalServiceUtil.fetchLayout(plid);
402    
403                    return contains(
404                            permissionChecker, layout, portletId, actionId, DEFAULT_STRICT);
405            }
406    
407            public boolean contains(
408                            PermissionChecker permissionChecker, long plid, String portletId,
409                            String actionId, boolean strict)
410                    throws PortalException, SystemException {
411    
412                    Layout layout = LayoutLocalServiceUtil.fetchLayout(plid);
413    
414                    return contains(
415                            permissionChecker, 0, layout, portletId, actionId, strict);
416            }
417    
418            public boolean contains(
419                            PermissionChecker permissionChecker, String portletId,
420                            String actionId)
421                    throws PortalException, SystemException {
422    
423                    return contains(permissionChecker, 0, portletId, actionId);
424            }
425    
426            public String getPrimaryKey(long plid, String portletId) {
427                    return String.valueOf(plid).concat(
428                            PortletConstants.LAYOUT_SEPARATOR).concat(portletId);
429            }
430    
431            public boolean hasLayoutManagerPermission(
432                    String portletId, String actionId) {
433    
434                    try {
435                            return hasLayoutManagerPermissionImpl(portletId, actionId);
436                    }
437                    catch (Exception e) {
438                            _log.error(e, e);
439    
440                            return false;
441                    }
442            }
443    
444            protected boolean hasLayoutManagerPermissionImpl(
445                    String portletId, String actionId) {
446    
447                    portletId = PortletConstants.getRootPortletId(portletId);
448    
449                    List<String> layoutManagerActions =
450                            ResourceActionsUtil.getPortletResourceLayoutManagerActions(
451                                    portletId);
452    
453                    return layoutManagerActions.contains(actionId);
454            }
455    
456            private static Log _log = LogFactoryUtil.getLog(
457                    PortletPermissionImpl.class);
458    
459    }