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