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    
021    import java.util.Collections;
022    import java.util.HashSet;
023    import java.util.Set;
024    
025    /**
026     * @author L??szl?? Csontos
027     */
028    public class UserBagImpl implements UserBag {
029    
030            public UserBagImpl(
031                    long userId, Set<Group> userGroups, Set<Organization> userOrgs,
032                    Set<Group> userOrgGroups, Set<Group> userUserGroupGroups,
033                    Set<Role> userRoles) {
034    
035                    _userId = userId;
036                    _userGroups = Collections.unmodifiableSet(userGroups);
037                    _userOrgs = Collections.unmodifiableSet(userOrgs);
038                    _userOrgGroups = Collections.unmodifiableSet(userOrgGroups);
039                    _userUserGroupGroups = Collections.unmodifiableSet(userUserGroupGroups);
040                    _userRoles = Collections.unmodifiableSet(userRoles);
041            }
042    
043            @Override
044            public Set<Group> getGroups() {
045                    if (_groups == null) {
046                            _groups = new HashSet<>();
047    
048                            _groups.addAll(_userGroups);
049                            _groups.addAll(_userOrgGroups);
050                            _groups.addAll(_userUserGroupGroups);
051    
052                            _groups = Collections.unmodifiableSet(_groups);
053                    }
054    
055                    return _groups;
056            }
057    
058            @Override
059            public Set<Role> getRoles() {
060                    return _userRoles;
061            }
062    
063            @Override
064            public Set<Group> getUserGroups() {
065                    return _userGroups;
066            }
067    
068            @Override
069            public long getUserId() {
070                    return _userId;
071            }
072    
073            @Override
074            public Set<Group> getUserOrgGroups() {
075                    return _userOrgGroups;
076            }
077    
078            @Override
079            public Set<Organization> getUserOrgs() {
080                    return _userOrgs;
081            }
082    
083            @Override
084            public Set<Group> getUserUserGroupGroups() {
085                    return _userUserGroupGroups;
086            }
087    
088            @Override
089            public boolean hasRole(Role role) {
090                    return _userRoles.contains(role);
091            }
092    
093            private Set<Group> _groups;
094            private final Set<Group> _userGroups;
095            private final long _userId;
096            private final Set<Group> _userOrgGroups;
097            private final Set<Organization> _userOrgs;
098            private final Set<Role> _userRoles;
099            private final Set<Group> _userUserGroupGroups;
100    
101    }