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