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.messageboards.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.messageboards.model.MBCategory;
34  import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
35  import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
36  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
37  
38  /**
39   * <a href="MBCategoryPermission.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   */
43  public class MBCategoryPermission {
44  
45      public static void check(
46              PermissionChecker permissionChecker, long plid, long categoryId,
47              String actionId)
48          throws PortalException, SystemException {
49  
50          if (!contains(permissionChecker, plid, categoryId, actionId)) {
51              throw new PrincipalException();
52          }
53      }
54  
55      public static void check(
56              PermissionChecker permissionChecker, long categoryId,
57              String actionId)
58          throws PortalException, SystemException {
59  
60          if (!contains(permissionChecker, categoryId, actionId)) {
61              throw new PrincipalException();
62          }
63      }
64  
65      public static void check(
66              PermissionChecker permissionChecker, MBCategory category,
67              String actionId)
68          throws PortalException, SystemException {
69  
70          if (!contains(permissionChecker, category, actionId)) {
71              throw new PrincipalException();
72          }
73      }
74  
75      public static boolean contains(
76              PermissionChecker permissionChecker, long plid, long categoryId,
77              String actionId)
78          throws PortalException, SystemException {
79  
80          if (categoryId == MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
81              return PortletPermissionUtil.contains(
82                  permissionChecker, plid, PortletKeys.MESSAGE_BOARDS, actionId);
83          }
84          else {
85              return contains(permissionChecker, categoryId, actionId);
86          }
87      }
88  
89      public static boolean contains(
90              PermissionChecker permissionChecker, long categoryId,
91              String actionId)
92          throws PortalException, SystemException {
93  
94          MBCategory category =
95              MBCategoryLocalServiceUtil.getCategory(categoryId);
96  
97          return contains(permissionChecker, category, actionId);
98      }
99  
100     public static boolean contains(
101             PermissionChecker permissionChecker, MBCategory category,
102             String actionId)
103         throws PortalException, SystemException {
104 
105         if (actionId.equals(ActionKeys.ADD_CATEGORY)) {
106             actionId = ActionKeys.ADD_SUBCATEGORY;
107         }
108 
109         if (MBBanLocalServiceUtil.hasBan(
110                 category.getGroupId(), permissionChecker.getUserId())) {
111 
112             return false;
113         }
114 
115         long categoryId = category.getCategoryId();
116 
117         if (actionId.equals(ActionKeys.VIEW)) {
118             while (categoryId != MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
119                 category = MBCategoryLocalServiceUtil.getCategory(categoryId);
120 
121                 categoryId = category.getParentCategoryId();
122 
123                 if (!permissionChecker.hasOwnerPermission(
124                         category.getCompanyId(), MBCategory.class.getName(),
125                         category.getCategoryId(), category.getUserId(),
126                         actionId) &&
127                     !permissionChecker.hasPermission(
128                         category.getGroupId(), MBCategory.class.getName(),
129                         category.getCategoryId(), actionId)) {
130 
131                     return false;
132                 }
133 
134                 if (!PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
135                     break;
136                 }
137             }
138 
139             return true;
140         }
141         else {
142             while (categoryId != MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
143                 if (permissionChecker.hasOwnerPermission(
144                         category.getCompanyId(), MBCategory.class.getName(),
145                         category.getCategoryId(), category.getUserId(),
146                         actionId)) {
147 
148                     return true;
149                 }
150 
151                 if (permissionChecker.hasPermission(
152                         category.getGroupId(), MBCategory.class.getName(),
153                         category.getCategoryId(), actionId)) {
154 
155                     return true;
156                 }
157 
158                 if (actionId.equals(ActionKeys.VIEW)) {
159                     break;
160                 }
161 
162                 category = MBCategoryLocalServiceUtil.getCategory(categoryId);
163 
164                 categoryId = category.getParentCategoryId();
165             }
166 
167             return false;
168         }
169     }
170 
171 }