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.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    }