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