001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(
037                            long userId, String pluginId, String pluginType)
038                    throws PortalException {
039    
040                    if (!hasPermission(userId, pluginId, pluginType)) {
041                            throw new PrincipalException();
042                    }
043            }
044    
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            public PluginSetting getPluginSetting(
055                            long companyId, String pluginId, String pluginType)
056                    throws SystemException {
057    
058                    PluginSetting pluginSetting = pluginSettingPersistence.fetchByC_I_T(
059                            companyId, pluginId, pluginType);
060    
061                    if (pluginSetting == null) {
062                            Plugin plugin = null;
063    
064                            if (pluginType.equals(Plugin.TYPE_LAYOUT_TEMPLATE)) {
065                                    plugin = layoutTemplateLocalService.getLayoutTemplate(
066                                            pluginId, false, null);
067                            }
068                            else if (pluginType.equals(Plugin.TYPE_THEME)) {
069                                    boolean wapTheme = true;
070    
071                                    plugin = themeLocalService.getTheme(
072                                            companyId, pluginId, wapTheme);
073                            }
074    
075                            if ((plugin == null) ||
076                                    (plugin.getDefaultPluginSetting() == null)) {
077    
078                                    pluginSetting = getDefaultPluginSetting();
079    
080                                    pluginSetting.setCompanyId(companyId);
081                            }
082                            else {
083                                    pluginSetting = plugin.getDefaultPluginSetting(companyId);
084                            }
085                    }
086    
087                    return pluginSetting;
088            }
089    
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(
109                                            "Could not check permissions for " + pluginId, e);
110                            }
111    
112                            return false;
113                    }
114            }
115    
116            public PluginSetting updatePluginSetting(
117                            long companyId, String pluginId, String pluginType, String roles,
118                            boolean active)
119                    throws SystemException {
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, false);
140    
141                    return pluginSetting;
142            }
143    
144            private static Log _log = LogFactoryUtil.getLog(
145                    PluginSettingLocalServiceImpl.class);
146    
147    }