1
14
15 package com.liferay.portal.security.permission;
16
17 import com.liferay.portal.PortalException;
18 import com.liferay.portal.SystemException;
19 import com.liferay.portal.model.Group;
20 import com.liferay.portal.model.Organization;
21 import com.liferay.portal.model.OrganizationConstants;
22 import com.liferay.portal.model.Role;
23 import com.liferay.portal.model.RoleConstants;
24 import com.liferay.portal.service.OrganizationLocalServiceUtil;
25 import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
26
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
31
36 public class PermissionCheckerBagImpl implements PermissionCheckerBag {
37
38 public PermissionCheckerBagImpl() {
39 }
40
41 public PermissionCheckerBagImpl(
42 long userId, List<Group> userGroups, List<Organization> userOrgs,
43 List<Group> userOrgGroups, List<Group> userUserGroupGroups,
44 List<Group> groups, List<Role> roles) {
45
46 _userId = userId;
47 _userGroups = userGroups;
48 _userOrgs = userOrgs;
49 _userOrgGroups = userOrgGroups;
50 _userUserGroupGroups = userUserGroupGroups;
51 _groups = groups;
52 _roles = roles;
53 }
54
55 public List<Group> getUserGroups() {
56 return _userGroups;
57 }
58
59 public List<Organization> getUserOrgs() {
60 return _userOrgs;
61 }
62
63 public List<Group> getUserOrgGroups() {
64 return _userOrgGroups;
65 }
66
67 public List<Group> getUserUserGroupGroups() {
68 return _userUserGroupGroups;
69 }
70
71 public List<Group> getGroups() {
72 return _groups;
73 }
74
75 public List<Role> getRoles() {
76 return _roles;
77 }
78
79 public boolean isCommunityAdmin(
80 PermissionChecker permissionChecker, Group group)
81 throws Exception {
82
83 Boolean value = _communityAdmins.get(group.getGroupId());
84
85 if (value == null) {
86 value = Boolean.valueOf(
87 isCommunityAdminImpl(permissionChecker, group));
88
89 _communityAdmins.put(group.getGroupId(), value);
90 }
91
92 return value.booleanValue();
93 }
94
95 public boolean isCommunityOwner(
96 PermissionChecker permissionChecker, Group group)
97 throws Exception {
98
99 Boolean value = _communityOwners.get(group.getGroupId());
100
101 if (value == null) {
102 value = Boolean.valueOf(
103 isCommunityOwnerImpl(permissionChecker, group));
104
105 _communityOwners.put(group.getGroupId(), value);
106 }
107
108 return value.booleanValue();
109 }
110
111 protected boolean isCommunityAdminImpl(
112 PermissionChecker permissionChecker, Group group)
113 throws PortalException, SystemException {
114
115 if (group.isCommunity()) {
116 if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
117 _userId, group.getGroupId(),
118 RoleConstants.COMMUNITY_ADMINISTRATOR, true) ||
119 UserGroupRoleLocalServiceUtil.hasUserGroupRole(
120 _userId, group.getGroupId(),
121 RoleConstants.COMMUNITY_OWNER, true)) {
122
123 return true;
124 }
125 }
126 else if (group.isOrganization()) {
127 long organizationId = group.getClassPK();
128
129 while (organizationId !=
130 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
131
132 Organization organization =
133 OrganizationLocalServiceUtil.getOrganization(
134 organizationId);
135
136 Group organizationGroup = organization.getGroup();
137
138 long organizationGroupId = organizationGroup.getGroupId();
139
140 if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
141 _userId, organizationGroupId,
142 RoleConstants.ORGANIZATION_ADMINISTRATOR, true) ||
143 UserGroupRoleLocalServiceUtil.hasUserGroupRole(
144 _userId, organizationGroupId,
145 RoleConstants.ORGANIZATION_OWNER, true)) {
146
147 return true;
148 }
149
150 organizationId = organization.getParentOrganizationId();
151 }
152 }
153 else if (group.isUser()) {
154 long userId = group.getClassPK();
155
156 if (userId == _userId) {
157 return true;
158 }
159 }
160
161 return false;
162 }
163
164 protected boolean isCommunityOwnerImpl(
165 PermissionChecker permissionChecker, Group group)
166 throws PortalException, SystemException {
167
168 if (group.isCommunity()) {
169 if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
170 _userId, group.getGroupId(),
171 RoleConstants.COMMUNITY_OWNER, true)) {
172
173 return true;
174 }
175 }
176 else if (group.isOrganization()) {
177 long organizationId = group.getClassPK();
178
179 while (organizationId !=
180 OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
181
182 Organization organization =
183 OrganizationLocalServiceUtil.getOrganization(
184 organizationId);
185
186 Group organizationGroup = organization.getGroup();
187
188 long organizationGroupId = organizationGroup.getGroupId();
189
190 if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
191 _userId, organizationGroupId,
192 RoleConstants.ORGANIZATION_OWNER, true)) {
193
194 return true;
195 }
196
197 organizationId = organization.getParentOrganizationId();
198 }
199 }
200 else if (group.isUser()) {
201 long userId = group.getClassPK();
202
203 if (userId == _userId) {
204 return true;
205 }
206 }
207
208 return false;
209 }
210
211 private long _userId;
212 private List<Group> _userGroups;
213 private List<Organization> _userOrgs;
214 private List<Group> _userOrgGroups;
215 private List<Group> _userUserGroupGroups;
216 private List<Group> _groups;
217 private List<Role> _roles;
218 private Map<Long, Boolean> _communityAdmins = new HashMap<Long, Boolean>();
219 private Map<Long, Boolean> _communityOwners = new HashMap<Long, Boolean>();
220
221 }