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                    if (MBBanLocalServiceUtil.hasBan(
075                                    message.getGroupId(), permissionChecker.getUserId())) {
076    
077                            return false;
078                    }
079    
080                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
081                            permissionChecker, message.getGroupId(), MBMessage.class.getName(),
082                            message.getMessageId(), PortletKeys.MESSAGE_BOARDS, actionId);
083    
084                    if (hasPermission != null) {
085                            return hasPermission.booleanValue();
086                    }
087    
088                    if (message.isDraft() || message.isScheduled()) {
089                            if (actionId.equals(ActionKeys.VIEW) &&
090                                    !contains(permissionChecker, message, ActionKeys.UPDATE)) {
091    
092                                    return false;
093                            }
094                    }
095                    else if (message.isPending()) {
096                            hasPermission = WorkflowPermissionUtil.hasPermission(
097                                    permissionChecker, message.getGroupId(),
098                                    message.getWorkflowClassName(), message.getMessageId(),
099                                    actionId);
100    
101                            if (hasPermission != null) {
102                                    return hasPermission.booleanValue();
103                            }
104                    }
105    
106                    if (actionId.equals(ActionKeys.VIEW) &&
107                            PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
108    
109                            long categoryId = message.getCategoryId();
110    
111                            if ((categoryId !=
112                                            MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
113                                    (categoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
114    
115                                    try {
116                                            MBCategory category =
117                                                    MBCategoryLocalServiceUtil.getCategory(categoryId);
118    
119                                            if (!MBCategoryPermission.contains(
120                                                            permissionChecker, category, actionId)) {
121    
122                                                    return false;
123                                            }
124                                    }
125                                    catch (NoSuchCategoryException nsce) {
126                                            if (!message.isInTrash()) {
127                                                    throw nsce;
128                                            }
129                                    }
130                            }
131                    }
132    
133                    if (permissionChecker.hasOwnerPermission(
134                                    message.getCompanyId(), MBMessage.class.getName(),
135                                    message.getRootMessageId(), message.getUserId(), actionId)) {
136    
137                            return true;
138                    }
139    
140                    return permissionChecker.hasPermission(
141                            message.getGroupId(), MBMessage.class.getName(),
142                            message.getMessageId(), actionId);
143            }
144    
145    }