1
19
20 package com.liferay.portlet.messageboards.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.messageboards.model.MBCategory;
30 import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
31 import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
32 import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
33
34
40 public class MBCategoryPermission {
41
42 public static void check(
43 PermissionChecker permissionChecker, long plid, long categoryId,
44 String actionId)
45 throws PortalException, SystemException {
46
47 if (!contains(permissionChecker, plid, categoryId, actionId)) {
48 throw new PrincipalException();
49 }
50 }
51
52 public static void check(
53 PermissionChecker permissionChecker, long categoryId,
54 String actionId)
55 throws PortalException, SystemException {
56
57 if (!contains(permissionChecker, categoryId, actionId)) {
58 throw new PrincipalException();
59 }
60 }
61
62 public static void check(
63 PermissionChecker permissionChecker, MBCategory category,
64 String actionId)
65 throws PortalException, SystemException {
66
67 if (!contains(permissionChecker, category, actionId)) {
68 throw new PrincipalException();
69 }
70 }
71
72 public static boolean contains(
73 PermissionChecker permissionChecker, long plid, long categoryId,
74 String actionId)
75 throws PortalException, SystemException {
76
77 if (categoryId == MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
78 return PortletPermissionUtil.contains(
79 permissionChecker, plid, PortletKeys.MESSAGE_BOARDS, 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 if (permissionChecker.hasOwnerPermission(
116 category.getCompanyId(), MBCategory.class.getName(),
117 category.getCategoryId(), category.getUserId(), actionId)) {
118
119 return true;
120 }
121
122 if (permissionChecker.hasPermission(
123 category.getGroupId(), MBCategory.class.getName(),
124 category.getCategoryId(), actionId)) {
125
126 return true;
127 }
128
129 if (actionId.equals(ActionKeys.VIEW)) {
130 break;
131 }
132
133 category = MBCategoryLocalServiceUtil.getCategory(categoryId);
134
135 categoryId = category.getParentCategoryId();
136 }
137
138 return false;
139 }
140
141 }