001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.security.permission;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.model.Group;
020    import com.liferay.portal.model.Organization;
021    import com.liferay.portal.model.OrganizationConstants;
022    import com.liferay.portal.model.Role;
023    import com.liferay.portal.model.RoleConstants;
024    import com.liferay.portal.service.OrganizationLocalServiceUtil;
025    import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
026    import com.liferay.portal.service.permission.LayoutPrototypePermissionUtil;
027    import com.liferay.portal.service.permission.LayoutSetPrototypePermissionUtil;
028    
029    import java.util.Arrays;
030    import java.util.HashMap;
031    import java.util.List;
032    import java.util.Map;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class PermissionCheckerBagImpl implements PermissionCheckerBag {
038    
039            public PermissionCheckerBagImpl() {
040            }
041    
042            public PermissionCheckerBagImpl(
043                    long userId, List<Group> userGroups, List<Organization> userOrgs,
044                    List<Group> userOrgGroups, List<Group> userUserGroupGroups,
045                    List<Group> groups, List<Role> roles) {
046    
047                    _userId = userId;
048                    _userGroups = userGroups;
049                    _userOrgs = userOrgs;
050                    _userOrgGroups = userOrgGroups;
051                    _userUserGroupGroups = userUserGroupGroups;
052                    _groups = groups;
053                    _roles = roles;
054            }
055    
056            public List<Group> getGroups() {
057                    return _groups;
058            }
059    
060            public long[] getRoleIds() {
061                    if (_roleIds == null) {
062                            List<Role> roles = getRoles();
063    
064                            long[] roleIds = new long[roles.size()];
065    
066                            for (int i = 0; i < roles.size(); i++) {
067                                    Role role = roles.get(i);
068    
069                                    roleIds[i] = role.getRoleId();
070                            }
071    
072                            Arrays.sort(roleIds);
073    
074                            _roleIds = roleIds;
075                    }
076    
077                    return _roleIds;
078            }
079    
080            public List<Role> getRoles() {
081                    return _roles;
082            }
083    
084            public List<Group> getUserGroups() {
085                    return _userGroups;
086            }
087    
088            public List<Group> getUserOrgGroups() {
089                    return _userOrgGroups;
090            }
091    
092            public List<Organization> getUserOrgs() {
093                    return _userOrgs;
094            }
095    
096            public List<Group> getUserUserGroupGroups() {
097                    return _userUserGroupGroups;
098            }
099    
100            /**
101             * @deprecated As of 6.1, renamed to {@link #isGroupAdmin(PermissionChecker,
102             *             Group)}
103             */
104            public boolean isCommunityAdmin(
105                            PermissionChecker permissionChecker, Group group)
106                    throws Exception {
107    
108                    return isGroupAdmin(permissionChecker, group);
109            }
110    
111            /**
112             * @deprecated As of 6.1, renamed to {@link #isGroupOwner(PermissionChecker,
113             *             Group)}
114             */
115            public boolean isCommunityOwner(
116                            PermissionChecker permissionChecker, Group group)
117                    throws Exception {
118    
119                    return isGroupOwner(permissionChecker, group);
120            }
121    
122            public boolean isGroupAdmin(
123                            PermissionChecker permissionChecker, Group group)
124                    throws Exception {
125    
126                    Boolean value = _groupAdmins.get(group.getGroupId());
127    
128                    if (value == null) {
129                            value = Boolean.valueOf(isGroupAdminImpl(permissionChecker, group));
130    
131                            _groupAdmins.put(group.getGroupId(), value);
132                    }
133    
134                    return value.booleanValue();
135            }
136    
137            public boolean isGroupMember(
138                            PermissionChecker permissionChecker, Group group)
139                    throws Exception {
140    
141                    for (Role role : _roles) {
142                            String name = role.getName();
143    
144                            if (name.equals(RoleConstants.SITE_MEMBER)) {
145                                    return true;
146                            }
147                    }
148    
149                    return false;
150            }
151    
152            public boolean isGroupOwner(
153                            PermissionChecker permissionChecker, Group group)
154                    throws Exception {
155    
156                    Boolean value = _groupOwners.get(group.getGroupId());
157    
158                    if (value == null) {
159                            value = Boolean.valueOf(isGroupOwnerImpl(permissionChecker, group));
160    
161                            _groupOwners.put(group.getGroupId(), value);
162                    }
163    
164                    return value.booleanValue();
165            }
166    
167            protected boolean isGroupAdminImpl(
168                            PermissionChecker permissionChecker, Group group)
169                    throws PortalException, SystemException {
170    
171                    if (group.isSite()) {
172                            if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
173                                            _userId, group.getGroupId(),
174                                            RoleConstants.SITE_ADMINISTRATOR, true) ||
175                                    UserGroupRoleLocalServiceUtil.hasUserGroupRole(
176                                            _userId, group.getGroupId(), RoleConstants.SITE_OWNER,
177                                            true)) {
178    
179                                    return true;
180                            }
181                    }
182    
183                    if (group.isCompany()) {
184                            if (permissionChecker.isCompanyAdmin()) {
185                                    return true;
186                            }
187                            else {
188                                    return false;
189                            }
190                    }
191                    else if (group.isLayoutPrototype()) {
192                            if (LayoutPrototypePermissionUtil.contains(
193                                            permissionChecker, group.getClassPK(), ActionKeys.UPDATE)) {
194    
195                                    return true;
196                            }
197                            else {
198                                    return false;
199                            }
200                    }
201                    else if (group.isLayoutSetPrototype()) {
202                            if (LayoutSetPrototypePermissionUtil.contains(
203                                            permissionChecker, group.getClassPK(), ActionKeys.UPDATE)) {
204    
205                                    return true;
206                            }
207                            else {
208                                    return false;
209                            }
210                    }
211                    else if (group.isOrganization()) {
212                            long organizationId = group.getOrganizationId();
213    
214                            while (organizationId !=
215                                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
216    
217                                    Organization organization =
218                                            OrganizationLocalServiceUtil.getOrganization(
219                                                    organizationId);
220    
221                                    Group organizationGroup = organization.getGroup();
222    
223                                    long organizationGroupId = organizationGroup.getGroupId();
224    
225                                    if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
226                                                    _userId, organizationGroupId,
227                                                    RoleConstants.ORGANIZATION_ADMINISTRATOR, true) ||
228                                            UserGroupRoleLocalServiceUtil.hasUserGroupRole(
229                                                    _userId, organizationGroupId,
230                                                    RoleConstants.ORGANIZATION_OWNER, true)) {
231    
232                                            return true;
233                                    }
234    
235                                    organizationId = organization.getParentOrganizationId();
236                            }
237                    }
238    
239                    return false;
240            }
241    
242            protected boolean isGroupOwnerImpl(
243                            PermissionChecker permissionChecker, Group group)
244                    throws PortalException, SystemException {
245    
246                    if (group.isSite()) {
247                            if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
248                                            _userId, group.getGroupId(), RoleConstants.SITE_OWNER,
249                                            true)) {
250    
251                                    return true;
252                            }
253                    }
254    
255                    if (group.isLayoutPrototype()) {
256                            if (LayoutPrototypePermissionUtil.contains(
257                                            permissionChecker, group.getClassPK(), ActionKeys.UPDATE)) {
258    
259                                    return true;
260                            }
261                            else {
262                                    return false;
263                            }
264                    }
265                    else if (group.isLayoutSetPrototype()) {
266                            if (LayoutSetPrototypePermissionUtil.contains(
267                                            permissionChecker, group.getClassPK(), ActionKeys.UPDATE)) {
268    
269                                    return true;
270                            }
271                            else {
272                                    return false;
273                            }
274                    }
275                    else if (group.isOrganization()) {
276                            long organizationId = group.getOrganizationId();
277    
278                            while (organizationId !=
279                                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
280    
281                                    Organization organization =
282                                            OrganizationLocalServiceUtil.getOrganization(
283                                                    organizationId);
284    
285                                    Group organizationGroup = organization.getGroup();
286    
287                                    long organizationGroupId = organizationGroup.getGroupId();
288    
289                                    if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
290                                                    _userId, organizationGroupId,
291                                                    RoleConstants.ORGANIZATION_OWNER, true)) {
292    
293                                            return true;
294                                    }
295    
296                                    organizationId = organization.getParentOrganizationId();
297                            }
298                    }
299                    else if (group.isUser()) {
300                            long userId = group.getClassPK();
301    
302                            if (userId == _userId) {
303                                    return true;
304                            }
305                    }
306    
307                    return false;
308            }
309    
310            private Map<Long, Boolean> _groupAdmins = new HashMap<Long, Boolean>();
311            private Map<Long, Boolean> _groupOwners = new HashMap<Long, Boolean>();
312            private List<Group> _groups;
313            private long[] _roleIds;
314            private List<Role> _roles;
315            private List<Group> _userGroups;
316            private long _userId;
317            private List<Group> _userOrgGroups;
318            private List<Organization> _userOrgs;
319            private List<Group> _userUserGroupGroups;
320    
321    }