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