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