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, long ownerId,
037                            String actionId)
038                    throws PortalException, SystemException {
039    
040                    if (!contains(
041                                    permissionChecker, companyId, groupId, className, classPK,
042                                    messageId, ownerId, actionId)) {
043    
044                            throw new PrincipalException();
045                    }
046            }
047    
048            public static void check(
049                            PermissionChecker permissionChecker, long companyId, long groupId,
050                            String className, long classPK, long ownerId, String actionId)
051                    throws PortalException, SystemException {
052    
053                    if (!contains(
054                                    permissionChecker, companyId, groupId, className, classPK,
055                                    ownerId, actionId)) {
056    
057                            throw new PrincipalException();
058                    }
059            }
060    
061            public static boolean contains(
062                            PermissionChecker permissionChecker, long companyId, long groupId,
063                            String className, long classPK, long messageId, long ownerId,
064                            String actionId)
065                    throws PortalException, SystemException {
066    
067                    MBMessage message = MBMessageLocalServiceUtil.getMessage(
068                            messageId);
069    
070                    if (message.isPending()) {
071                            Boolean hasPermission = WorkflowPermissionUtil.hasPermission(
072                                    permissionChecker, message.getGroupId(),
073                                    message.getWorkflowClassName(), message.getMessageId(),
074                                    actionId);
075    
076                            if (hasPermission != null) {
077                                    return hasPermission.booleanValue();
078                            }
079                    }
080    
081                    return contains(
082                            permissionChecker, companyId, groupId, className, classPK,
083                            ownerId, actionId);
084            }
085    
086            public static boolean contains(
087                            PermissionChecker permissionChecker, long companyId, long groupId,
088                            String className, long classPK, long ownerId, String actionId)
089                    throws SystemException {
090    
091                    List<String> resourceActions = ResourceActionsUtil.getResourceActions(
092                            className);
093    
094                    if (!resourceActions.contains(actionId)) {
095                            return true;
096                    }
097    
098                    if (MBBanLocalServiceUtil.hasBan(
099                                    groupId, permissionChecker.getUserId())) {
100    
101                            return false;
102                    }
103    
104                    if (permissionChecker.hasOwnerPermission(
105                                    companyId, className, classPK, ownerId, actionId)) {
106    
107                            return true;
108                    }
109    
110                    return permissionChecker.hasPermission(
111                            groupId, className, classPK, actionId);
112            }
113    
114    }