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.kernel.exception.PortalException;
018    import com.liferay.portal.model.BaseModel;
019    import com.liferay.portal.model.Group;
020    import com.liferay.portal.model.Organization;
021    import com.liferay.portal.model.Role;
022    import com.liferay.portal.service.GroupLocalServiceUtil;
023    import com.liferay.portal.service.OrganizationLocalServiceUtil;
024    import com.liferay.portal.service.RoleLocalServiceUtil;
025    
026    import java.util.Arrays;
027    import java.util.Collection;
028    import java.util.HashSet;
029    import java.util.List;
030    import java.util.Set;
031    
032    /**
033     * @author L??szl?? Csontos
034     * @author Preston Crary
035     */
036    public class UserBagImpl implements UserBag {
037    
038            public UserBagImpl(
039                    long userId, Collection<Group> userGroups,
040                    Collection<Organization> userOrgs, Collection<Group> userOrgGroups,
041                    Collection<Role> userRoles) {
042    
043                    _userId = userId;
044                    _userGroupIds = _toSortedLongArray(userGroups);
045                    _userOrgIds = _toSortedLongArray(userOrgs);
046                    _userOrgGroupIds = _toSortedLongArray(userOrgGroups);
047                    _userRoleIds = _toSortedLongArray(userRoles);
048            }
049    
050            @Override
051            public Set<Group> getGroups() throws PortalException {
052                    Set<Group> groups = new HashSet<>(getUserGroups());
053    
054                    groups.addAll(getUserOrgGroups());
055    
056                    return groups;
057            }
058    
059            @Override
060            public long[] getRoleIds() {
061                    return _userRoleIds.clone();
062            }
063    
064            @Override
065            public List<Role> getRoles() throws PortalException {
066                    return RoleLocalServiceUtil.getRoles(_userRoleIds);
067            }
068    
069            @Override
070            public long[] getUserGroupIds() {
071                    return _userGroupIds.clone();
072            }
073    
074            @Override
075            public List<Group> getUserGroups() throws PortalException {
076                    return GroupLocalServiceUtil.getGroups(_userGroupIds);
077            }
078    
079            @Override
080            public long getUserId() {
081                    return _userId;
082            }
083    
084            @Override
085            public long[] getUserOrgGroupIds() {
086                    return _userOrgGroupIds.clone();
087            }
088    
089            @Override
090            public List<Group> getUserOrgGroups() throws PortalException {
091                    return GroupLocalServiceUtil.getGroups(_userOrgGroupIds);
092            }
093    
094            @Override
095            public long[] getUserOrgIds() {
096                    return _userOrgIds.clone();
097            }
098    
099            @Override
100            public List<Organization> getUserOrgs() throws PortalException {
101                    return OrganizationLocalServiceUtil.getOrganizations(_userOrgIds);
102            }
103    
104            @Override
105            public boolean hasRole(Role role) {
106                    return _search(_userRoleIds, role.getRoleId());
107            }
108    
109            @Override
110            public boolean hasUserGroup(Group group) {
111                    return _search(_userGroupIds, group.getGroupId());
112            }
113    
114            @Override
115            public boolean hasUserOrg(Organization organization) {
116                    return _search(_userOrgIds, organization.getOrganizationId());
117            }
118    
119            @Override
120            public boolean hasUserOrgGroup(Group group) {
121                    return _search(_userOrgGroupIds, group.getGroupId());
122            }
123    
124            private boolean _search(long[] ids, long id) {
125                    if (Arrays.binarySearch(ids, id) >= 0) {
126                            return true;
127                    }
128    
129                    return false;
130            }
131    
132            private long[] _toSortedLongArray(
133                    Collection<? extends BaseModel<?>> baseModels) {
134    
135                    if ((baseModels == null) || baseModels.isEmpty()) {
136                            return new long[0];
137                    }
138    
139                    long[] array = new long[baseModels.size()];
140    
141                    int index = 0;
142    
143                    for (BaseModel<?> baseModel : baseModels) {
144                            array[index++] = (long)baseModel.getPrimaryKeyObj();
145                    }
146    
147                    Arrays.sort(array);
148    
149                    return array;
150            }
151    
152            private final long[] _userGroupIds;
153            private final long _userId;
154            private final long[] _userOrgGroupIds;
155            private final long[] _userOrgIds;
156            private final long[] _userRoleIds;
157    
158    }