001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.model.Plugin;
023    import com.liferay.portal.model.PluginSetting;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.model.impl.PluginSettingImpl;
026    import com.liferay.portal.security.auth.PrincipalException;
027    import com.liferay.portal.service.base.PluginSettingLocalServiceBaseImpl;
028    import com.liferay.portal.util.PortalUtil;
029    
030    /**
031     * @author Jorge Ferrer
032     */
033    public class PluginSettingLocalServiceImpl
034            extends PluginSettingLocalServiceBaseImpl {
035    
036            public void checkPermission(long userId, String pluginId, String pluginType)
037                    throws PortalException {
038    
039                    if (!hasPermission(userId, pluginId, pluginType)) {
040                            throw new PrincipalException();
041                    }
042            }
043    
044            public PluginSetting getDefaultPluginSetting() {
045                    PluginSettingImpl pluginSetting = new PluginSettingImpl();
046    
047                    pluginSetting.setRoles(StringPool.BLANK);
048                    pluginSetting.setActive(true);
049    
050                    return pluginSetting;
051            }
052    
053            public PluginSetting getPluginSetting(
054                            long companyId, String pluginId, String pluginType)
055                    throws SystemException {
056    
057                    PluginSetting pluginSetting = pluginSettingPersistence.fetchByC_I_T(
058                            companyId, pluginId, pluginType);
059    
060                    if (pluginSetting == null) {
061                            Plugin plugin = null;
062    
063                            if (pluginType.equals(Plugin.TYPE_LAYOUT_TEMPLATE)) {
064                                    plugin = layoutTemplateLocalService.getLayoutTemplate(
065                                            pluginId, false, null);
066                            }
067                            else if (pluginType.equals(Plugin.TYPE_THEME)) {
068                                    boolean wapTheme = true;
069    
070                                    plugin = themeLocalService.getTheme(
071                                            companyId, pluginId, wapTheme);
072                            }
073    
074                            if ((plugin == null) ||
075                                    (plugin.getDefaultPluginSetting() == null)) {
076    
077                                    pluginSetting = getDefaultPluginSetting();
078    
079                                    pluginSetting.setCompanyId(companyId);
080                            }
081                            else {
082                                    pluginSetting = plugin.getDefaultPluginSetting(companyId);
083                            }
084                    }
085    
086                    return pluginSetting;
087            }
088    
089            public boolean hasPermission(
090                    long userId, String pluginId, String pluginType) {
091    
092                    try {
093                            User user = userPersistence.findByPrimaryKey(userId);
094    
095                            PluginSetting pluginSetting = getPluginSetting(
096                                    user.getCompanyId(), pluginId, pluginType);
097    
098                            if (!pluginSetting.hasPermission(userId)) {
099                                    return false;
100                            }
101                            else {
102                                    return true;
103                            }
104                    }
105                    catch (Exception e) {
106                            if (_log.isWarnEnabled()) {
107                                    _log.warn("Could not check permissions for " + pluginId, e);
108                            }
109    
110                            return false;
111                    }
112            }
113    
114            public PluginSetting updatePluginSetting(
115                            long companyId, String pluginId, String pluginType, String roles,
116                            boolean active)
117                    throws SystemException {
118    
119                    pluginId = PortalUtil.getJsSafePortletId(pluginId);
120    
121                    PluginSetting pluginSetting = pluginSettingPersistence.fetchByC_I_T(
122                            companyId, pluginId, pluginType);
123    
124                    if (pluginSetting == null) {
125                            long pluginSettingId = counterLocalService.increment();
126    
127                            pluginSetting = pluginSettingPersistence.create(pluginSettingId);
128    
129                            pluginSetting.setCompanyId(companyId);
130                            pluginSetting.setPluginId(pluginId);
131                            pluginSetting.setPluginType(pluginType);
132                    }
133    
134                    pluginSetting.setRoles(roles);
135                    pluginSetting.setActive(active);
136    
137                    pluginSettingPersistence.update(pluginSetting, false);
138    
139                    return pluginSetting;
140            }
141    
142            private static Log _log = LogFactoryUtil.getLog(
143                    PluginSettingLocalServiceImpl.class);
144    
145    }