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.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.model.Plugin;
23  import com.liferay.portal.model.PluginSetting;
24  import com.liferay.portal.model.User;
25  import com.liferay.portal.model.impl.PluginSettingImpl;
26  import com.liferay.portal.security.auth.PrincipalException;
27  import com.liferay.portal.service.base.PluginSettingLocalServiceBaseImpl;
28  import com.liferay.portal.util.PortalUtil;
29  
30  /**
31   * <a href="PluginSettingLocalServiceImpl.java.html"><b><i>View Source</i></b>
32   * </a>
33   *
34   * @author Jorge Ferrer
35   */
36  public class PluginSettingLocalServiceImpl
37      extends PluginSettingLocalServiceBaseImpl {
38  
39      public void checkPermission(
40              long userId, String pluginId, String pluginType)
41          throws PortalException {
42  
43          if (!hasPermission(userId, pluginId, pluginType)) {
44              throw new PrincipalException();
45          }
46      }
47  
48      public PluginSetting getDefaultPluginSetting() {
49          PluginSettingImpl pluginSetting = new PluginSettingImpl();
50  
51          pluginSetting.setRoles(StringPool.BLANK);
52          pluginSetting.setActive(true);
53  
54          return pluginSetting;
55      }
56  
57      public PluginSetting getPluginSetting(
58              long companyId, String pluginId, String pluginType)
59          throws SystemException {
60  
61          PluginSetting pluginSetting = pluginSettingPersistence.fetchByC_I_T(
62              companyId, pluginId, pluginType);
63  
64          if (pluginSetting == null) {
65              Plugin plugin = null;
66  
67              if (pluginType.equals(Plugin.TYPE_LAYOUT_TEMPLATE)) {
68                  plugin = layoutTemplateLocalService.getLayoutTemplate(
69                      pluginId, false, null);
70              }
71              else if (pluginType.equals(Plugin.TYPE_THEME)) {
72                  boolean wapTheme = true;
73  
74                  plugin = themeLocalService.getTheme(
75                      companyId, pluginId, wapTheme);
76              }
77  
78              if ((plugin == null) ||
79                  (plugin.getDefaultPluginSetting() == null)) {
80  
81                  pluginSetting = getDefaultPluginSetting();
82  
83                  pluginSetting.setCompanyId(companyId);
84              }
85              else {
86                  pluginSetting = plugin.getDefaultPluginSetting(companyId);
87              }
88          }
89  
90          return pluginSetting;
91      }
92  
93      public boolean hasPermission(
94          long userId, String pluginId, String pluginType) {
95  
96          try {
97              User user = userPersistence.findByPrimaryKey(userId);
98  
99              PluginSetting pluginSetting = getPluginSetting(
100                 user.getCompanyId(), pluginId, pluginType);
101 
102             if (!pluginSetting.hasPermission(userId)) {
103                 return false;
104             }
105             else {
106                 return true;
107             }
108         }
109         catch (Exception e) {
110             if (_log.isWarnEnabled()) {
111                 _log.warn(
112                     "Could not check permissions for " + pluginId, e);
113             }
114 
115             return false;
116         }
117     }
118 
119     public PluginSetting updatePluginSetting(
120             long companyId, String pluginId, String pluginType, String roles,
121             boolean active)
122         throws SystemException {
123 
124         pluginId = PortalUtil.getJsSafePortletId(pluginId);
125 
126         PluginSetting pluginSetting = pluginSettingPersistence.fetchByC_I_T(
127             companyId, pluginId, pluginType);
128 
129         if (pluginSetting == null) {
130             long pluginSettingId = counterLocalService.increment();
131 
132             pluginSetting = pluginSettingPersistence.create(pluginSettingId);
133 
134             pluginSetting.setCompanyId(companyId);
135             pluginSetting.setPluginId(pluginId);
136             pluginSetting.setPluginType(pluginType);
137         }
138 
139         pluginSetting.setRoles(roles);
140         pluginSetting.setActive(active);
141 
142         pluginSettingPersistence.update(pluginSetting, false);
143 
144         return pluginSetting;
145     }
146 
147     private static Log _log = LogFactoryUtil.getLog(
148         PluginSettingLocalServiceImpl.class);
149 
150 }