001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.messageboards.service.permission;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.security.permission.ActionKeys;
026: import com.liferay.portal.kernel.security.permission.PermissionChecker;
027: import com.liferay.portal.security.auth.PrincipalException;
028: import com.liferay.portal.service.permission.PortletPermissionUtil;
029: import com.liferay.portal.util.PortletKeys;
030: import com.liferay.portlet.messageboards.model.MBCategory;
031: import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
032: import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
033: import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
034:
035: /**
036: * <a href="MBCategoryPermission.java.html"><b><i>View Source</i></b></a>
037: *
038: * @author Brian Wing Shun Chan
039: *
040: */
041: public class MBCategoryPermission {
042:
043: public static void check(PermissionChecker permissionChecker,
044: long plid, long categoryId, String actionId)
045: throws PortalException, SystemException {
046:
047: if (!contains(permissionChecker, plid, categoryId, actionId)) {
048: throw new PrincipalException();
049: }
050: }
051:
052: public static void check(PermissionChecker permissionChecker,
053: long categoryId, String actionId) throws PortalException,
054: SystemException {
055:
056: if (!contains(permissionChecker, categoryId, actionId)) {
057: throw new PrincipalException();
058: }
059: }
060:
061: public static void check(PermissionChecker permissionChecker,
062: MBCategory category, String actionId)
063: throws PortalException, SystemException {
064:
065: if (!contains(permissionChecker, category, actionId)) {
066: throw new PrincipalException();
067: }
068: }
069:
070: public static boolean contains(PermissionChecker permissionChecker,
071: long plid, long categoryId, String actionId)
072: throws PortalException, SystemException {
073:
074: if (categoryId == MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
075: return PortletPermissionUtil.contains(permissionChecker,
076: plid, PortletKeys.MESSAGE_BOARDS, actionId);
077: } else {
078: return contains(permissionChecker, categoryId, actionId);
079: }
080: }
081:
082: public static boolean contains(PermissionChecker permissionChecker,
083: long categoryId, String actionId) throws PortalException,
084: SystemException {
085:
086: MBCategory category = MBCategoryLocalServiceUtil
087: .getCategory(categoryId);
088:
089: return contains(permissionChecker, category, actionId);
090: }
091:
092: public static boolean contains(PermissionChecker permissionChecker,
093: MBCategory category, String actionId)
094: throws PortalException, SystemException {
095:
096: if (actionId.equals(ActionKeys.ADD_CATEGORY)) {
097: actionId = ActionKeys.ADD_SUBCATEGORY;
098: }
099:
100: if (MBBanLocalServiceUtil.hasBan(category.getGroupId(),
101: permissionChecker.getUserId())) {
102:
103: return false;
104: } else {
105: long categoryId = category.getCategoryId();
106:
107: while (categoryId != MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
108:
109: category = MBCategoryLocalServiceUtil
110: .getCategory(categoryId);
111:
112: categoryId = category.getParentCategoryId();
113:
114: if (permissionChecker.hasPermission(category
115: .getGroupId(), MBCategory.class.getName(),
116: category.getCategoryId(), actionId)) {
117:
118: return true;
119: }
120:
121: if (actionId.equals(ActionKeys.VIEW)) {
122: break;
123: }
124: }
125: }
126:
127: return false;
128: }
129:
130: }
|