1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.imagegallery.service.permission;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.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.IGFolderConstants;
25  import com.liferay.portlet.imagegallery.model.IGImage;
26  import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
27  
28  /**
29   * <a href="IGImagePermission.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   */
33  public class IGImagePermission {
34  
35      public static void check(
36              PermissionChecker permissionChecker, IGImage image, String actionId)
37          throws PortalException, SystemException {
38  
39          if (!contains(permissionChecker, image, actionId)) {
40              throw new PrincipalException();
41          }
42      }
43  
44      public static void check(
45              PermissionChecker permissionChecker, long imageId, String actionId)
46          throws PortalException, SystemException {
47  
48          if (!contains(permissionChecker, imageId, actionId)) {
49              throw new PrincipalException();
50          }
51      }
52  
53      public static boolean contains(
54              PermissionChecker permissionChecker, IGImage image, String actionId)
55          throws PortalException, SystemException {
56  
57          if (PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
58              if (image.getFolderId() !=
59                      IGFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
60  
61                  IGFolder folder = image.getFolder();
62  
63                  if (!IGFolderPermission.contains(
64                          permissionChecker, folder, ActionKeys.ACCESS) &&
65                      !IGFolderPermission.contains(
66                          permissionChecker, folder, ActionKeys.VIEW)) {
67  
68                      return false;
69                  }
70              }
71          }
72  
73          if (permissionChecker.hasOwnerPermission(
74                  image.getCompanyId(), IGImage.class.getName(),
75                  image.getImageId(), image.getUserId(), actionId)) {
76  
77              return true;
78          }
79  
80          return permissionChecker.hasPermission(
81              image.getGroupId(), IGImage.class.getName(), image.getImageId(),
82              actionId);
83      }
84  
85      public static boolean contains(
86              PermissionChecker permissionChecker, long imageId, String actionId)
87          throws PortalException, SystemException {
88  
89          IGImage image = IGImageLocalServiceUtil.getImage(imageId);
90  
91          return contains(permissionChecker, image, actionId);
92      }
93  
94  }