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 (MBBanLocalServiceUtil.hasBan(
073                                    groupId, permissionChecker.getUserId())) {
074    
075                            return false;
076                    }
077    
078                    if ((categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
079                            (categoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
080    
081                            return MBPermission.contains(permissionChecker, groupId, actionId);
082                    }
083    
084                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(
085                            categoryId);
086    
087                    return contains(permissionChecker, category, actionId);
088            }
089    
090            public static boolean contains(
091                            PermissionChecker permissionChecker, long categoryId,
092                            String actionId)
093                    throws PortalException, SystemException {
094    
095                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(
096                            categoryId);
097    
098                    return contains(permissionChecker, category, actionId);
099            }
100    
101            public static boolean contains(
102                            PermissionChecker permissionChecker, MBCategory category,
103                            String actionId)
104                    throws PortalException, SystemException {
105    
106                    if (MBBanLocalServiceUtil.hasBan(
107                                    category.getGroupId(), permissionChecker.getUserId())) {
108    
109                            return false;
110                    }
111    
112                    if (actionId.equals(ActionKeys.ADD_CATEGORY)) {
113                            actionId = ActionKeys.ADD_SUBCATEGORY;
114                    }
115    
116                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
117                            permissionChecker, category.getGroupId(),
118                            MBCategory.class.getName(), category.getCategoryId(),
119                            PortletKeys.MESSAGE_BOARDS, actionId);
120    
121                    if (hasPermission != null) {
122                            return hasPermission.booleanValue();
123                    }
124    
125                    if (actionId.equals(ActionKeys.VIEW) &&
126                            PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
127    
128                            try {
129                                    long categoryId = category.getCategoryId();
130    
131                                    while (categoryId !=
132                                                            MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
133    
134                                            category = MBCategoryLocalServiceUtil.getCategory(
135                                                    categoryId);
136    
137                                            if (!_hasPermission(
138                                                            permissionChecker, category, actionId)) {
139    
140                                                    return false;
141                                            }
142    
143                                            categoryId = category.getParentCategoryId();
144                                    }
145                            }
146                            catch (NoSuchCategoryException nsce) {
147                                    if (!category.isInTrash()) {
148                                            throw nsce;
149                                    }
150                            }
151    
152                            return MBPermission.contains(
153                                    permissionChecker, category.getGroupId(), actionId);
154                    }
155    
156                    return _hasPermission(permissionChecker, category, actionId);
157            }
158    
159            private static boolean _hasPermission(
160                    PermissionChecker permissionChecker, MBCategory category,
161                    String actionId) {
162    
163                    if (permissionChecker.hasOwnerPermission(
164                                    category.getCompanyId(), MBCategory.class.getName(),
165                                    category.getCategoryId(), category.getUserId(), actionId) ||
166                            permissionChecker.hasPermission(
167                                    category.getGroupId(), MBCategory.class.getName(),
168                                    category.getCategoryId(), actionId)) {
169    
170                            return true;
171                    }
172    
173                    return false;
174            }
175    
176    }