001    /**
002     * Copyright (c) 2000-2010 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.model.ShoppingItem;
026    import com.liferay.portlet.shopping.service.ShoppingItemLocalServiceUtil;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class ShoppingItemPermission {
032    
033            public static void check(
034                            PermissionChecker permissionChecker, long itemId, String actionId)
035                    throws PortalException, SystemException {
036    
037                    if (!contains(permissionChecker, itemId, actionId)) {
038                            throw new PrincipalException();
039                    }
040            }
041    
042            public static void check(
043                            PermissionChecker permissionChecker, ShoppingItem item,
044                            String actionId)
045                    throws PortalException, SystemException {
046    
047                    if (!contains(permissionChecker, item, actionId)) {
048                            throw new PrincipalException();
049                    }
050            }
051    
052            public static boolean contains(
053                            PermissionChecker permissionChecker, long itemId, String actionId)
054                    throws PortalException, SystemException {
055    
056                    ShoppingItem item = ShoppingItemLocalServiceUtil.getItem(itemId);
057    
058                    return contains(permissionChecker, item, actionId);
059            }
060    
061            public static boolean contains(
062                            PermissionChecker permissionChecker, ShoppingItem item,
063                            String actionId)
064                    throws PortalException, SystemException {
065    
066                    if (PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
067                            if (item.getCategoryId() !=
068                                            ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
069    
070                                    ShoppingCategory category = item.getCategory();
071    
072                                    if (!ShoppingCategoryPermission.contains(
073                                                    permissionChecker, category, ActionKeys.VIEW)) {
074    
075                                            return false;
076                                    }
077                            }
078                    }
079    
080                    if (permissionChecker.hasOwnerPermission(
081                                    item.getCompanyId(), ShoppingItem.class.getName(),
082                                    item.getItemId(), item.getUserId(), actionId)) {
083    
084                            return true;
085                    }
086    
087                    return permissionChecker.hasPermission(
088                            item.getGroupId(), ShoppingItem.class.getName(), item.getItemId(),
089                            actionId);
090            }
091    
092    }