1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class PermissionCheckerBagImpl implements PermissionCheckerBag {
45  
46      public PermissionCheckerBagImpl() {
47      }
48  
49      public PermissionCheckerBagImpl(
50          long userId, List<Group> userGroups, List<Organization> userOrgs,
51          List<Group> userOrgGroups, List<Group> userUserGroupGroups,
52          List<Group> groups, List<Role> roles) {
53  
54          _userId = userId;
55          _userGroups = userGroups;
56          _userOrgs = userOrgs;
57          _userOrgGroups = userOrgGroups;
58          _userUserGroupGroups = userUserGroupGroups;
59          _groups = groups;
60          _roles = roles;
61      }
62  
63      public List<Group> getUserGroups() {
64          return _userGroups;
65      }
66  
67      public List<Organization> getUserOrgs() {
68          return _userOrgs;
69      }
70  
71      public List<Group> getUserOrgGroups() {
72          return _userOrgGroups;
73      }
74  
75      public List<Group> getUserUserGroupGroups() {
76          return _userUserGroupGroups;
77      }
78  
79      public List<Group> getGroups() {
80          return _groups;
81      }
82  
83      public List<Role> getRoles() {
84          return _roles;
85      }
86  
87      public boolean isCommunityAdmin(
88              PermissionChecker permissionChecker, Group group)
89          throws Exception {
90  
91          Boolean value = _communityAdmins.get(group.getGroupId());
92  
93          if (value == null) {
94              value = Boolean.valueOf(
95                  isCommunityAdminImpl(permissionChecker, group));
96  
97              _communityAdmins.put(group.getGroupId(), value);
98          }
99  
100         return value.booleanValue();
101     }
102 
103     public boolean isCommunityOwner(
104             PermissionChecker permissionChecker, Group group)
105         throws Exception {
106 
107         Boolean value = _communityOwners.get(group.getGroupId());
108 
109         if (value == null) {
110             value = Boolean.valueOf(
111                 isCommunityOwnerImpl(permissionChecker, group));
112 
113             _communityOwners.put(group.getGroupId(), value);
114         }
115 
116         return value.booleanValue();
117     }
118 
119     protected boolean isCommunityAdminImpl(
120             PermissionChecker permissionChecker, Group group)
121         throws PortalException, SystemException {
122 
123         if (group.isCommunity()) {
124             if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
125                     _userId, group.getGroupId(),
126                     RoleConstants.COMMUNITY_ADMINISTRATOR) ||
127                 UserGroupRoleLocalServiceUtil.hasUserGroupRole(
128                     _userId, group.getGroupId(),
129                     RoleConstants.COMMUNITY_OWNER)) {
130 
131                 return true;
132             }
133         }
134         else if (group.isOrganization()) {
135             long organizationId = group.getClassPK();
136 
137             while (organizationId !=
138                         OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
139 
140                 Organization organization =
141                     OrganizationLocalServiceUtil.getOrganization(
142                         organizationId);
143 
144                 Group organizationGroup = organization.getGroup();
145 
146                 long organizationGroupId = organizationGroup.getGroupId();
147 
148                 if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
149                         _userId, organizationGroupId,
150                         RoleConstants.ORGANIZATION_ADMINISTRATOR) ||
151                     UserGroupRoleLocalServiceUtil.hasUserGroupRole(
152                         _userId, organizationGroupId,
153                         RoleConstants.ORGANIZATION_OWNER)) {
154 
155                     return true;
156                 }
157 
158                 organizationId = organization.getParentOrganizationId();
159             }
160         }
161         else if (group.isUser()) {
162             long userId = group.getClassPK();
163 
164             if (userId == _userId) {
165                 return true;
166             }
167         }
168 
169         return false;
170     }
171 
172     protected boolean isCommunityOwnerImpl(
173             PermissionChecker permissionChecker, Group group)
174         throws PortalException, SystemException {
175 
176         if (group.isCommunity()) {
177             if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
178                     _userId, group.getGroupId(),
179                     RoleConstants.COMMUNITY_OWNER)) {
180 
181                 return true;
182             }
183         }
184         else if (group.isOrganization()) {
185             long organizationId = group.getClassPK();
186 
187             while (organizationId !=
188                         OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
189 
190                 Organization organization =
191                     OrganizationLocalServiceUtil.getOrganization(
192                         organizationId);
193 
194                 Group organizationGroup = organization.getGroup();
195 
196                 long organizationGroupId = organizationGroup.getGroupId();
197 
198                 if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
199                         _userId, organizationGroupId,
200                         RoleConstants.ORGANIZATION_OWNER)) {
201 
202                     return true;
203                 }
204 
205                 organizationId = organization.getParentOrganizationId();
206             }
207         }
208         else if (group.isUser()) {
209             long userId = group.getClassPK();
210 
211             if (userId == _userId) {
212                 return true;
213             }
214         }
215 
216         return false;
217     }
218 
219     private long _userId;
220     private List<Group> _userGroups;
221     private List<Organization> _userOrgs;
222     private List<Group> _userOrgGroups;
223     private List<Group> _userUserGroupGroups;
224     private List<Group> _groups;
225     private List<Role> _roles;
226     private Map<Long, Boolean> _communityAdmins = new HashMap<Long, Boolean>();
227     private Map<Long, Boolean> _communityOwners = new HashMap<Long, Boolean>();
228 
229 }