001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 List<? extends Plugin> restrictPlugins(
032                            List<? extends Plugin> plugins, long companyId, long userId)
033                    throws SystemException {
034    
035                    List<Plugin> visiblePlugins = new ArrayList<Plugin>(plugins.size());
036    
037                    for (Plugin 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 List<? extends Plugin> restrictPlugins(
053                            List<? extends Plugin> plugins, User user)
054                    throws SystemException {
055    
056                    return restrictPlugins(plugins, user.getCompanyId(), user.getUserId());
057            }
058    
059    }