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.journal.service.permission;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.staging.permission.StagingPermissionUtil;
021    import com.liferay.portal.model.ResourceConstants;
022    import com.liferay.portal.security.auth.PrincipalException;
023    import com.liferay.portal.security.permission.PermissionChecker;
024    import com.liferay.portal.service.ResourceLocalServiceUtil;
025    import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
026    import com.liferay.portal.util.PortletKeys;
027    
028    /**
029     * @author Jorge Ferrer
030     */
031    public class JournalPermission {
032    
033            public static final String RESOURCE_NAME = "com.liferay.portlet.journal";
034    
035            public static void check(
036                            PermissionChecker permissionChecker, long groupId, String actionId)
037                    throws PortalException {
038    
039                    if (!contains(permissionChecker, groupId, actionId)) {
040                            throw new PrincipalException();
041                    }
042            }
043    
044            public static boolean contains(
045                    PermissionChecker permissionChecker, long groupId, String actionId) {
046    
047                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
048                            permissionChecker, groupId, RESOURCE_NAME, groupId,
049                            PortletKeys.JOURNAL, actionId);
050    
051                    if (hasPermission != null) {
052                            return hasPermission.booleanValue();
053                    }
054    
055                    try {
056                            int count =
057                                    ResourcePermissionLocalServiceUtil.getResourcePermissionsCount(
058                                            permissionChecker.getCompanyId(), RESOURCE_NAME,
059                                            ResourceConstants.SCOPE_INDIVIDUAL,
060                                            String.valueOf(groupId));
061    
062                            if (count == 0) {
063                                    ResourceLocalServiceUtil.addResources(
064                                            permissionChecker.getCompanyId(), groupId, 0, RESOURCE_NAME,
065                                            groupId, false, true, true);
066                            }
067                    }
068                    catch (Exception e) {
069                            if (_log.isWarnEnabled()) {
070                                    _log.warn(e, e);
071                            }
072                    }
073    
074                    return permissionChecker.hasPermission(
075                            groupId, RESOURCE_NAME, groupId, actionId);
076            }
077    
078            private static Log _log = LogFactoryUtil.getLog(JournalPermission.class);
079    
080    }