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   */
45  public class PluginSettingLocalServiceImpl
46      extends PluginSettingLocalServiceBaseImpl {
47  
48      public void checkPermission(
49              long userId, String pluginId, String pluginType)
50          throws PortalException {
51  
52          if (!hasPermission(userId, pluginId, pluginType)) {
53              throw new PrincipalException();
54          }
55      }
56  
57      public PluginSetting getDefaultPluginSetting() {
58          PluginSettingImpl pluginSetting = new PluginSettingImpl();
59  
60          pluginSetting.setRoles(StringPool.BLANK);
61          pluginSetting.setActive(true);
62  
63          return pluginSetting;
64      }
65  
66      public PluginSetting getPluginSetting(
67              long companyId, String pluginId, String pluginType)
68          throws SystemException {
69  
70          PluginSetting pluginSetting = pluginSettingPersistence.fetchByC_I_T(
71              companyId, pluginId, pluginType);
72  
73          if (pluginSetting == null) {
74              Plugin plugin = null;
75  
76              if (pluginType.equals(Plugin.TYPE_LAYOUT_TEMPLATE)) {
77                  plugin = layoutTemplateLocalService.getLayoutTemplate(
78                      pluginId, false, null);
79              }
80              else if (pluginType.equals(Plugin.TYPE_THEME)) {
81                  boolean wapTheme = true;
82  
83                  plugin = themeLocalService.getTheme(
84                      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 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         LogFactoryUtil.getLog(PluginSettingLocalServiceImpl.class);
158 
159 }