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.SCFrameworkVersion;
023    import com.liferay.portlet.softwarecatalog.service.SCFrameworkVersionLocalServiceUtil;
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.SCFrameworkVersion"
033            }
034    )
035    public class SCFrameworkVersionPermission
036            implements BaseModelPermissionChecker {
037    
038            public static void check(
039                            PermissionChecker permissionChecker, long frameworkVersionId,
040                            String actionId)
041                    throws PortalException {
042    
043                    if (!contains(permissionChecker, frameworkVersionId, actionId)) {
044                            throw new PrincipalException();
045                    }
046            }
047    
048            public static void check(
049                            PermissionChecker permissionChecker,
050                            SCFrameworkVersion frameworkVersion, String actionId)
051                    throws PortalException {
052    
053                    if (!contains(permissionChecker, frameworkVersion, actionId)) {
054                            throw new PrincipalException();
055                    }
056            }
057    
058            public static boolean contains(
059                            PermissionChecker permissionChecker, long frameworkVersionId,
060                            String actionId)
061                    throws PortalException {
062    
063                    SCFrameworkVersion frameworkVersion =
064                            SCFrameworkVersionLocalServiceUtil.getFrameworkVersion(
065                                    frameworkVersionId);
066    
067                    return contains(permissionChecker, frameworkVersion, actionId);
068            }
069    
070            public static boolean contains(
071                    PermissionChecker permissionChecker,
072                    SCFrameworkVersion frameworkVersion, String actionId) {
073    
074                    if (permissionChecker.hasOwnerPermission(
075                                    frameworkVersion.getCompanyId(),
076                                    SCFrameworkVersion.class.getName(),
077                                    frameworkVersion.getFrameworkVersionId(),
078                                    frameworkVersion.getUserId(), actionId)) {
079    
080                            return true;
081                    }
082    
083                    return permissionChecker.hasPermission(
084                            frameworkVersion.getGroupId(), SCFrameworkVersion.class.getName(),
085                            frameworkVersion.getFrameworkVersionId(), actionId);
086            }
087    
088            @Override
089            public void checkBaseModel(
090                            PermissionChecker permissionChecker, long groupId, long primaryKey,
091                            String actionId)
092                    throws PortalException {
093    
094                    check(permissionChecker, primaryKey, actionId);
095            }
096    
097    }