001    /**
002     * Copyright (c) 2000-2010 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.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.model.MBCategoryConstants;
025    import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
026    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
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 ((categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
059                            (categoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
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 (categoryId !=
090                                            MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
091    
092                                    category = MBCategoryLocalServiceUtil.getCategory(categoryId);
093    
094                                    categoryId = category.getParentCategoryId();
095    
096                                    if (!permissionChecker.hasOwnerPermission(
097                                                    category.getCompanyId(), MBCategory.class.getName(),
098                                                    category.getCategoryId(), category.getUserId(),
099                                                    actionId) &&
100                                            !permissionChecker.hasPermission(
101                                                    category.getGroupId(), MBCategory.class.getName(),
102                                                    category.getCategoryId(), actionId)) {
103    
104                                            return false;
105                                    }
106    
107                                    if (!PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
108                                            break;
109                                    }
110                            }
111    
112                            return true;
113                    }
114                    else {
115                            while (categoryId !=
116                                            MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
117    
118                                    if (permissionChecker.hasOwnerPermission(
119                                                    category.getCompanyId(), MBCategory.class.getName(),
120                                                    category.getCategoryId(), category.getUserId(),
121                                                    actionId)) {
122    
123                                            return true;
124                                    }
125    
126                                    if (permissionChecker.hasPermission(
127                                                    category.getGroupId(), MBCategory.class.getName(),
128                                                    category.getCategoryId(), actionId)) {
129    
130                                            return true;
131                                    }
132    
133                                    if (actionId.equals(ActionKeys.VIEW)) {
134                                            break;
135                                    }
136    
137                                    category = MBCategoryLocalServiceUtil.getCategory(categoryId);
138    
139                                    categoryId = category.getParentCategoryId();
140                            }
141    
142                            return false;
143                    }
144            }
145    
146    }