1
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.portlet.messageboards.model.MBCategory;
31 import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
32 import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
33 import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
34
35
41 public class MBCategoryPermission {
42
43 public static void check(
44 PermissionChecker permissionChecker, long groupId, long categoryId,
45 String actionId)
46 throws PortalException, SystemException {
47
48 if (!contains(permissionChecker, groupId, categoryId, actionId)) {
49 throw new PrincipalException();
50 }
51 }
52
53 public static void check(
54 PermissionChecker permissionChecker, long categoryId,
55 String actionId)
56 throws PortalException, SystemException {
57
58 if (!contains(permissionChecker, categoryId, actionId)) {
59 throw new PrincipalException();
60 }
61 }
62
63 public static void check(
64 PermissionChecker permissionChecker, MBCategory category,
65 String actionId)
66 throws PortalException, SystemException {
67
68 if (!contains(permissionChecker, category, actionId)) {
69 throw new PrincipalException();
70 }
71 }
72
73 public static boolean contains(
74 PermissionChecker permissionChecker, long groupId, long categoryId,
75 String actionId)
76 throws PortalException, SystemException {
77
78 if (categoryId == MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
79 return MBPermission.contains(permissionChecker, groupId, actionId);
80 }
81 else {
82 return contains(permissionChecker, categoryId, actionId);
83 }
84 }
85
86 public static boolean contains(
87 PermissionChecker permissionChecker, long categoryId,
88 String actionId)
89 throws PortalException, SystemException {
90
91 MBCategory category =
92 MBCategoryLocalServiceUtil.getCategory(categoryId);
93
94 return contains(permissionChecker, category, actionId);
95 }
96
97 public static boolean contains(
98 PermissionChecker permissionChecker, MBCategory category,
99 String actionId)
100 throws PortalException, SystemException {
101
102 if (actionId.equals(ActionKeys.ADD_CATEGORY)) {
103 actionId = ActionKeys.ADD_SUBCATEGORY;
104 }
105
106 if (MBBanLocalServiceUtil.hasBan(
107 category.getGroupId(), permissionChecker.getUserId())) {
108
109 return false;
110 }
111
112 long categoryId = category.getCategoryId();
113
114 while (categoryId != MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
115 category = MBCategoryLocalServiceUtil.getCategory(categoryId);
116
117 categoryId = category.getParentCategoryId();
118
119 if (permissionChecker.hasOwnerPermission(
120 category.getCompanyId(), MBCategory.class.getName(),
121 category.getCategoryId(), category.getUserId(), actionId)) {
122
123 return true;
124 }
125
126 if (permissionChecker.hasPermission(
127 category.getGroupId(), MBCategory.class.getName(),
128 category.getCategoryId(), actionId)) {
129
130 return true;
131 }
132
133 if (actionId.equals(ActionKeys.VIEW)) {
134 break;
135 }
136 }
137
138 return false;
139 }
140
141 }