001    /**
002     * Copyright (c) 2000-2012 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.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.workflow.permission.WorkflowPermissionUtil;
020    import com.liferay.portal.security.auth.PrincipalException;
021    import com.liferay.portal.security.permission.PermissionChecker;
022    import com.liferay.portal.security.permission.ResourceActionsUtil;
023    import com.liferay.portal.util.PropsValues;
024    import com.liferay.portlet.messageboards.model.MBMessage;
025    import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
026    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
027    
028    import java.util.List;
029    
030    /**
031     * @author Charles May
032     */
033    public class MBDiscussionPermission {
034    
035            public static void check(
036                            PermissionChecker permissionChecker, long companyId, long groupId,
037                            String className, long classPK, long messageId, long ownerId,
038                            String actionId)
039                    throws PortalException, SystemException {
040    
041                    if (!contains(
042                                    permissionChecker, companyId, groupId, className, classPK,
043                                    messageId, ownerId, actionId)) {
044    
045                            throw new PrincipalException();
046                    }
047            }
048    
049            public static void check(
050                            PermissionChecker permissionChecker, long companyId, long groupId,
051                            String className, long classPK, long ownerId, String actionId)
052                    throws PortalException, SystemException {
053    
054                    if (!contains(
055                                    permissionChecker, companyId, groupId, className, classPK,
056                                    ownerId, actionId)) {
057    
058                            throw new PrincipalException();
059                    }
060            }
061    
062            public static boolean contains(
063                            PermissionChecker permissionChecker, long companyId, long groupId,
064                            String className, long classPK, long messageId, long ownerId,
065                            String actionId)
066                    throws PortalException, SystemException {
067    
068                    MBMessage message = MBMessageLocalServiceUtil.getMessage(messageId);
069    
070                    if (PropsValues.DISCUSSION_COMMENTS_ALWAYS_EDITABLE_BY_OWNER &&
071                            (permissionChecker.getUserId() == message.getUserId())) {
072    
073                            return true;
074                    }
075    
076                    if (message.isPending()) {
077                            Boolean hasPermission = WorkflowPermissionUtil.hasPermission(
078                                    permissionChecker, message.getGroupId(),
079                                    message.getWorkflowClassName(), message.getMessageId(),
080                                    actionId);
081    
082                            if (hasPermission != null) {
083                                    return hasPermission.booleanValue();
084                            }
085                    }
086    
087                    return contains(
088                            permissionChecker, companyId, groupId, className, classPK, ownerId,
089                            actionId);
090            }
091    
092            public static boolean contains(
093                            PermissionChecker permissionChecker, long companyId, long groupId,
094                            String className, long classPK, long ownerId, String actionId)
095                    throws SystemException {
096    
097                    List<String> resourceActions = ResourceActionsUtil.getResourceActions(
098                            className);
099    
100                    if (!resourceActions.contains(actionId)) {
101                            return true;
102                    }
103    
104                    if (MBBanLocalServiceUtil.hasBan(
105                                    groupId, permissionChecker.getUserId())) {
106    
107                            return false;
108                    }
109    
110                    if ((ownerId > 0) &&
111                            permissionChecker.hasOwnerPermission(
112                                    companyId, className, classPK, ownerId, actionId)) {
113    
114                            return true;
115                    }
116    
117                    return permissionChecker.hasPermission(
118                            groupId, className, classPK, actionId);
119            }
120    
121    }