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