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.portal.service.permission.PortletPermissionUtil;
31 import com.liferay.portal.util.PortletKeys;
32 import com.liferay.portlet.messageboards.model.MBCategory;
33 import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
34 import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
35 import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
36
37
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 else {
115 long categoryId = category.getCategoryId();
116
117 while (categoryId !=
118 MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
119
120 category = MBCategoryLocalServiceUtil.getCategory(categoryId);
121
122 categoryId = category.getParentCategoryId();
123
124 if (permissionChecker.hasPermission(
125 category.getGroupId(), MBCategory.class.getName(),
126 category.getCategoryId(), actionId)) {
127
128 return true;
129 }
130
131 if (actionId.equals(ActionKeys.VIEW)) {
132 break;
133 }
134 }
135 }
136
137 return false;
138 }
139
140 }