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