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.plugin;
016    
017    import com.liferay.portal.model.Plugin;
018    import com.liferay.portal.model.PluginSetting;
019    import com.liferay.portal.model.User;
020    import com.liferay.portal.service.PluginSettingLocalServiceUtil;
021    
022    import java.util.ArrayList;
023    import java.util.List;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class PluginUtil {
029    
030            public static <P extends Plugin> List<P> restrictPlugins(
031                    List<P> plugins, long companyId, long userId) {
032    
033                    List<P> visiblePlugins = new ArrayList<>(plugins.size());
034    
035                    for (P plugin : plugins) {
036                            PluginSetting pluginSetting =
037                                    PluginSettingLocalServiceUtil.getPluginSetting(
038                                            companyId, plugin.getPluginId(), plugin.getPluginType());
039    
040                            if (pluginSetting.isActive() &&
041                                    pluginSetting.hasPermission(userId)) {
042    
043                                    visiblePlugins.add(plugin);
044                            }
045                    }
046    
047                    return visiblePlugins;
048            }
049    
050            public static <P extends Plugin> List<P> restrictPlugins(
051                    List<P> plugins, User user) {
052    
053                    return restrictPlugins(plugins, user.getCompanyId(), user.getUserId());
054            }
055    
056    }