1   /**
2    * Copyright (c) 2000-2007 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.NoSuchGroupException;
26  import com.liferay.portal.NoSuchResourceException;
27  import com.liferay.portal.PortalException;
28  import com.liferay.portal.SystemException;
29  import com.liferay.portal.kernel.security.permission.ActionKeys;
30  import com.liferay.portal.kernel.security.permission.PermissionChecker;
31  import com.liferay.portal.kernel.security.permission.PermissionCheckerBag;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.model.Group;
34  import com.liferay.portal.model.Resource;
35  import com.liferay.portal.model.User;
36  import com.liferay.portal.model.impl.ResourceImpl;
37  import com.liferay.portal.model.impl.RoleImpl;
38  import com.liferay.portal.service.GroupServiceUtil;
39  import com.liferay.portal.service.PermissionServiceUtil;
40  import com.liferay.portal.service.ResourceServiceUtil;
41  import com.liferay.portal.service.RoleServiceUtil;
42  import com.liferay.portal.util.PortalUtil;
43  
44  import java.rmi.RemoteException;
45  
46  import java.util.HashMap;
47  import java.util.List;
48  import java.util.Map;
49  
50  /**
51   * <a href="PermissionCheckerBagImpl.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   *
55   */
56  public class PermissionCheckerBagImpl implements PermissionCheckerBag {
57  
58      public PermissionCheckerBagImpl() {
59      }
60  
61      public PermissionCheckerBagImpl(long userId, List userGroups,
62                                      List userOrgs, List userOrgGroups,
63                                      List userUserGroupGroups, List groups,
64                                      List roles) {
65  
66          _userId = userId;
67          _userGroups = userGroups;
68          _userOrgs = userOrgs;
69          _userOrgGroups = userOrgGroups;
70          _userUserGroupGroups = userUserGroupGroups;
71          _groups = groups;
72          _roles = roles;
73      }
74  
75      public List getUserGroups() {
76          return _userGroups;
77      }
78  
79      public List getUserOrgs() {
80          return _userOrgs;
81      }
82  
83      public List getUserOrgGroups() {
84          return _userOrgGroups;
85      }
86  
87      public List getUserUserGroupGroups() {
88          return _userUserGroupGroups;
89      }
90  
91      public List getGroups() {
92          return _groups;
93      }
94  
95      public List getRoles() {
96          return _roles;
97      }
98  
99      public boolean isCompanyAdmin(long companyId) throws Exception {
100         String key = String.valueOf(companyId);
101 
102         Boolean value = (Boolean)_isCompanyAdmin.get(key);
103 
104         if (value == null) {
105             boolean hasAdminRole = RoleServiceUtil.hasUserRole(
106                 _userId, companyId, RoleImpl.ADMINISTRATOR, true);
107 
108             value = Boolean.valueOf(hasAdminRole);
109 
110             _isCompanyAdmin.put(key, value);
111         }
112 
113         return value.booleanValue();
114     }
115 
116     public boolean isCommunityAdmin(
117             PermissionChecker permissionChecker, long companyId, long groupId,
118             String name)
119         throws Exception {
120 
121         String key =
122             companyId + StringPool.PIPE + groupId + StringPool.PIPE + name;
123 
124         Boolean value = (Boolean)_isCommunityAdmin.get(key);
125 
126         if (value == null) {
127             value = Boolean.valueOf(
128                 isCommunityAdminImpl(
129                     permissionChecker, companyId, groupId, name));
130 
131             _isCommunityAdmin.put(key, value);
132         }
133 
134         return value.booleanValue();
135     }
136 
137     protected boolean isCommunityAdminImpl(
138             PermissionChecker permissionChecker, long companyId, long groupId,
139             String name)
140         throws PortalException, RemoteException, SystemException {
141 
142         if (groupId <= 0) {
143             return false;
144         }
145 
146         try {
147             Resource resource = ResourceServiceUtil.getResource(
148                 companyId, Group.class.getName(), ResourceImpl.SCOPE_INDIVIDUAL,
149                 String.valueOf(groupId));
150 
151             if (PermissionServiceUtil.hasUserPermission(
152                     _userId, ActionKeys.ADMINISTRATE,
153                     resource.getResourceId())) {
154 
155                 return true;
156             }
157 
158             if (permissionChecker.hasUserPermission(
159                     groupId, Group.class.getName(), String.valueOf(groupId),
160                     ActionKeys.ADMINISTRATE, false)) {
161 
162                 return true;
163             }
164         }
165         catch (NoSuchResourceException nsre) {
166         }
167         catch (PortalException pe) {
168             throw pe;
169         }
170         catch (RemoteException re) {
171             throw re;
172         }
173         catch (SystemException se) {
174             throw se;
175         }
176         catch (Exception e) {
177             throw new SystemException(e);
178         }
179 
180         try {
181             Group group = GroupServiceUtil.getGroup(groupId);
182 
183             long userClassNameId = PortalUtil.getClassNameId(User.class);
184 
185             if ((group.getClassNameId() == userClassNameId) &&
186                 (group.getClassPK() == _userId)) {
187 
188                 return true;
189             }
190         }
191         catch (NoSuchGroupException nsge) {
192         }
193 
194         return false;
195     }
196 
197     private long _userId;
198     private List _userGroups;
199     private List _userOrgs;
200     private List _userOrgGroups;
201     private List _userUserGroupGroups;
202     private List _groups;
203     private List _roles;
204     private Map _isCompanyAdmin = new HashMap();
205     private Map _isCommunityAdmin = new HashMap();
206 
207 }