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.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                    if (actionId.equals(ActionKeys.VIEW) &&
081                            PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
082    
083                            long categoryId = category.getCategoryId();
084    
085                            while (categoryId !=
086                                                    ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
087    
088                                    category = ShoppingCategoryLocalServiceUtil.getCategory(
089                                            categoryId);
090    
091                                    if (!_hasPermission(permissionChecker, category, actionId)) {
092                                            return false;
093                                    }
094    
095                                    categoryId = category.getParentCategoryId();
096                            }
097    
098                            return ShoppingPermission.contains(
099                                    permissionChecker, category.getGroupId(), actionId);
100                    }
101    
102                    return _hasPermission(permissionChecker, category, actionId);
103            }
104    
105            private static boolean _hasPermission(
106                    PermissionChecker permissionChecker, ShoppingCategory category,
107                    String actionId) {
108    
109                    if (permissionChecker.hasOwnerPermission(
110                                    category.getCompanyId(), ShoppingCategory.class.getName(),
111                                    category.getCategoryId(), category.getUserId(), actionId) ||
112                            permissionChecker.hasPermission(
113                                    category.getGroupId(), ShoppingCategory.class.getName(),
114                                    category.getCategoryId(), actionId)) {
115    
116                            return true;
117                    }
118    
119                    return false;
120            }
121    
122    }