1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.imagegallery.service.permission;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.security.auth.PrincipalException;
28  import com.liferay.portal.security.permission.ActionKeys;
29  import com.liferay.portal.security.permission.PermissionChecker;
30  import com.liferay.portal.service.permission.PortletPermissionUtil;
31  import com.liferay.portal.util.PortletKeys;
32  import com.liferay.portal.util.PropsValues;
33  import com.liferay.portlet.imagegallery.model.IGFolder;
34  import com.liferay.portlet.imagegallery.model.impl.IGFolderImpl;
35  import com.liferay.portlet.imagegallery.service.IGFolderLocalServiceUtil;
36  
37  /**
38   * <a href="IGFolderPermission.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class IGFolderPermission {
43  
44      public static void check(
45              PermissionChecker permissionChecker, long plid, long folderId,
46              String actionId)
47          throws PortalException, SystemException {
48  
49          if (!contains(permissionChecker, plid, folderId, actionId)) {
50              throw new PrincipalException();
51          }
52      }
53  
54      public static void check(
55              PermissionChecker permissionChecker, long folderId, String actionId)
56          throws PortalException, SystemException {
57  
58          if (!contains(permissionChecker, folderId, actionId)) {
59              throw new PrincipalException();
60          }
61      }
62  
63      public static void check(
64              PermissionChecker permissionChecker, IGFolder folder,
65              String actionId)
66          throws PortalException, SystemException {
67  
68          if (!contains(permissionChecker, folder, actionId)) {
69              throw new PrincipalException();
70          }
71      }
72  
73      public static boolean contains(
74              PermissionChecker permissionChecker, long plid, long folderId,
75              String actionId)
76          throws PortalException, SystemException {
77  
78          if (folderId == IGFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
79  
80              return PortletPermissionUtil.contains(
81                  permissionChecker, plid, PortletKeys.IMAGE_GALLERY, actionId);
82          }
83          else {
84              return contains(permissionChecker, folderId, actionId);
85          }
86      }
87  
88      public static boolean contains(
89              PermissionChecker permissionChecker, long folderId, String actionId)
90          throws PortalException, SystemException {
91  
92          IGFolder folder = IGFolderLocalServiceUtil.getFolder(folderId);
93  
94          return contains(permissionChecker, folder, actionId);
95      }
96  
97      public static boolean contains(
98              PermissionChecker permissionChecker, IGFolder folder,
99              String actionId)
100         throws PortalException, SystemException {
101 
102         if (actionId.equals(ActionKeys.ADD_FOLDER)) {
103             actionId = ActionKeys.ADD_SUBFOLDER;
104         }
105 
106         long folderId = folder.getFolderId();
107 
108         if (actionId.equals(ActionKeys.VIEW)) {
109             while (folderId != IGFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
110                 folder = IGFolderLocalServiceUtil.getFolder(folderId);
111 
112                 folderId = folder.getParentFolderId();
113 
114                 if (!permissionChecker.hasOwnerPermission(
115                         folder.getCompanyId(), IGFolder.class.getName(),
116                         folder.getFolderId(), folder.getUserId(), actionId) &&
117                     !permissionChecker.hasPermission(
118                         folder.getGroupId(), IGFolder.class.getName(),
119                         folder.getFolderId(), actionId)) {
120 
121                     return false;
122                 }
123 
124                 if (!PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
125                     break;
126                 }
127             }
128 
129             return true;
130         }
131         else {
132             while (folderId != IGFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
133                 if (permissionChecker.hasOwnerPermission(
134                         folder.getCompanyId(), IGFolder.class.getName(),
135                         folder.getFolderId(), folder.getUserId(), actionId)) {
136 
137                     return true;
138                 }
139 
140                 if (permissionChecker.hasPermission(
141                         folder.getGroupId(), IGFolder.class.getName(),
142                         folder.getFolderId(), actionId)) {
143 
144                     return true;
145                 }
146 
147                 if (actionId.equals(ActionKeys.VIEW)) {
148                     break;
149                 }
150 
151                 folder = IGFolderLocalServiceUtil.getFolder(folderId);
152 
153                 folderId = folder.getParentFolderId();
154             }
155 
156             return false;
157         }
158     }
159 
160 }