001    /**
002     * Copyright (c) 2000-present 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.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.model.Plugin;
022    import com.liferay.portal.model.PluginSetting;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.model.impl.PluginSettingImpl;
025    import com.liferay.portal.security.auth.PrincipalException;
026    import com.liferay.portal.service.base.PluginSettingLocalServiceBaseImpl;
027    import com.liferay.portal.util.PortalUtil;
028    
029    /**
030     * @author Jorge Ferrer
031     */
032    public class PluginSettingLocalServiceImpl
033            extends PluginSettingLocalServiceBaseImpl {
034    
035            @Override
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            @Override
045            public PluginSetting getDefaultPluginSetting() {
046                    PluginSettingImpl pluginSetting = new PluginSettingImpl();
047    
048                    pluginSetting.setRoles(StringPool.BLANK);
049                    pluginSetting.setActive(true);
050    
051                    return pluginSetting;
052            }
053    
054            @Override
055            public PluginSetting getPluginSetting(
056                    long companyId, String pluginId, String pluginType) {
057    
058                    PluginSetting pluginSetting = pluginSettingPersistence.fetchByC_I_T(
059                            companyId, pluginId, pluginType);
060    
061                    if (pluginSetting != null) {
062                            return pluginSetting;
063                    }
064    
065                    Plugin plugin = null;
066    
067                    if (pluginType.equals(Plugin.TYPE_LAYOUT_TEMPLATE)) {
068                            plugin = layoutTemplateLocalService.getLayoutTemplate(
069                                    pluginId, false, null);
070                    }
071                    else if (pluginType.equals(Plugin.TYPE_THEME)) {
072                            boolean wapTheme = true;
073    
074                            plugin = themeLocalService.getTheme(companyId, pluginId, wapTheme);
075                    }
076    
077                    if ((plugin == null) || (plugin.getDefaultPluginSetting() == null)) {
078                            pluginSetting = getDefaultPluginSetting();
079    
080                            pluginSetting.setCompanyId(companyId);
081                    }
082                    else {
083                            pluginSetting = plugin.getDefaultPluginSetting(companyId);
084                    }
085    
086                    return pluginSetting;
087            }
088    
089            @Override
090            public boolean hasPermission(
091                    long userId, String pluginId, String pluginType) {
092    
093                    try {
094                            User user = userPersistence.findByPrimaryKey(userId);
095    
096                            PluginSetting pluginSetting = getPluginSetting(
097                                    user.getCompanyId(), pluginId, pluginType);
098    
099                            if (!pluginSetting.hasPermission(userId)) {
100                                    return false;
101                            }
102                            else {
103                                    return true;
104                            }
105                    }
106                    catch (Exception e) {
107                            if (_log.isWarnEnabled()) {
108                                    _log.warn("Could not check permissions for " + pluginId, e);
109                            }
110    
111                            return false;
112                    }
113            }
114    
115            @Override
116            public PluginSetting updatePluginSetting(
117                    long companyId, String pluginId, String pluginType, String roles,
118                    boolean active) {
119    
120                    pluginId = PortalUtil.getJsSafePortletId(pluginId);
121    
122                    PluginSetting pluginSetting = pluginSettingPersistence.fetchByC_I_T(
123                            companyId, pluginId, pluginType);
124    
125                    if (pluginSetting == null) {
126                            long pluginSettingId = counterLocalService.increment();
127    
128                            pluginSetting = pluginSettingPersistence.create(pluginSettingId);
129    
130                            pluginSetting.setCompanyId(companyId);
131                            pluginSetting.setPluginId(pluginId);
132                            pluginSetting.setPluginType(pluginType);
133                    }
134    
135                    pluginSetting.setRoles(roles);
136                    pluginSetting.setActive(active);
137    
138                    pluginSettingPersistence.update(pluginSetting);
139    
140                    return pluginSetting;
141            }
142    
143            private static final Log _log = LogFactoryUtil.getLog(
144                    PluginSettingLocalServiceImpl.class);
145    
146    }