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