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