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