1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.plugin;
16  
17  import com.liferay.portal.kernel.exception.SystemException;
18  import com.liferay.portal.model.Plugin;
19  import com.liferay.portal.model.PluginSetting;
20  import com.liferay.portal.model.User;
21  import com.liferay.portal.service.PluginSettingLocalServiceUtil;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  /**
27   * <a href="PluginUtil.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   */
31  public class PluginUtil {
32  
33      public static List<? extends Plugin> restrictPlugins(
34              List<? extends Plugin> plugins, long companyId, long userId)
35          throws SystemException {
36  
37          List<Plugin> visiblePlugins = new ArrayList<Plugin>(plugins.size());
38  
39          for (Plugin plugin : plugins) {
40              PluginSetting pluginSetting =
41                  PluginSettingLocalServiceUtil.getPluginSetting(
42                      companyId, plugin.getPluginId(), plugin.getPluginType());
43  
44              if (pluginSetting.isActive() &&
45                  pluginSetting.hasPermission(userId)) {
46  
47                  visiblePlugins.add(plugin);
48              }
49          }
50  
51          return visiblePlugins;
52      }
53  
54      public static List<? extends Plugin> restrictPlugins(
55              List<? extends Plugin> plugins, User user)
56          throws SystemException {
57  
58          return restrictPlugins(plugins, user.getCompanyId(), user.getUserId());
59      }
60  
61  }