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