1   /**
2    * Copyright (c) 2000-2008 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.kernel.security.permission.PermissionChecker;
28  import com.liferay.portal.kernel.security.permission.PermissionCheckerBag;
29  import com.liferay.portal.model.Group;
30  import com.liferay.portal.model.Organization;
31  import com.liferay.portal.model.impl.OrganizationImpl;
32  import com.liferay.portal.model.impl.RoleImpl;
33  import com.liferay.portal.service.OrganizationLocalServiceUtil;
34  import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
35  
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map;
39  
40  /**
41   * <a href="PermissionCheckerBagImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class PermissionCheckerBagImpl implements PermissionCheckerBag {
47  
48      public PermissionCheckerBagImpl() {
49      }
50  
51      public PermissionCheckerBagImpl(
52          long userId, List userGroups, List userOrgs, List userOrgGroups,
53          List userUserGroupGroups, List groups, List 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 getUserGroups() {
65          return _userGroups;
66      }
67  
68      public List getUserOrgs() {
69          return _userOrgs;
70      }
71  
72      public List getUserOrgGroups() {
73          return _userOrgGroups;
74      }
75  
76      public List getUserUserGroupGroups() {
77          return _userUserGroupGroups;
78      }
79  
80      public List getGroups() {
81          return _groups;
82      }
83  
84      public List getRoles() {
85          return _roles;
86      }
87  
88      public boolean isCommunityAdmin(
89              PermissionChecker permissionChecker, Object groupObj)
90          throws Exception {
91  
92          Group group = (Group)groupObj;
93  
94          String key = String.valueOf(group.getGroupId());
95  
96          Boolean value = (Boolean)_communityAdmins.get(key);
97  
98          if (value == null) {
99              value = Boolean.valueOf(
100                 isCommunityAdminImpl(permissionChecker, group));
101 
102             _communityAdmins.put(key, value);
103         }
104 
105         return value.booleanValue();
106     }
107 
108     public boolean isCommunityOwner(
109             PermissionChecker permissionChecker, Object groupObj)
110         throws Exception {
111 
112         Group group = (Group)groupObj;
113 
114         String key = String.valueOf(group.getGroupId());
115 
116         Boolean value = (Boolean)_communityOwners.get(key);
117 
118         if (value == null) {
119             value = Boolean.valueOf(
120                 isCommunityOwnerImpl(permissionChecker, group));
121 
122             _communityOwners.put(key, value);
123         }
124 
125         return value.booleanValue();
126     }
127 
128     protected boolean isCommunityAdminImpl(
129             PermissionChecker permissionChecker, Group group)
130         throws PortalException, SystemException {
131 
132         if (group.isCommunity()) {
133             if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
134                     _userId, group.getGroupId(),
135                     RoleImpl.COMMUNITY_ADMINISTRATOR) ||
136                 UserGroupRoleLocalServiceUtil.hasUserGroupRole(
137                     _userId, group.getGroupId(), RoleImpl.COMMUNITY_OWNER)) {
138 
139                 return true;
140             }
141         }
142         else if (group.isOrganization()) {
143             long organizationId = group.getClassPK();
144 
145             while (organizationId !=
146                         OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID) {
147 
148                 Organization organization =
149                     OrganizationLocalServiceUtil.getOrganization(
150                         organizationId);
151 
152                 Group organizationGroup = organization.getGroup();
153 
154                 long organizationGroupId = organizationGroup.getGroupId();
155 
156                 if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
157                         _userId, organizationGroupId,
158                         RoleImpl.ORGANIZATION_ADMINISTRATOR) ||
159                     UserGroupRoleLocalServiceUtil.hasUserGroupRole(
160                         _userId, organizationGroupId,
161                         RoleImpl.ORGANIZATION_OWNER)) {
162 
163                     return true;
164                 }
165 
166                 organizationId = organization.getParentOrganizationId();
167             }
168         }
169         else if (group.isUser()) {
170             long userId = group.getClassPK();
171 
172             if (userId == _userId) {
173                 return true;
174             }
175         }
176 
177         return false;
178     }
179 
180     protected boolean isCommunityOwnerImpl(
181             PermissionChecker permissionChecker, Group group)
182         throws PortalException, SystemException {
183 
184         if (group.isCommunity()) {
185             if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
186                     _userId, group.getGroupId(), RoleImpl.COMMUNITY_OWNER)) {
187 
188                 return true;
189             }
190         }
191         else if (group.isOrganization()) {
192             long organizationId = group.getClassPK();
193 
194             while (organizationId !=
195                         OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID) {
196 
197                 Organization organization =
198                     OrganizationLocalServiceUtil.getOrganization(
199                         organizationId);
200 
201                 Group organizationGroup = organization.getGroup();
202 
203                 long organizationGroupId = organizationGroup.getGroupId();
204 
205                 if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
206                         _userId, organizationGroupId,
207                         RoleImpl.ORGANIZATION_OWNER)) {
208 
209                     return true;
210                 }
211 
212                 organizationId = organization.getParentOrganizationId();
213             }
214         }
215         else if (group.isUser()) {
216             long userId = group.getClassPK();
217 
218             if (userId == _userId) {
219                 return true;
220             }
221         }
222 
223         return false;
224     }
225 
226     private long _userId;
227     private List _userGroups;
228     private List _userOrgs;
229     private List _userOrgGroups;
230     private List _userUserGroupGroups;
231     private List _groups;
232     private List _roles;
233     private Map _communityAdmins = new HashMap();
234     private Map _communityOwners = new HashMap();
235 
236 }