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.util.SetUtil;
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.model.UserGroup;
023    import com.liferay.portal.service.GroupLocalServiceUtil;
024    import com.liferay.portal.service.OrganizationLocalServiceUtil;
025    import com.liferay.portal.service.RoleLocalServiceUtil;
026    import com.liferay.portal.service.UserGroupLocalServiceUtil;
027    import com.liferay.portal.util.PropsValues;
028    
029    import java.util.HashSet;
030    import java.util.LinkedHashSet;
031    import java.util.List;
032    import java.util.Set;
033    
034    /**
035     * @author Preston Crary
036     */
037    public class UserBagFactoryImpl implements UserBagFactory {
038    
039            @Override
040            public UserBag create(long userId) throws PortalException {
041                    UserBag userBag = PermissionCacheUtil.getUserBag(userId);
042    
043                    if (userBag != null) {
044                            return userBag;
045                    }
046    
047                    try {
048                            List<Group> userGroups = GroupLocalServiceUtil.getUserGroups(
049                                    userId, true);
050    
051                            Set<Organization> userOrgs = getUserOrgs(userId);
052    
053                            Set<Group> userOrgGroups = new HashSet<>(userOrgs.size());
054    
055                            for (Organization organization : userOrgs) {
056                                    userOrgGroups.add(organization.getGroup());
057                            }
058    
059                            List<UserGroup> userUserGroups =
060                                    UserGroupLocalServiceUtil.getUserUserGroups(userId);
061    
062                            List<Group> userUserGroupGroups =
063                                    GroupLocalServiceUtil.getUserGroupsGroups(userUserGroups);
064    
065                            Set<Role> userRoles = new HashSet<>();
066    
067                            if (!userGroups.isEmpty()) {
068                                    List<Role> userRelatedRoles =
069                                            RoleLocalServiceUtil.getUserRelatedRoles(
070                                                    userId, userGroups);
071    
072                                    userRoles.addAll(userRelatedRoles);
073                            }
074                            else {
075                                    userRoles.addAll(RoleLocalServiceUtil.getUserRoles(userId));
076                            }
077    
078                            userBag = new UserBagImpl(
079                                    userId, SetUtil.fromList(userGroups), userOrgs, userOrgGroups,
080                                    SetUtil.fromList(userUserGroupGroups), userRoles);
081    
082                            PermissionCacheUtil.putUserBag(userId, userBag);
083    
084                            return userBag;
085                    }
086                    catch (Exception e) {
087                            PermissionCacheUtil.removeUserBag(userId);
088    
089                            throw e;
090                    }
091            }
092    
093            protected Set<Organization> getUserOrgs(long userId)
094                    throws PortalException {
095    
096                    List<Organization> userOrgs =
097                            OrganizationLocalServiceUtil.getUserOrganizations(userId);
098    
099                    if (userOrgs.isEmpty()) {
100                            return new HashSet<>(userOrgs);
101                    }
102    
103                    Set<Organization> organizations = new LinkedHashSet<>();
104    
105                    for (Organization organization : userOrgs) {
106                            if (organizations.add(organization) &&
107                                    !PropsValues.ORGANIZATIONS_MEMBERSHIP_STRICT) {
108    
109                                    List<Organization> ancestorOrganizations =
110                                            OrganizationLocalServiceUtil.getParentOrganizations(
111                                                    organization.getOrganizationId());
112    
113                                    organizations.addAll(ancestorOrganizations);
114                            }
115                    }
116    
117                    return organizations;
118            }
119    
120    }