001    /**
002     * Copyright (c) 2000-2012 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     * @author Mate Thurzo
031     */
032    public class MBCategoryPermission {
033    
034            public static void check(
035                            PermissionChecker permissionChecker, long groupId, long categoryId,
036                            String actionId)
037                    throws PortalException, SystemException {
038    
039                    if (!contains(permissionChecker, groupId, categoryId, actionId)) {
040                            throw new PrincipalException();
041                    }
042            }
043    
044            public static void check(
045                            PermissionChecker permissionChecker, long categoryId,
046                            String actionId)
047                    throws PortalException, SystemException {
048    
049                    if (!contains(permissionChecker, categoryId, actionId)) {
050                            throw new PrincipalException();
051                    }
052            }
053    
054            public static void check(
055                            PermissionChecker permissionChecker, MBCategory category,
056                            String actionId)
057                    throws PortalException, SystemException {
058    
059                    if (!contains(permissionChecker, category, actionId)) {
060                            throw new PrincipalException();
061                    }
062            }
063    
064            public static boolean contains(
065                            PermissionChecker permissionChecker, long groupId, long categoryId,
066                            String actionId)
067                    throws PortalException, SystemException {
068    
069                    if ((categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
070                            (categoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
071    
072                            return MBPermission.contains(permissionChecker, groupId, actionId);
073                    }
074                    else {
075                            MBCategory category = MBCategoryLocalServiceUtil.getCategory(
076                                    categoryId);
077    
078                            return contains(permissionChecker, category, actionId);
079                    }
080            }
081    
082            public static boolean contains(
083                            PermissionChecker permissionChecker, long categoryId,
084                            String actionId)
085                    throws PortalException, SystemException {
086    
087                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(
088                            categoryId);
089    
090                    return contains(permissionChecker, category, actionId);
091            }
092    
093            public static boolean contains(
094                            PermissionChecker permissionChecker, MBCategory category,
095                            String actionId)
096                    throws PortalException, SystemException {
097    
098                    if (actionId.equals(ActionKeys.ADD_CATEGORY)) {
099                            actionId = ActionKeys.ADD_SUBCATEGORY;
100                    }
101    
102                    if (MBBanLocalServiceUtil.hasBan(
103                                    category.getGroupId(), permissionChecker.getUserId())) {
104    
105                            return false;
106                    }
107    
108                    long categoryId = category.getCategoryId();
109    
110                    if (actionId.equals(ActionKeys.VIEW)) {
111                            while (categoryId !=
112                                            MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
113    
114                                    category = MBCategoryLocalServiceUtil.getCategory(categoryId);
115    
116                                    categoryId = category.getParentCategoryId();
117    
118                                    if (!permissionChecker.hasOwnerPermission(
119                                                    category.getCompanyId(), MBCategory.class.getName(),
120                                                    category.getCategoryId(), category.getUserId(),
121                                                    actionId) &&
122                                            !permissionChecker.hasPermission(
123                                                    category.getGroupId(), MBCategory.class.getName(),
124                                                    category.getCategoryId(), actionId)) {
125    
126                                            return false;
127                                    }
128    
129                                    if (!PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
130                                            break;
131                                    }
132                            }
133    
134                            return true;
135                    }
136                    else {
137                            while (categoryId !=
138                                            MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
139    
140                                    category = MBCategoryLocalServiceUtil.getCategory(categoryId);
141    
142                                    categoryId = category.getParentCategoryId();
143    
144                                    if (permissionChecker.hasOwnerPermission(
145                                                    category.getCompanyId(), MBCategory.class.getName(),
146                                                    category.getCategoryId(), category.getUserId(),
147                                                    actionId)) {
148    
149                                            return true;
150                                    }
151    
152                                    if (permissionChecker.hasPermission(
153                                                    category.getGroupId(), MBCategory.class.getName(),
154                                                    category.getCategoryId(), actionId)) {
155    
156                                            return true;
157                                    }
158    
159                                    if (actionId.equals(ActionKeys.VIEW)) {
160                                            break;
161                                    }
162                            }
163    
164                            return false;
165                    }
166            }
167    
168    }