1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portlet.imagegallery.service.permission;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.security.auth.PrincipalException;
20  import com.liferay.portal.security.permission.ActionKeys;
21  import com.liferay.portal.security.permission.PermissionChecker;
22  import com.liferay.portal.util.PropsValues;
23  import com.liferay.portlet.imagegallery.model.IGFolder;
24  import com.liferay.portlet.imagegallery.model.IGImage;
25  import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
26  
27  /**
28   * <a href="IGImagePermission.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class IGImagePermission {
33  
34      public static void check(
35              PermissionChecker permissionChecker, IGImage image, String actionId)
36          throws PortalException, SystemException {
37  
38          if (!contains(permissionChecker, image, actionId)) {
39              throw new PrincipalException();
40          }
41      }
42  
43      public static void check(
44              PermissionChecker permissionChecker, long imageId, String actionId)
45          throws PortalException, SystemException {
46  
47          if (!contains(permissionChecker, imageId, actionId)) {
48              throw new PrincipalException();
49          }
50      }
51  
52      public static boolean contains(
53              PermissionChecker permissionChecker, IGImage image, String actionId)
54          throws PortalException, SystemException {
55  
56          IGFolder folder = image.getFolder();
57  
58          if (PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
59              if (!IGFolderPermission.contains(
60                      permissionChecker, folder, ActionKeys.VIEW)) {
61  
62                  return false;
63              }
64          }
65  
66          if (permissionChecker.hasOwnerPermission(
67                  image.getCompanyId(), IGImage.class.getName(),
68                  image.getImageId(), image.getUserId(), actionId)) {
69  
70              return true;
71          }
72  
73          return permissionChecker.hasPermission(
74              folder.getGroupId(), IGImage.class.getName(), image.getImageId(),
75              actionId);
76      }
77  
78      public static boolean contains(
79              PermissionChecker permissionChecker, long imageId, String actionId)
80          throws PortalException, SystemException {
81  
82          IGImage image = IGImageLocalServiceUtil.getImage(imageId);
83  
84          return contains(permissionChecker, image, actionId);
85      }
86  
87  }