001    /**
002     * Copyright (c) 2000-2013 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            @Override
037            public void checkPermission(long userId, String pluginId, String pluginType)
038                    throws PortalException {
039    
040                    if (!hasPermission(userId, pluginId, pluginType)) {
041                            throw new PrincipalException();
042                    }
043            }
044    
045            @Override
046            public PluginSetting getDefaultPluginSetting() {
047                    PluginSettingImpl pluginSetting = new PluginSettingImpl();
048    
049                    pluginSetting.setRoles(StringPool.BLANK);
050                    pluginSetting.setActive(true);
051    
052                    return pluginSetting;
053            }
054    
055            @Override
056            public PluginSetting getPluginSetting(
057                            long companyId, String pluginId, String pluginType)
058                    throws SystemException {
059    
060                    PluginSetting pluginSetting = pluginSettingPersistence.fetchByC_I_T(
061                            companyId, pluginId, pluginType);
062    
063                    if (pluginSetting != null) {
064                            return pluginSetting;
065                    }
066    
067                    Plugin plugin = null;
068    
069                    if (pluginType.equals(Plugin.TYPE_LAYOUT_TEMPLATE)) {
070                            plugin = layoutTemplateLocalService.getLayoutTemplate(
071                                    pluginId, false, null);
072                    }
073                    else if (pluginType.equals(Plugin.TYPE_THEME)) {
074                            boolean wapTheme = true;
075    
076                            plugin = themeLocalService.getTheme(companyId, pluginId, wapTheme);
077                    }
078    
079                    if ((plugin == null) || (plugin.getDefaultPluginSetting() == null)) {
080                            pluginSetting = getDefaultPluginSetting();
081    
082                            pluginSetting.setCompanyId(companyId);
083                    }
084                    else {
085                            pluginSetting = plugin.getDefaultPluginSetting(companyId);
086                    }
087    
088                    return pluginSetting;
089            }
090    
091            @Override
092            public boolean hasPermission(
093                    long userId, String pluginId, String pluginType) {
094    
095                    try {
096                            User user = userPersistence.findByPrimaryKey(userId);
097    
098                            PluginSetting pluginSetting = getPluginSetting(
099                                    user.getCompanyId(), pluginId, pluginType);
100    
101                            if (!pluginSetting.hasPermission(userId)) {
102                                    return false;
103                            }
104                            else {
105                                    return true;
106                            }
107                    }
108                    catch (Exception e) {
109                            if (_log.isWarnEnabled()) {
110                                    _log.warn("Could not check permissions for " + pluginId, e);
111                            }
112    
113                            return false;
114                    }
115            }
116    
117            @Override
118            public PluginSetting updatePluginSetting(
119                            long companyId, String pluginId, String pluginType, String roles,
120                            boolean active)
121                    throws SystemException {
122    
123                    pluginId = PortalUtil.getJsSafePortletId(pluginId);
124    
125                    PluginSetting pluginSetting = pluginSettingPersistence.fetchByC_I_T(
126                            companyId, pluginId, pluginType);
127    
128                    if (pluginSetting == null) {
129                            long pluginSettingId = counterLocalService.increment();
130    
131                            pluginSetting = pluginSettingPersistence.create(pluginSettingId);
132    
133                            pluginSetting.setCompanyId(companyId);
134                            pluginSetting.setPluginId(pluginId);
135                            pluginSetting.setPluginType(pluginType);
136                    }
137    
138                    pluginSetting.setRoles(roles);
139                    pluginSetting.setActive(active);
140    
141                    pluginSettingPersistence.update(pluginSetting, false);
142    
143                    return pluginSetting;
144            }
145    
146            private static Log _log = LogFactoryUtil.getLog(
147                    PluginSettingLocalServiceImpl.class);
148    
149    }