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