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.journal.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.kernel.staging.permission.StagingPermissionUtil;
020    import com.liferay.portal.security.auth.PrincipalException;
021    import com.liferay.portal.security.permission.ActionKeys;
022    import com.liferay.portal.security.permission.BaseModelPermissionChecker;
023    import com.liferay.portal.security.permission.PermissionChecker;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portal.util.PropsValues;
026    import com.liferay.portlet.journal.NoSuchFolderException;
027    import com.liferay.portlet.journal.model.JournalFolder;
028    import com.liferay.portlet.journal.model.JournalFolderConstants;
029    import com.liferay.portlet.journal.service.JournalFolderLocalServiceUtil;
030    
031    /**
032     * @author Juan Fern??ndez
033     * @author Zsolt Berentey
034     */
035    @OSGiBeanProperties(
036            property = {
037                    "model.class.name=com.liferay.portlet.journal.model.JournalFolder"
038            }
039    )
040    public class JournalFolderPermission implements BaseModelPermissionChecker {
041    
042            public static void check(
043                            PermissionChecker permissionChecker, JournalFolder folder,
044                            String actionId)
045                    throws PortalException {
046    
047                    if (!contains(permissionChecker, folder, actionId)) {
048                            throw new PrincipalException();
049                    }
050            }
051    
052            public static void check(
053                            PermissionChecker permissionChecker, long groupId, long folderId,
054                            String actionId)
055                    throws PortalException {
056    
057                    if (!contains(permissionChecker, groupId, folderId, actionId)) {
058                            throw new PrincipalException();
059                    }
060            }
061    
062            public static boolean contains(
063                            PermissionChecker permissionChecker, JournalFolder folder,
064                            String actionId)
065                    throws PortalException {
066    
067                    if (actionId.equals(ActionKeys.ADD_FOLDER)) {
068                            actionId = ActionKeys.ADD_SUBFOLDER;
069                    }
070    
071                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
072                            permissionChecker, folder.getGroupId(),
073                            JournalFolder.class.getName(), folder.getFolderId(),
074                            PortletKeys.JOURNAL, actionId);
075    
076                    if (hasPermission != null) {
077                            return hasPermission.booleanValue();
078                    }
079    
080                    if (actionId.equals(ActionKeys.VIEW) &&
081                            PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
082    
083                            try {
084                                    long folderId = folder.getFolderId();
085    
086                                    while (folderId !=
087                                                            JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
088    
089                                            folder = JournalFolderLocalServiceUtil.getFolder(folderId);
090    
091                                            if (!_hasPermission(permissionChecker, folder, actionId)) {
092                                                    return false;
093                                            }
094    
095                                            folderId = folder.getParentFolderId();
096                                    }
097                            }
098                            catch (NoSuchFolderException nsfe) {
099                                    if (!folder.isInTrash()) {
100                                            throw nsfe;
101                                    }
102                            }
103    
104                            return JournalPermission.contains(
105                                    permissionChecker, folder.getGroupId(), actionId);
106                    }
107    
108                    return _hasPermission(permissionChecker, folder, actionId);
109            }
110    
111            public static boolean contains(
112                            PermissionChecker permissionChecker, long groupId, long folderId,
113                            String actionId)
114                    throws PortalException {
115    
116                    if (folderId == JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
117                            return JournalPermission.contains(
118                                    permissionChecker, groupId, actionId);
119                    }
120                    else {
121                            JournalFolder folder =
122                                    JournalFolderLocalServiceUtil.getJournalFolder(folderId);
123    
124                            return contains(permissionChecker, folder, actionId);
125                    }
126            }
127    
128            @Override
129            public void checkBaseModel(
130                            PermissionChecker permissionChecker, long groupId, long primaryKey,
131                            String actionId)
132                    throws PortalException {
133    
134                    check(permissionChecker, groupId, primaryKey, actionId);
135            }
136    
137            private static boolean _hasPermission(
138                    PermissionChecker permissionChecker, JournalFolder folder,
139                    String actionId) {
140    
141                    if (permissionChecker.hasOwnerPermission(
142                                    folder.getCompanyId(), JournalFolder.class.getName(),
143                                    folder.getFolderId(), folder.getUserId(), actionId) ||
144                            permissionChecker.hasPermission(
145                                    folder.getGroupId(), JournalFolder.class.getName(),
146                                    folder.getFolderId(), actionId)) {
147    
148                            return true;
149                    }
150    
151                    return false;
152            }
153    
154    }