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.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                            boolean wapTheme = true;
074    
075                            plugin = themeLocalService.getTheme(companyId, pluginId, wapTheme);
076                    }
077    
078                    if ((plugin == null) || (plugin.getDefaultPluginSetting() == null)) {
079                            pluginSetting = getDefaultPluginSetting();
080    
081                            pluginSetting.setCompanyId(companyId);
082                    }
083                    else {
084                            pluginSetting = plugin.getDefaultPluginSetting(companyId);
085                    }
086    
087                    return pluginSetting;
088            }
089    
090            @Override
091            public boolean hasPermission(
092                    long userId, String pluginId, String pluginType) {
093    
094                    try {
095                            User user = userPersistence.findByPrimaryKey(userId);
096    
097                            PluginSetting pluginSetting = getPluginSetting(
098                                    user.getCompanyId(), pluginId, pluginType);
099    
100                            if (!pluginSetting.hasPermission(userId)) {
101                                    return false;
102                            }
103                            else {
104                                    return true;
105                            }
106                    }
107                    catch (Exception e) {
108                            if (_log.isWarnEnabled()) {
109                                    _log.warn("Could not check permissions for " + pluginId, e);
110                            }
111    
112                            return false;
113                    }
114            }
115    
116            @Override
117            public PluginSetting updatePluginSetting(
118                    long companyId, String pluginId, String pluginType, String roles,
119                    boolean active) {
120    
121                    pluginId = PortalUtil.getJsSafePortletId(pluginId);
122    
123                    PluginSetting pluginSetting = pluginSettingPersistence.fetchByC_I_T(
124                            companyId, pluginId, pluginType);
125    
126                    if (pluginSetting == null) {
127                            long pluginSettingId = counterLocalService.increment();
128    
129                            pluginSetting = pluginSettingPersistence.create(pluginSettingId);
130    
131                            pluginSetting.setCompanyId(companyId);
132                            pluginSetting.setPluginId(pluginId);
133                            pluginSetting.setPluginType(pluginType);
134                    }
135    
136                    pluginSetting.setRoles(roles);
137                    pluginSetting.setActive(active);
138    
139                    pluginSettingPersistence.update(pluginSetting);
140    
141                    return pluginSetting;
142            }
143    
144            private static final Log _log = LogFactoryUtil.getLog(
145                    PluginSettingLocalServiceImpl.class);
146    
147    }