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.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 (PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
111                            long originalCategoryId = categoryId;
112    
113                            while (categoryId !=
114                                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
115    
116                                    category = MBCategoryLocalServiceUtil.getCategory(categoryId);
117    
118                                    if (!permissionChecker.hasOwnerPermission(
119                                                    category.getCompanyId(), MBCategory.class.getName(),
120                                                    categoryId, category.getUserId(), ActionKeys.VIEW) &&
121                                            !permissionChecker.hasPermission(
122                                                    category.getGroupId(), MBCategory.class.getName(),
123                                                    categoryId, ActionKeys.VIEW)) {
124    
125                                            return false;
126                                    }
127    
128                                    categoryId = category.getParentCategoryId();
129                            }
130    
131                            if (actionId.equals(ActionKeys.VIEW)) {
132                                    return true;
133                            }
134    
135                            categoryId = originalCategoryId;
136                    }
137    
138                    while (categoryId != MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
139                            category = MBCategoryLocalServiceUtil.getCategory(categoryId);
140    
141                            if (permissionChecker.hasOwnerPermission(
142                                            category.getCompanyId(), MBCategory.class.getName(),
143                                            categoryId, category.getUserId(), actionId) ||
144                                    permissionChecker.hasPermission(
145                                            category.getGroupId(), MBCategory.class.getName(),
146                                            categoryId, actionId)) {
147    
148                                    return true;
149                            }
150    
151                            categoryId = category.getParentCategoryId();
152                    }
153    
154                    return false;
155            }
156    
157    }