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