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