/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.portal.settings.portlet.action;
import com.liferay.portal.kernel.portlet.bridges.mvc.BaseFormMVCActionCommand;
import com.liferay.portal.kernel.security.auth.PrincipalException;
import com.liferay.portal.kernel.security.permission.PermissionChecker;
import com.liferay.portal.kernel.security.permission.PermissionThreadLocal;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.settings.CompanyServiceSettingsLocator;
import com.liferay.portal.kernel.settings.ModifiableSettings;
import com.liferay.portal.kernel.settings.Settings;
import com.liferay.portal.kernel.settings.SettingsDescriptor;
import com.liferay.portal.kernel.settings.SettingsException;
import com.liferay.portal.kernel.settings.SettingsFactoryUtil;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Portal;
import com.liferay.portal.kernel.util.WebKeys;
import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.ValidatorException;
/**
* @author Tomas Polesovsky
* @author Philip Jones
*/
public abstract class BasePortalSettingsFormMVCActionCommand
extends BaseFormMVCActionCommand {
@Override
protected void doProcessAction(
ActionRequest actionRequest, ActionResponse actionResponse)
throws Exception {
ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
WebKeys.THEME_DISPLAY);
if (!hasPermissions(actionRequest, actionResponse, themeDisplay)) {
return;
}
storeSettings(actionRequest, themeDisplay);
}
protected boolean getBoolean(ActionRequest actionRequest, String name) {
return ParamUtil.getBoolean(
actionRequest, getParameterNamespace() + name);
}
protected abstract String getParameterNamespace();
protected abstract String getSettingsId();
protected String getString(ActionRequest actionRequest, String name) {
return ParamUtil.getString(
actionRequest, getParameterNamespace() + name);
}
protected boolean hasPermissions(
ActionRequest actionRequest, ActionResponse actionResponse,
ThemeDisplay themeDisplay) {
PermissionChecker permissionChecker =
PermissionThreadLocal.getPermissionChecker();
if (!permissionChecker.isCompanyAdmin(themeDisplay.getCompanyId())) {
SessionErrors.add(actionRequest, PrincipalException.class);
actionResponse.setRenderParameter("mvcPath", "/error.jsp");
return false;
}
return true;
}
protected void storeSettings(
ActionRequest actionRequest, ThemeDisplay themeDisplay)
throws IOException, SettingsException, ValidatorException {
Settings settings = SettingsFactoryUtil.getSettings(
new CompanyServiceSettingsLocator(
themeDisplay.getCompanyId(), getSettingsId()));
ModifiableSettings modifiableSettings =
settings.getModifiableSettings();
SettingsDescriptor settingsDescriptor =
SettingsFactoryUtil.getSettingsDescriptor(getSettingsId());
for (String name : settingsDescriptor.getAllKeys()) {
String value = getString(actionRequest, name);
if (value.equals(Portal.TEMP_OBFUSCATION_VALUE)) {
continue;
}
String oldValue = settings.getValue(name, null);
if (!value.equals(oldValue)) {
modifiableSettings.setValue(name, value);
}
}
modifiableSettings.store();
}
}