001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.service.impl;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.util.StringPool;
026: import com.liferay.portal.model.Plugin;
027: import com.liferay.portal.model.PluginSetting;
028: import com.liferay.portal.model.User;
029: import com.liferay.portal.model.impl.LayoutTemplateImpl;
030: import com.liferay.portal.model.impl.PluginSettingImpl;
031: import com.liferay.portal.model.impl.ThemeImpl;
032: import com.liferay.portal.security.auth.PrincipalException;
033: import com.liferay.portal.service.base.PluginSettingLocalServiceBaseImpl;
034: import com.liferay.portal.util.PortalUtil;
035:
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038:
039: /**
040: * <a href="PluginSettingLocalServiceImpl.java.html"><b><i>View Source</i></b>
041: * </a>
042: *
043: * @author Jorge Ferrer
044: *
045: */
046: public class PluginSettingLocalServiceImpl extends
047: PluginSettingLocalServiceBaseImpl {
048:
049: public void checkPermission(long userId, String pluginId,
050: String pluginType) throws PortalException {
051:
052: if (!hasPermission(userId, pluginId, pluginType)) {
053: throw new PrincipalException();
054: }
055: }
056:
057: public PluginSetting getDefaultPluginSetting() {
058: PluginSettingImpl pluginSetting = new PluginSettingImpl();
059:
060: pluginSetting.setRoles(StringPool.BLANK);
061: pluginSetting.setActive(true);
062:
063: return pluginSetting;
064: }
065:
066: public PluginSetting getPluginSetting(long companyId,
067: String pluginId, String pluginType) throws PortalException,
068: SystemException {
069:
070: PluginSetting pluginSetting = pluginSettingPersistence
071: .fetchByC_I_T(companyId, pluginId, pluginType);
072:
073: if (pluginSetting == null) {
074: Plugin plugin = null;
075:
076: if (pluginType.equals(LayoutTemplateImpl.PLUGIN_TYPE)) {
077: plugin = LayoutTemplateLocalUtil.getLayoutTemplate(
078: pluginId, false, null);
079: } else if (pluginType.equals(ThemeImpl.PLUGIN_TYPE)) {
080: boolean wapTheme = true;
081:
082: plugin = ThemeLocalUtil.getTheme(companyId, pluginId,
083: wapTheme);
084: }
085:
086: if ((plugin == null)
087: || (plugin.getDefaultPluginSetting() == null)) {
088:
089: pluginSetting = getDefaultPluginSetting();
090:
091: pluginSetting.setCompanyId(companyId);
092: } else {
093: pluginSetting = plugin
094: .getDefaultPluginSetting(companyId);
095: }
096: }
097:
098: return pluginSetting;
099: }
100:
101: public boolean hasPermission(long userId, String pluginId,
102: String pluginType) {
103:
104: try {
105: User user = userPersistence.findByPrimaryKey(userId);
106:
107: PluginSetting pluginSetting = getPluginSetting(user
108: .getCompanyId(), pluginId, pluginType);
109:
110: if (!pluginSetting.hasPermission(userId)) {
111: return false;
112: } else {
113: return true;
114: }
115: } catch (Exception e) {
116: if (_log.isWarnEnabled()) {
117: _log.warn(
118: "Could not check permissions for " + pluginId,
119: e);
120: }
121:
122: return false;
123: }
124: }
125:
126: public PluginSetting updatePluginSetting(long companyId,
127: String pluginId, String pluginType, String roles,
128: boolean active) throws PortalException, SystemException {
129:
130: pluginId = PortalUtil.getJsSafePortletId(pluginId);
131:
132: PluginSetting pluginSetting = pluginSettingPersistence
133: .fetchByC_I_T(companyId, pluginId, pluginType);
134:
135: if (pluginSetting == null) {
136: long pluginSettingId = counterLocalService.increment();
137:
138: pluginSetting = pluginSettingPersistence
139: .create(pluginSettingId);
140:
141: pluginSetting.setCompanyId(companyId);
142: pluginSetting.setPluginId(pluginId);
143: pluginSetting.setPluginType(pluginType);
144: }
145:
146: pluginSetting.setRoles(roles);
147: pluginSetting.setActive(active);
148:
149: pluginSettingPersistence.update(pluginSetting);
150:
151: return pluginSetting;
152: }
153:
154: private static Log _log = LogFactory
155: .getLog(PluginSettingLocalServiceImpl.class);
156:
157: }
|