001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.shopping.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.shopping.model.ShoppingCategory;
024    import com.liferay.portlet.shopping.model.ShoppingCategoryConstants;
025    import com.liferay.portlet.shopping.service.ShoppingCategoryLocalServiceUtil;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class ShoppingCategoryPermission {
031    
032            public static void check(
033                            PermissionChecker permissionChecker, long groupId, long categoryId,
034                            String actionId)
035                    throws PortalException, SystemException {
036    
037                    if (!contains(permissionChecker, groupId, categoryId, actionId)) {
038                            throw new PrincipalException();
039                    }
040            }
041    
042            public static void check(
043                            PermissionChecker permissionChecker, ShoppingCategory category,
044                            String actionId)
045                    throws PortalException, SystemException {
046    
047                    if (!contains(permissionChecker, category, actionId)) {
048                            throw new PrincipalException();
049                    }
050            }
051    
052            public static boolean contains(
053                            PermissionChecker permissionChecker, long groupId, long categoryId,
054                            String actionId)
055                    throws PortalException, SystemException {
056    
057                    if (categoryId ==
058                                    ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
059    
060                            return ShoppingPermission.contains(
061                                    permissionChecker, groupId, actionId);
062                    }
063                    else {
064                            ShoppingCategory category =
065                                    ShoppingCategoryLocalServiceUtil.getCategory(categoryId);
066    
067                            return contains(permissionChecker, category, actionId);
068                    }
069            }
070    
071            public static boolean contains(
072                            PermissionChecker permissionChecker, ShoppingCategory category,
073                            String actionId)
074                    throws PortalException, SystemException {
075    
076                    if (actionId.equals(ActionKeys.ADD_CATEGORY)) {
077                            actionId = ActionKeys.ADD_SUBCATEGORY;
078                    }
079    
080                    long categoryId = category.getCategoryId();
081    
082                    if (actionId.equals(ActionKeys.VIEW)) {
083                            while (categoryId !=
084                                            ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
085    
086                                    category = ShoppingCategoryLocalServiceUtil.getCategory(
087                                            categoryId);
088    
089                                    categoryId = category.getParentCategoryId();
090    
091                                    if (!permissionChecker.hasOwnerPermission(
092                                                    category.getCompanyId(),
093                                                    ShoppingCategory.class.getName(),
094                                                    category.getCategoryId(), category.getUserId(),
095                                                    actionId) &&
096                                            !permissionChecker.hasPermission(
097                                                    category.getGroupId(), ShoppingCategory.class.getName(),
098                                                    category.getCategoryId(), actionId)) {
099    
100                                            return false;
101                                    }
102    
103                                    if (!PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
104                                            break;
105                                    }
106                            }
107    
108                            return true;
109                    }
110                    else {
111                            while (categoryId !=
112                                            ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
113    
114                                    if (permissionChecker.hasOwnerPermission(
115                                                    category.getCompanyId(),
116                                                    ShoppingCategory.class.getName(),
117                                                    category.getCategoryId(), category.getUserId(),
118                                                    actionId)) {
119    
120                                            return true;
121                                    }
122    
123                                    if (permissionChecker.hasPermission(
124                                                    category.getGroupId(), ShoppingCategory.class.getName(),
125                                                    category.getCategoryId(), actionId)) {
126    
127                                            return true;
128                                    }
129    
130                                    if (actionId.equals(ActionKeys.VIEW)) {
131                                            break;
132                                    }
133    
134                                    category = ShoppingCategoryLocalServiceUtil.getCategory(
135                                            categoryId);
136    
137                                    categoryId = category.getParentCategoryId();
138                            }
139    
140                            return false;
141                    }
142            }
143    
144    }