1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.service.impl;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.log.Log;
25  import com.liferay.portal.kernel.log.LogFactoryUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.model.Plugin;
28  import com.liferay.portal.model.PluginSetting;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.model.impl.PluginSettingImpl;
31  import com.liferay.portal.security.auth.PrincipalException;
32  import com.liferay.portal.service.base.PluginSettingLocalServiceBaseImpl;
33  import com.liferay.portal.util.PortalUtil;
34  
35  /**
36   * <a href="PluginSettingLocalServiceImpl.java.html"><b><i>View Source</i></b>
37   * </a>
38   *
39   * @author Jorge Ferrer
40   *
41   */
42  public class PluginSettingLocalServiceImpl
43      extends PluginSettingLocalServiceBaseImpl {
44  
45      public void checkPermission(
46              long userId, String pluginId, String pluginType)
47          throws PortalException {
48  
49          if (!hasPermission(userId, pluginId, pluginType)) {
50              throw new PrincipalException();
51          }
52      }
53  
54      public PluginSetting getDefaultPluginSetting() {
55          PluginSettingImpl pluginSetting = new PluginSettingImpl();
56  
57          pluginSetting.setRoles(StringPool.BLANK);
58          pluginSetting.setActive(true);
59  
60          return pluginSetting;
61      }
62  
63      public PluginSetting getPluginSetting(
64              long companyId, String pluginId, String pluginType)
65          throws SystemException {
66  
67          PluginSetting pluginSetting = pluginSettingPersistence.fetchByC_I_T(
68              companyId, pluginId, pluginType);
69  
70          if (pluginSetting == null) {
71              Plugin plugin = null;
72  
73              if (pluginType.equals(Plugin.TYPE_LAYOUT_TEMPLATE)) {
74                  plugin = layoutTemplateLocalService.getLayoutTemplate(
75                      pluginId, false, null);
76              }
77              else if (pluginType.equals(Plugin.TYPE_THEME)) {
78                  boolean wapTheme = true;
79  
80                  plugin = themeLocalService.getTheme(
81                      companyId, pluginId, wapTheme);
82              }
83  
84              if ((plugin == null) ||
85                  (plugin.getDefaultPluginSetting() == null)) {
86  
87                  pluginSetting = getDefaultPluginSetting();
88  
89                  pluginSetting.setCompanyId(companyId);
90              }
91              else {
92                  pluginSetting = plugin.getDefaultPluginSetting(companyId);
93              }
94          }
95  
96          return pluginSetting;
97      }
98  
99      public boolean hasPermission(
100         long userId, String pluginId, String pluginType) {
101 
102         try {
103             User user = userPersistence.findByPrimaryKey(userId);
104 
105             PluginSetting pluginSetting = getPluginSetting(
106                 user.getCompanyId(), pluginId, pluginType);
107 
108             if (!pluginSetting.hasPermission(userId)) {
109                 return false;
110             }
111             else {
112                 return true;
113             }
114         }
115         catch (Exception e) {
116             if (_log.isWarnEnabled()) {
117                 _log.warn(
118                     "Could not check permissions for " + pluginId, e);
119             }
120 
121             return false;
122         }
123     }
124 
125     public PluginSetting updatePluginSetting(
126             long companyId, String pluginId, String pluginType, String roles,
127             boolean active)
128         throws SystemException {
129 
130         pluginId = PortalUtil.getJsSafePortletId(pluginId);
131 
132         PluginSetting pluginSetting = pluginSettingPersistence.fetchByC_I_T(
133             companyId, pluginId, pluginType);
134 
135         if (pluginSetting == null) {
136             long pluginSettingId = counterLocalService.increment();
137 
138             pluginSetting = pluginSettingPersistence.create(pluginSettingId);
139 
140             pluginSetting.setCompanyId(companyId);
141             pluginSetting.setPluginId(pluginId);
142             pluginSetting.setPluginType(pluginType);
143         }
144 
145         pluginSetting.setRoles(roles);
146         pluginSetting.setActive(active);
147 
148         pluginSettingPersistence.update(pluginSetting, false);
149 
150         return pluginSetting;
151     }
152 
153     private static Log _log =
154         LogFactoryUtil.getLog(PluginSettingLocalServiceImpl.class);
155 
156 }