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 PortalException, 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 = LayoutTemplateLocalUtil.getLayoutTemplate(
79                      pluginId, false, null);
80              }
81              else if (pluginType.equals(Plugin.TYPE_THEME)) {
82                  boolean wapTheme = true;
83  
84                  plugin = ThemeLocalUtil.getTheme(companyId, pluginId, wapTheme);
85              }
86  
87              if ((plugin == null) ||
88                  (plugin.getDefaultPluginSetting() == null)) {
89  
90                  pluginSetting = getDefaultPluginSetting();
91  
92                  pluginSetting.setCompanyId(companyId);
93              }
94              else {
95                  pluginSetting = plugin.getDefaultPluginSetting(companyId);
96              }
97          }
98  
99          return pluginSetting;
100     }
101 
102     public boolean hasPermission(
103         long userId, String pluginId, String pluginType) {
104 
105         try {
106             User user = userPersistence.findByPrimaryKey(userId);
107 
108             PluginSetting pluginSetting = getPluginSetting(
109                 user.getCompanyId(), pluginId, pluginType);
110 
111             if (!pluginSetting.hasPermission(userId)) {
112                 return false;
113             }
114             else {
115                 return true;
116             }
117         }
118         catch (Exception e) {
119             if (_log.isWarnEnabled()) {
120                 _log.warn(
121                     "Could not check permissions for " + pluginId, e);
122             }
123 
124             return false;
125         }
126     }
127 
128     public PluginSetting updatePluginSetting(
129             long companyId, String pluginId, String pluginType, String roles,
130             boolean active)
131         throws PortalException, SystemException {
132 
133         pluginId = PortalUtil.getJsSafePortletId(pluginId);
134 
135         PluginSetting pluginSetting = pluginSettingPersistence.fetchByC_I_T(
136             companyId, pluginId, pluginType);
137 
138         if (pluginSetting == null) {
139             long pluginSettingId = counterLocalService.increment();
140 
141             pluginSetting = pluginSettingPersistence.create(pluginSettingId);
142 
143             pluginSetting.setCompanyId(companyId);
144             pluginSetting.setPluginId(pluginId);
145             pluginSetting.setPluginType(pluginType);
146         }
147 
148         pluginSetting.setRoles(roles);
149         pluginSetting.setActive(active);
150 
151         pluginSettingPersistence.update(pluginSetting, false);
152 
153         return pluginSetting;
154     }
155 
156     private static Log _log =
157         LogFactory.getLog(PluginSettingLocalServiceImpl.class);
158 
159 }