001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.messageboards.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.staging.permission.StagingPermissionUtil;
020    import com.liferay.portal.kernel.workflow.permission.WorkflowPermissionUtil;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portal.security.permission.ResourceActionsUtil;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portal.util.PropsValues;
026    import com.liferay.portlet.messageboards.model.MBMessage;
027    import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
028    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
029    
030    import java.util.List;
031    
032    /**
033     * @author Charles May
034     */
035    public class MBDiscussionPermission {
036    
037            public static void check(
038                            PermissionChecker permissionChecker, long companyId, long groupId,
039                            String className, long classPK, long messageId, long ownerId,
040                            String actionId)
041                    throws PortalException, SystemException {
042    
043                    if (!contains(
044                                    permissionChecker, companyId, groupId, className, classPK,
045                                    messageId, ownerId, actionId)) {
046    
047                            throw new PrincipalException();
048                    }
049            }
050    
051            public static void check(
052                            PermissionChecker permissionChecker, long companyId, long groupId,
053                            String className, long classPK, long ownerId, String actionId)
054                    throws PortalException, SystemException {
055    
056                    if (!contains(
057                                    permissionChecker, companyId, groupId, className, classPK,
058                                    ownerId, actionId)) {
059    
060                            throw new PrincipalException();
061                    }
062            }
063    
064            public static boolean contains(
065                            PermissionChecker permissionChecker, long companyId, long groupId,
066                            String className, long classPK, long messageId, long ownerId,
067                            String actionId)
068                    throws PortalException, SystemException {
069    
070                    MBMessage message = MBMessageLocalServiceUtil.getMessage(messageId);
071    
072                    if (PropsValues.DISCUSSION_COMMENTS_ALWAYS_EDITABLE_BY_OWNER &&
073                            (permissionChecker.getUserId() == message.getUserId())) {
074    
075                            return true;
076                    }
077    
078                    if (message.isPending()) {
079                            Boolean hasPermission = WorkflowPermissionUtil.hasPermission(
080                                    permissionChecker, message.getGroupId(),
081                                    message.getWorkflowClassName(), message.getMessageId(),
082                                    actionId);
083    
084                            if (hasPermission != null) {
085                                    return hasPermission.booleanValue();
086                            }
087                    }
088    
089                    return contains(
090                            permissionChecker, companyId, groupId, className, classPK, ownerId,
091                            actionId);
092            }
093    
094            public static boolean contains(
095                            PermissionChecker permissionChecker, long companyId, long groupId,
096                            String className, long classPK, long ownerId, String actionId)
097                    throws SystemException {
098    
099                    if (MBBanLocalServiceUtil.hasBan(
100                                    groupId, permissionChecker.getUserId())) {
101    
102                            return false;
103                    }
104    
105                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
106                            permissionChecker, groupId, className, classPK,
107                            PortletKeys.MESSAGE_BOARDS, actionId);
108    
109                    if (hasPermission != null) {
110                            return hasPermission.booleanValue();
111                    }
112    
113                    List<String> resourceActions = ResourceActionsUtil.getResourceActions(
114                            className);
115    
116                    if (!resourceActions.contains(actionId)) {
117                            return true;
118                    }
119    
120                    if ((ownerId > 0) &&
121                            permissionChecker.hasOwnerPermission(
122                                    companyId, className, classPK, ownerId, actionId)) {
123    
124                            return true;
125                    }
126    
127                    return permissionChecker.hasPermission(
128                            groupId, className, classPK, actionId);
129            }
130    
131    }