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                    if (_userGroups.contains(group)) {
150                            return true;
151                    }
152    
153                    return false;
154            }
155    
156            public boolean isGroupOwner(
157                            PermissionChecker permissionChecker, Group group)
158                    throws Exception {
159    
160                    Boolean value = _groupOwners.get(group.getGroupId());
161    
162                    if (value == null) {
163                            value = Boolean.valueOf(isGroupOwnerImpl(permissionChecker, group));
164    
165                            _groupOwners.put(group.getGroupId(), value);
166                    }
167    
168                    return value.booleanValue();
169            }
170    
171            public boolean isOrganizationAdmin(
172                            PermissionChecker permissionChecker, Organization organization)
173                    throws Exception {
174    
175                    Boolean value = _organizationAdmins.get(
176                            organization.getOrganizationId());
177    
178                    if (value == null) {
179                            value = Boolean.valueOf(
180                                    isOrganizationAdminImpl(permissionChecker, organization));
181    
182                            _organizationAdmins.put(organization.getOrganizationId(), value);
183                    }
184    
185                    return value.booleanValue();
186            }
187    
188            protected boolean isGroupAdminImpl(
189                            PermissionChecker permissionChecker, Group group)
190                    throws PortalException, SystemException {
191    
192                    if (group.isSite()) {
193                            if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
194                                            _userId, group.getGroupId(),
195                                            RoleConstants.SITE_ADMINISTRATOR, true) ||
196                                    UserGroupRoleLocalServiceUtil.hasUserGroupRole(
197                                            _userId, group.getGroupId(), RoleConstants.SITE_OWNER,
198                                            true)) {
199    
200                                    return true;
201                            }
202                    }
203    
204                    if (group.isCompany()) {
205                            if (permissionChecker.isCompanyAdmin()) {
206                                    return true;
207                            }
208                            else {
209                                    return false;
210                            }
211                    }
212                    else if (group.isLayoutPrototype()) {
213                            if (LayoutPrototypePermissionUtil.contains(
214                                            permissionChecker, group.getClassPK(), ActionKeys.UPDATE)) {
215    
216                                    return true;
217                            }
218                            else {
219                                    return false;
220                            }
221                    }
222                    else if (group.isLayoutSetPrototype()) {
223                            if (LayoutSetPrototypePermissionUtil.contains(
224                                            permissionChecker, group.getClassPK(), ActionKeys.UPDATE)) {
225    
226                                    return true;
227                            }
228                            else {
229                                    return false;
230                            }
231                    }
232                    else if (group.isOrganization()) {
233                            long organizationId = group.getOrganizationId();
234    
235                            while (organizationId !=
236                                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
237    
238                                    Organization organization =
239                                            OrganizationLocalServiceUtil.getOrganization(
240                                                    organizationId);
241    
242                                    Group organizationGroup = organization.getGroup();
243    
244                                    long organizationGroupId = organizationGroup.getGroupId();
245    
246                                    if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
247                                                    _userId, organizationGroupId,
248                                                    RoleConstants.ORGANIZATION_ADMINISTRATOR, true) ||
249                                            UserGroupRoleLocalServiceUtil.hasUserGroupRole(
250                                                    _userId, organizationGroupId,
251                                                    RoleConstants.ORGANIZATION_OWNER, true)) {
252    
253                                            return true;
254                                    }
255    
256                                    organizationId = organization.getParentOrganizationId();
257                            }
258                    }
259    
260                    return false;
261            }
262    
263            protected boolean isGroupOwnerImpl(
264                            PermissionChecker permissionChecker, Group group)
265                    throws PortalException, SystemException {
266    
267                    if (group.isSite()) {
268                            if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
269                                            _userId, group.getGroupId(), RoleConstants.SITE_OWNER,
270                                            true)) {
271    
272                                    return true;
273                            }
274                    }
275    
276                    if (group.isLayoutPrototype()) {
277                            if (LayoutPrototypePermissionUtil.contains(
278                                            permissionChecker, group.getClassPK(), ActionKeys.UPDATE)) {
279    
280                                    return true;
281                            }
282                            else {
283                                    return false;
284                            }
285                    }
286                    else if (group.isLayoutSetPrototype()) {
287                            if (LayoutSetPrototypePermissionUtil.contains(
288                                            permissionChecker, group.getClassPK(), ActionKeys.UPDATE)) {
289    
290                                    return true;
291                            }
292                            else {
293                                    return false;
294                            }
295                    }
296                    else if (group.isOrganization()) {
297                            long organizationId = group.getOrganizationId();
298    
299                            while (organizationId !=
300                                                    OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID) {
301    
302                                    Organization organization =
303                                            OrganizationLocalServiceUtil.getOrganization(
304                                                    organizationId);
305    
306                                    Group organizationGroup = organization.getGroup();
307    
308                                    long organizationGroupId = organizationGroup.getGroupId();
309    
310                                    if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
311                                                    _userId, organizationGroupId,
312                                                    RoleConstants.ORGANIZATION_OWNER, true)) {
313    
314                                            return true;
315                                    }
316    
317                                    organizationId = organization.getParentOrganizationId();
318                            }
319                    }
320                    else if (group.isUser()) {
321                            long userId = group.getClassPK();
322    
323                            if (userId == _userId) {
324                                    return true;
325                            }
326                    }
327    
328                    return false;
329            }
330    
331            protected boolean isOrganizationAdminImpl(
332                            PermissionChecker permissionChecker, Organization organization)
333                    throws PortalException, SystemException {
334    
335                    while (organization != null) {
336                            Group organizationGroup = organization.getGroup();
337    
338                            long organizationGroupId = organizationGroup.getGroupId();
339    
340                            if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
341                                            _userId, organizationGroupId,
342                                            RoleConstants.ORGANIZATION_ADMINISTRATOR, true) ||
343                                    UserGroupRoleLocalServiceUtil.hasUserGroupRole(
344                                            _userId, organizationGroupId,
345                                            RoleConstants.ORGANIZATION_OWNER, true)) {
346    
347                                    return true;
348                            }
349    
350                            organization = organization.getParentOrganization();
351                    }
352    
353                    return false;
354            }
355    
356            private Map<Long, Boolean> _groupAdmins = new HashMap<Long, Boolean>();
357            private Map<Long, Boolean> _groupOwners = new HashMap<Long, Boolean>();
358            private List<Group> _groups;
359            private Map<Long, Boolean> _organizationAdmins =
360                    new HashMap<Long, Boolean>();
361            private long[] _roleIds;
362            private List<Role> _roles;
363            private List<Group> _userGroups;
364            private long _userId;
365            private List<Group> _userOrgGroups;
366            private List<Organization> _userOrgs;
367            private List<Group> _userUserGroupGroups;
368    
369    }