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.documentlibrary.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 DLPermission {
032    
033            public static final String RESOURCE_NAME =
034                    "com.liferay.portlet.documentlibrary";
035    
036            public static void check(
037                            PermissionChecker permissionChecker, long groupId, String actionId)
038                    throws PortalException {
039    
040                    if (!contains(permissionChecker, groupId, actionId)) {
041                            throw new PrincipalException();
042                    }
043            }
044    
045            public static boolean contains(
046                    PermissionChecker permissionChecker, long groupId, String actionId) {
047    
048                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
049                            permissionChecker, groupId, RESOURCE_NAME, groupId,
050                            PortletKeys.DOCUMENT_LIBRARY, actionId);
051    
052                    if (hasPermission != null) {
053                            return hasPermission.booleanValue();
054                    }
055    
056                    try {
057                            int count =
058                                    ResourcePermissionLocalServiceUtil.getResourcePermissionsCount(
059                                            permissionChecker.getCompanyId(), RESOURCE_NAME,
060                                            ResourceConstants.SCOPE_INDIVIDUAL,
061                                            String.valueOf(groupId));
062    
063                            if (count == 0) {
064                                    ResourceLocalServiceUtil.addResources(
065                                            permissionChecker.getCompanyId(), groupId, 0, RESOURCE_NAME,
066                                            groupId, false, true, true);
067                            }
068                    }
069                    catch (Exception e) {
070                            if (_log.isWarnEnabled()) {
071                                    _log.warn(e, e);
072                            }
073                    }
074    
075                    return permissionChecker.hasPermission(
076                            groupId, RESOURCE_NAME, groupId, actionId);
077            }
078    
079            private static Log _log = LogFactoryUtil.getLog(DLPermission.class);
080    
081    }