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