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.portlet.shopping.model.ShoppingOrder;
023    import com.liferay.portlet.shopping.service.ShoppingOrderLocalServiceUtil;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class ShoppingOrderPermission {
029    
030            public static void check(
031                            PermissionChecker permissionChecker, long groupId, long orderId,
032                            String actionId)
033                    throws PortalException, SystemException {
034    
035                    if (!contains(permissionChecker, groupId, orderId, actionId)) {
036                            throw new PrincipalException();
037                    }
038            }
039    
040            public static void check(
041                            PermissionChecker permissionChecker, long groupId,
042                            ShoppingOrder order, String actionId)
043                    throws PortalException {
044    
045                    if (!contains(permissionChecker, groupId, order, actionId)) {
046                            throw new PrincipalException();
047                    }
048            }
049    
050            public static boolean contains(
051                            PermissionChecker permissionChecker, long groupId, long orderId,
052                            String actionId)
053                    throws PortalException, SystemException {
054    
055                    ShoppingOrder order = ShoppingOrderLocalServiceUtil.getOrder(orderId);
056    
057                    return contains(permissionChecker, groupId, order, actionId);
058            }
059    
060            public static boolean contains(
061                    PermissionChecker permissionChecker, long groupId, ShoppingOrder order,
062                    String actionId) {
063    
064                    if (ShoppingPermission.contains(
065                                    permissionChecker, groupId, ActionKeys.MANAGE_ORDERS)) {
066    
067                            return true;
068                    }
069                    else {
070                            if (permissionChecker.hasOwnerPermission(
071                                            order.getCompanyId(), ShoppingOrder.class.getName(),
072                                            order.getOrderId(), order.getUserId(), actionId)) {
073    
074                                    return true;
075                            }
076    
077                            return permissionChecker.hasPermission(
078                                    order.getGroupId(), ShoppingOrder.class.getName(),
079                                    order.getOrderId(), actionId);
080                    }
081            }
082    
083    }