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.model.Plugin;
021    import com.liferay.portal.kernel.model.PluginSetting;
022    import com.liferay.portal.kernel.model.User;
023    import com.liferay.portal.kernel.security.auth.PrincipalException;
024    import com.liferay.portal.kernel.util.PortalUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.model.impl.PluginSettingImpl;
027    import com.liferay.portal.service.base.PluginSettingLocalServiceBaseImpl;
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.MustHavePermission(
041                                    userId, pluginType, pluginId);
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    
059                    PluginSetting pluginSetting = pluginSettingPersistence.fetchByC_I_T(
060                            companyId, pluginId, pluginType);
061    
062                    if (pluginSetting != null) {
063                            return pluginSetting;
064                    }
065    
066                    Plugin plugin = null;
067    
068                    if (pluginType.equals(Plugin.TYPE_LAYOUT_TEMPLATE)) {
069                            plugin = layoutTemplateLocalService.getLayoutTemplate(
070                                    pluginId, false, null);
071                    }
072                    else if (pluginType.equals(Plugin.TYPE_THEME)) {
073                            plugin = themeLocalService.getTheme(companyId, pluginId);
074                    }
075    
076                    if ((plugin == null) || (plugin.getDefaultPluginSetting() == null)) {
077                            pluginSetting = getDefaultPluginSetting();
078    
079                            pluginSetting.setCompanyId(companyId);
080                    }
081                    else {
082                            pluginSetting = plugin.getDefaultPluginSetting(companyId);
083                    }
084    
085                    return pluginSetting;
086            }
087    
088            @Override
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            @Override
115            public PluginSetting updatePluginSetting(
116                    long companyId, String pluginId, String pluginType, String roles,
117                    boolean active) {
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);
138    
139                    return pluginSetting;
140            }
141    
142            private static final Log _log = LogFactoryUtil.getLog(
143                    PluginSettingLocalServiceImpl.class);
144    
145    }