001    /**
002     * Copyright (c) 2000-present 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.softwarecatalog.service.permission;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
019    import com.liferay.portal.security.auth.PrincipalException;
020    import com.liferay.portal.security.permission.BaseModelPermissionChecker;
021    import com.liferay.portal.security.permission.PermissionChecker;
022    import com.liferay.portlet.softwarecatalog.model.SCProductEntry;
023    import com.liferay.portlet.softwarecatalog.service.SCProductEntryLocalServiceUtil;
024    
025    /**
026     * @author Jorge Ferrer
027     * @author Brian Wing Shun Chan
028     */
029    @OSGiBeanProperties(
030            property = {
031                    "model.class.name=" +
032                            "com.liferay.portlet.softwarecatalog.model.SCProductEntry"
033            }
034    )
035    public class SCProductEntryPermission implements BaseModelPermissionChecker {
036    
037            public static void check(
038                            PermissionChecker permissionChecker, long productEntryId,
039                            String actionId)
040                    throws PortalException {
041    
042                    if (!contains(permissionChecker, productEntryId, actionId)) {
043                            throw new PrincipalException();
044                    }
045            }
046    
047            public static void check(
048                            PermissionChecker permissionChecker, SCProductEntry productEntry,
049                            String actionId)
050                    throws PortalException {
051    
052                    if (!contains(permissionChecker, productEntry, actionId)) {
053                            throw new PrincipalException();
054                    }
055            }
056    
057            public static boolean contains(
058                            PermissionChecker permissionChecker, long productEntryId,
059                            String actionId)
060                    throws PortalException {
061    
062                    SCProductEntry productEntry =
063                            SCProductEntryLocalServiceUtil.getProductEntry(productEntryId);
064    
065                    return contains(permissionChecker, productEntry, actionId);
066            }
067    
068            public static boolean contains(
069                    PermissionChecker permissionChecker, SCProductEntry productEntry,
070                    String actionId) {
071    
072                    if (permissionChecker.hasOwnerPermission(
073                                    productEntry.getCompanyId(), SCProductEntry.class.getName(),
074                                    productEntry.getProductEntryId(), productEntry.getUserId(),
075                                    actionId)) {
076    
077                            return true;
078                    }
079    
080                    return permissionChecker.hasPermission(
081                            productEntry.getGroupId(), SCProductEntry.class.getName(),
082                            productEntry.getProductEntryId(), actionId);
083            }
084    
085            @Override
086            public void checkBaseModel(
087                            PermissionChecker permissionChecker, long groupId, long primaryKey,
088                            String actionId)
089                    throws PortalException {
090    
091                    check(permissionChecker, primaryKey, actionId);
092            }
093    
094    }