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