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