001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.security.permission;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.security.permission.PermissionChecker;
026: import com.liferay.portal.kernel.security.permission.PermissionCheckerBag;
027: import com.liferay.portal.model.Group;
028: import com.liferay.portal.model.Organization;
029: import com.liferay.portal.model.impl.OrganizationImpl;
030: import com.liferay.portal.model.impl.RoleImpl;
031: import com.liferay.portal.service.OrganizationLocalServiceUtil;
032: import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
033:
034: import java.util.HashMap;
035: import java.util.List;
036: import java.util.Map;
037:
038: /**
039: * <a href="PermissionCheckerBagImpl.java.html"><b><i>View Source</i></b></a>
040: *
041: * @author Brian Wing Shun Chan
042: *
043: */
044: public class PermissionCheckerBagImpl implements PermissionCheckerBag {
045:
046: public PermissionCheckerBagImpl() {
047: }
048:
049: public PermissionCheckerBagImpl(long userId, List userGroups,
050: List userOrgs, List userOrgGroups,
051: List userUserGroupGroups, List groups, List roles) {
052:
053: _userId = userId;
054: _userGroups = userGroups;
055: _userOrgs = userOrgs;
056: _userOrgGroups = userOrgGroups;
057: _userUserGroupGroups = userUserGroupGroups;
058: _groups = groups;
059: _roles = roles;
060: }
061:
062: public List getUserGroups() {
063: return _userGroups;
064: }
065:
066: public List getUserOrgs() {
067: return _userOrgs;
068: }
069:
070: public List getUserOrgGroups() {
071: return _userOrgGroups;
072: }
073:
074: public List getUserUserGroupGroups() {
075: return _userUserGroupGroups;
076: }
077:
078: public List getGroups() {
079: return _groups;
080: }
081:
082: public List getRoles() {
083: return _roles;
084: }
085:
086: public boolean isCommunityAdmin(
087: PermissionChecker permissionChecker, Object groupObj)
088: throws Exception {
089:
090: Group group = (Group) groupObj;
091:
092: String key = String.valueOf(group.getGroupId());
093:
094: Boolean value = (Boolean) _communityAdmins.get(key);
095:
096: if (value == null) {
097: value = Boolean.valueOf(isCommunityAdminImpl(
098: permissionChecker, group));
099:
100: _communityAdmins.put(key, value);
101: }
102:
103: return value.booleanValue();
104: }
105:
106: public boolean isCommunityOwner(
107: PermissionChecker permissionChecker, Object groupObj)
108: throws Exception {
109:
110: Group group = (Group) groupObj;
111:
112: String key = String.valueOf(group.getGroupId());
113:
114: Boolean value = (Boolean) _communityOwners.get(key);
115:
116: if (value == null) {
117: value = Boolean.valueOf(isCommunityOwnerImpl(
118: permissionChecker, group));
119:
120: _communityOwners.put(key, value);
121: }
122:
123: return value.booleanValue();
124: }
125:
126: protected boolean isCommunityAdminImpl(
127: PermissionChecker permissionChecker, Group group)
128: throws PortalException, SystemException {
129:
130: if (group.isCommunity()) {
131: if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(_userId,
132: group.getGroupId(),
133: RoleImpl.COMMUNITY_ADMINISTRATOR)
134: || UserGroupRoleLocalServiceUtil.hasUserGroupRole(
135: _userId, group.getGroupId(),
136: RoleImpl.COMMUNITY_OWNER)) {
137:
138: return true;
139: }
140: } else if (group.isOrganization()) {
141: long organizationId = group.getClassPK();
142:
143: while (organizationId != OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID) {
144:
145: Organization organization = OrganizationLocalServiceUtil
146: .getOrganization(organizationId);
147:
148: Group organizationGroup = organization.getGroup();
149:
150: long organizationGroupId = organizationGroup
151: .getGroupId();
152:
153: if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
154: _userId, organizationGroupId,
155: RoleImpl.ORGANIZATION_ADMINISTRATOR)
156: || UserGroupRoleLocalServiceUtil
157: .hasUserGroupRole(_userId,
158: organizationGroupId,
159: RoleImpl.ORGANIZATION_OWNER)) {
160:
161: return true;
162: }
163:
164: organizationId = organization.getParentOrganizationId();
165: }
166: } else if (group.isUser()) {
167: long userId = group.getClassPK();
168:
169: if (userId == _userId) {
170: return true;
171: }
172: }
173:
174: return false;
175: }
176:
177: protected boolean isCommunityOwnerImpl(
178: PermissionChecker permissionChecker, Group group)
179: throws PortalException, SystemException {
180:
181: if (group.isCommunity()) {
182: if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(_userId,
183: group.getGroupId(), RoleImpl.COMMUNITY_OWNER)) {
184:
185: return true;
186: }
187: } else if (group.isOrganization()) {
188: long organizationId = group.getClassPK();
189:
190: while (organizationId != OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID) {
191:
192: Organization organization = OrganizationLocalServiceUtil
193: .getOrganization(organizationId);
194:
195: Group organizationGroup = organization.getGroup();
196:
197: long organizationGroupId = organizationGroup
198: .getGroupId();
199:
200: if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
201: _userId, organizationGroupId,
202: RoleImpl.ORGANIZATION_OWNER)) {
203:
204: return true;
205: }
206:
207: organizationId = organization.getParentOrganizationId();
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 _userGroups;
222: private List _userOrgs;
223: private List _userOrgGroups;
224: private List _userUserGroupGroups;
225: private List _groups;
226: private List _roles;
227: private Map _communityAdmins = new HashMap();
228: private Map _communityOwners = new HashMap();
229:
230: }
|