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