001    /**
002     * Copyright (c) 2000-present 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.model.Group;
018    import com.liferay.portal.model.Organization;
019    import com.liferay.portal.model.UserConstants;
020    
021    import java.util.ArrayList;
022    import java.util.Collection;
023    import java.util.Collections;
024    import java.util.List;
025    
026    /**
027     * @author L??szl?? Csontos
028     */
029    public class UserPermissionCheckerBagImpl implements UserPermissionCheckerBag {
030    
031            public UserPermissionCheckerBagImpl() {
032                    this(UserConstants.USER_ID_DEFAULT);
033            }
034    
035            public UserPermissionCheckerBagImpl(long userId) {
036                    _userGroups = Collections.<Group>emptyList();
037                    _userId = userId;
038                    _userOrgs = Collections.<Organization>emptyList();
039                    _userOrgGroups = Collections.<Group>emptyList();
040                    _userUserGroupGroups = Collections.<Group>emptyList();
041            }
042    
043            public UserPermissionCheckerBagImpl(
044                    long userId, List<Group> userGroups, List<Organization> userOrgs,
045                    List<Group> userOrgGroups, List<Group> userUserGroupGroups) {
046    
047                    _userGroups = userGroups;
048                    _userId = userId;
049                    _userOrgs = userOrgs;
050                    _userOrgGroups = userOrgGroups;
051                    _userUserGroupGroups = userUserGroupGroups;
052            }
053    
054            public UserPermissionCheckerBagImpl(
055                    UserPermissionCheckerBag userPermissionCheckerBag) {
056    
057                    this(
058                            userPermissionCheckerBag.getUserId(),
059                            userPermissionCheckerBag.getUserGroups(),
060                            userPermissionCheckerBag.getUserOrgs(),
061                            userPermissionCheckerBag.getUserOrgGroups(),
062                            userPermissionCheckerBag.getUserUserGroupGroups());
063            }
064    
065            @Override
066            public List<Group> getGroups() {
067                    if (_groups == null) {
068                            Collection<Group>[] groupsArray = new Collection[3];
069    
070                            int groupsSize = 0;
071    
072                            if (!_userGroups.isEmpty()) {
073                                    groupsArray[0] = _userGroups;
074                                    groupsSize += _userGroups.size();
075                            }
076    
077                            if (!_userOrgGroups.isEmpty()) {
078                                    groupsArray[1] = _userOrgGroups;
079                                    groupsSize += _userOrgGroups.size();
080                            }
081    
082                            if (!_userUserGroupGroups.isEmpty()) {
083                                    groupsArray[2] = _userUserGroupGroups;
084                                    groupsSize += _userUserGroupGroups.size();
085                            }
086    
087                            _groups = new ArrayList<Group>(groupsSize);
088    
089                            for (Collection<Group> groupsItem : groupsArray) {
090                                    if (groupsItem != null) {
091                                            _groups.addAll(groupsItem);
092                                    }
093                            }
094                    }
095    
096                    return _groups;
097            }
098    
099            @Override
100            public List<Group> getUserGroups() {
101                    return _userGroups;
102            }
103    
104            @Override
105            public long getUserId() {
106                    return _userId;
107            }
108    
109            @Override
110            public List<Group> getUserOrgGroups() {
111                    return _userOrgGroups;
112            }
113    
114            @Override
115            public List<Organization> getUserOrgs() {
116                    return _userOrgs;
117            }
118    
119            @Override
120            public List<Group> getUserUserGroupGroups() {
121                    return _userUserGroupGroups;
122            }
123    
124            private List<Group> _groups;
125            private final List<Group> _userGroups;
126            private final long _userId;
127            private final List<Group> _userOrgGroups;
128            private final List<Organization> _userOrgs;
129            private final List<Group> _userUserGroupGroups;
130    
131    }