1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.security.permission;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.model.Group;
25  import com.liferay.portal.model.Organization;
26  import com.liferay.portal.model.OrganizationConstants;
27  import com.liferay.portal.model.Role;
28  import com.liferay.portal.model.RoleConstants;
29  import com.liferay.portal.service.OrganizationLocalServiceUtil;
30  import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
31  
32  import java.util.HashMap;
33  import java.util.List;
34  import java.util.Map;
35  
36  /**
37   * <a href="PermissionCheckerBagImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class PermissionCheckerBagImpl implements PermissionCheckerBag {
43  
44      public PermissionCheckerBagImpl() {
45      }
46  
47      public PermissionCheckerBagImpl(
48          long userId, List<Group> userGroups, List<Organization> userOrgs,
49          List<Group> userOrgGroups, List<Group> userUserGroupGroups,
50          List<Group> groups, List<Role> roles) {
51  
52          _userId = userId;
53          _userGroups = userGroups;
54          _userOrgs = userOrgs;
55          _userOrgGroups = userOrgGroups;
56          _userUserGroupGroups = userUserGroupGroups;
57          _groups = groups;
58          _roles = roles;
59      }
60  
61      public List<Group> getUserGroups() {
62          return _userGroups;
63      }
64  
65      public List<Organization> getUserOrgs() {
66          return _userOrgs;
67      }
68  
69      public List<Group> getUserOrgGroups() {
70          return _userOrgGroups;
71      }
72  
73      public List<Group> getUserUserGroupGroups() {
74          return _userUserGroupGroups;
75      }
76  
77      public List<Group> getGroups() {
78          return _groups;
79      }
80  
81      public List<Role> getRoles() {
82          return _roles;
83      }
84  
85      public boolean isCommunityAdmin(
86              PermissionChecker permissionChecker, Group group)
87          throws Exception {
88  
89          Boolean value = _communityAdmins.get(group.getGroupId());
90  
91          if (value == null) {
92              value = Boolean.valueOf(
93                  isCommunityAdminImpl(permissionChecker, group));
94  
95              _communityAdmins.put(group.getGroupId(), value);
96          }
97  
98          return value.booleanValue();
99      }
100 
101     public boolean isCommunityOwner(
102             PermissionChecker permissionChecker, Group group)
103         throws Exception {
104 
105         Boolean value = _communityOwners.get(group.getGroupId());
106 
107         if (value == null) {
108             value = Boolean.valueOf(
109                 isCommunityOwnerImpl(permissionChecker, group));
110 
111             _communityOwners.put(group.getGroupId(), value);
112         }
113 
114         return value.booleanValue();
115     }
116 
117     protected boolean isCommunityAdminImpl(
118             PermissionChecker permissionChecker, Group group)
119         throws PortalException, SystemException {
120 
121         if (group.isCommunity()) {
122             if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
123                     _userId, group.getGroupId(),
124                     RoleConstants.COMMUNITY_ADMINISTRATOR) ||
125                 UserGroupRoleLocalServiceUtil.hasUserGroupRole(
126                     _userId, group.getGroupId(),
127                     RoleConstants.COMMUNITY_OWNER)) {
128 
129                 return true;
130             }
131         }
132         else if (group.isOrganization()) {
133             long organizationId = group.getClassPK();
134 
135             while (organizationId !=
136                         OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
137 
138                 Organization organization =
139                     OrganizationLocalServiceUtil.getOrganization(
140                         organizationId);
141 
142                 Group organizationGroup = organization.getGroup();
143 
144                 long organizationGroupId = organizationGroup.getGroupId();
145 
146                 if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
147                         _userId, organizationGroupId,
148                         RoleConstants.ORGANIZATION_ADMINISTRATOR) ||
149                     UserGroupRoleLocalServiceUtil.hasUserGroupRole(
150                         _userId, organizationGroupId,
151                         RoleConstants.ORGANIZATION_OWNER)) {
152 
153                     return true;
154                 }
155 
156                 organizationId = organization.getParentOrganizationId();
157             }
158         }
159         else if (group.isUser()) {
160             long userId = group.getClassPK();
161 
162             if (userId == _userId) {
163                 return true;
164             }
165         }
166 
167         return false;
168     }
169 
170     protected boolean isCommunityOwnerImpl(
171             PermissionChecker permissionChecker, Group group)
172         throws PortalException, SystemException {
173 
174         if (group.isCommunity()) {
175             if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
176                     _userId, group.getGroupId(),
177                     RoleConstants.COMMUNITY_OWNER)) {
178 
179                 return true;
180             }
181         }
182         else if (group.isOrganization()) {
183             long organizationId = group.getClassPK();
184 
185             while (organizationId !=
186                         OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
187 
188                 Organization organization =
189                     OrganizationLocalServiceUtil.getOrganization(
190                         organizationId);
191 
192                 Group organizationGroup = organization.getGroup();
193 
194                 long organizationGroupId = organizationGroup.getGroupId();
195 
196                 if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
197                         _userId, organizationGroupId,
198                         RoleConstants.ORGANIZATION_OWNER)) {
199 
200                     return true;
201                 }
202 
203                 organizationId = organization.getParentOrganizationId();
204             }
205         }
206         else if (group.isUser()) {
207             long userId = group.getClassPK();
208 
209             if (userId == _userId) {
210                 return true;
211             }
212         }
213 
214         return false;
215     }
216 
217     private long _userId;
218     private List<Group> _userGroups;
219     private List<Organization> _userOrgs;
220     private List<Group> _userOrgGroups;
221     private List<Group> _userUserGroupGroups;
222     private List<Group> _groups;
223     private List<Role> _roles;
224     private Map<Long, Boolean> _communityAdmins = new HashMap<Long, Boolean>();
225     private Map<Long, Boolean> _communityOwners = new HashMap<Long, Boolean>();
226 
227 }