1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.util.StringPool;
28  import com.liferay.portal.model.Plugin;
29  import com.liferay.portal.model.PluginSetting;
30  import com.liferay.portal.model.User;
31  import com.liferay.portal.model.impl.PluginSettingImpl;
32  import com.liferay.portal.security.auth.PrincipalException;
33  import com.liferay.portal.service.base.PluginSettingLocalServiceBaseImpl;
34  import com.liferay.portal.util.PortalUtil;
35  
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  
39  /**
40   * <a href="PluginSettingLocalServiceImpl.java.html"><b><i>View Source</i></b>
41   * </a>
42   *
43   * @author Jorge Ferrer
44   *
45   */
46  public class PluginSettingLocalServiceImpl
47      extends PluginSettingLocalServiceBaseImpl {
48  
49      public void checkPermission(
50              long userId, String pluginId, String pluginType)
51          throws PortalException {
52  
53          if (!hasPermission(userId, pluginId, pluginType)) {
54              throw new PrincipalException();
55          }
56      }
57  
58      public PluginSetting getDefaultPluginSetting() {
59          PluginSettingImpl pluginSetting = new PluginSettingImpl();
60  
61          pluginSetting.setRoles(StringPool.BLANK);
62          pluginSetting.setActive(true);
63  
64          return pluginSetting;
65      }
66  
67      public PluginSetting getPluginSetting(
68              long companyId, String pluginId, String pluginType)
69          throws SystemException {
70  
71          PluginSetting pluginSetting = pluginSettingPersistence.fetchByC_I_T(
72              companyId, pluginId, pluginType);
73  
74          if (pluginSetting == null) {
75              Plugin plugin = null;
76  
77              if (pluginType.equals(Plugin.TYPE_LAYOUT_TEMPLATE)) {
78                  plugin = layoutTemplateLocalService.getLayoutTemplate(
79                      pluginId, false, null);
80              }
81              else if (pluginType.equals(Plugin.TYPE_THEME)) {
82                  boolean wapTheme = true;
83  
84                  plugin = themeLocalService.getTheme(
85                      companyId, pluginId, wapTheme);
86              }
87  
88              if ((plugin == null) ||
89                  (plugin.getDefaultPluginSetting() == null)) {
90  
91                  pluginSetting = getDefaultPluginSetting();
92  
93                  pluginSetting.setCompanyId(companyId);
94              }
95              else {
96                  pluginSetting = plugin.getDefaultPluginSetting(companyId);
97              }
98          }
99  
100         return pluginSetting;
101     }
102 
103     public boolean hasPermission(
104         long userId, String pluginId, String pluginType) {
105 
106         try {
107             User user = userPersistence.findByPrimaryKey(userId);
108 
109             PluginSetting pluginSetting = getPluginSetting(
110                 user.getCompanyId(), pluginId, pluginType);
111 
112             if (!pluginSetting.hasPermission(userId)) {
113                 return false;
114             }
115             else {
116                 return true;
117             }
118         }
119         catch (Exception e) {
120             if (_log.isWarnEnabled()) {
121                 _log.warn(
122                     "Could not check permissions for " + pluginId, e);
123             }
124 
125             return false;
126         }
127     }
128 
129     public PluginSetting updatePluginSetting(
130             long companyId, String pluginId, String pluginType, String roles,
131             boolean active)
132         throws SystemException {
133 
134         pluginId = PortalUtil.getJsSafePortletId(pluginId);
135 
136         PluginSetting pluginSetting = pluginSettingPersistence.fetchByC_I_T(
137             companyId, pluginId, pluginType);
138 
139         if (pluginSetting == null) {
140             long pluginSettingId = counterLocalService.increment();
141 
142             pluginSetting = pluginSettingPersistence.create(pluginSettingId);
143 
144             pluginSetting.setCompanyId(companyId);
145             pluginSetting.setPluginId(pluginId);
146             pluginSetting.setPluginType(pluginType);
147         }
148 
149         pluginSetting.setRoles(roles);
150         pluginSetting.setActive(active);
151 
152         pluginSettingPersistence.update(pluginSetting, false);
153 
154         return pluginSetting;
155     }
156 
157     private static Log _log =
158         LogFactory.getLog(PluginSettingLocalServiceImpl.class);
159 
160 }