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.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.MBCategoryConstants;
026    import com.liferay.portlet.messageboards.model.MBMessage;
027    import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
028    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
029    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
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 ((categoryId != MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
093                            (categoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
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                                    if (actionId.equals(ActionKeys.VIEW)) {
106                                            return true;
107                                    }
108                            }
109    
110                            if (MBCategoryPermission.contains(
111                                            permissionChecker, category, actionId)) {
112    
113                                    return true;
114                            }
115                    }
116    
117                    if (permissionChecker.hasOwnerPermission(
118                                    message.getCompanyId(), MBMessage.class.getName(),
119                                    message.getRootMessageId(), message.getUserId(), actionId)) {
120    
121                            return true;
122                    }
123    
124                    return permissionChecker.hasPermission(
125                            groupId, MBMessage.class.getName(), message.getMessageId(),
126                            actionId);
127            }
128    
129    }