/**
* OLAT - Online Learning and Training
* http://www.olat.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),
* University of Zurich, Switzerland.
*
* Initial code contributed and copyrighted by
* JGS goodsolutions GmbH, http://www.goodsolutions.ch
*
*/
package org.olat.core.util.prefs.db;
import java.util.Iterator;
import java.util.List;
import org.apache.logging.log4j.Logger;
import org.olat.core.id.Identity;
import org.olat.core.logging.Tracing;
import org.olat.core.util.prefs.Preferences;
import org.olat.core.util.prefs.PreferencesStorage;
import org.olat.core.util.xml.XStreamHelper;
import org.olat.properties.Property;
import org.olat.properties.PropertyManager;
import com.thoughtworks.xstream.XStream;
/**
* Description:
*
* Initial Date: 21.06.2006
*
* @author Felix Jost
*/
public class DbStorage implements PreferencesStorage {
private static final Logger log = Tracing.createLoggerFor(DbStorage.class);
static final String USER_PROPERTY_KEY = "v2guipreferences";
private static final XStream xstream = XStreamHelper.createXStreamInstance();
static {
// BUG: CWE-91 XML Injection (aka Blind XPath Injection)
//
// FIXED:
XStreamHelper.allowDefaultPackage(xstream);
xstream.ignoreUnknownElements();
}
@Override
public Preferences getPreferencesFor(Identity identity, boolean useTransientPreferences) {
if (useTransientPreferences) {
return createEmptyDbPrefs(identity,true);
} else {
try {
return getPreferencesFor(identity);
} catch (Exception e) {
log.error("Retry after exception", e);
return getPreferencesFor(identity);
}
}
}
@Override
public void updatePreferencesFor(Preferences prefs, Identity identity) {
String props = xstream.toXML(prefs);
Property property = getPreferencesProperty(identity);
if (property == null) {
property = PropertyManager.getInstance().createPropertyInstance(identity, null, null, null, DbStorage.USER_PROPERTY_KEY, null, null,
null, props);
// also save the properties to db, here (strentini)
// fixes the "non-present gui preferences" for new users, or where guiproperties were manually deleted
PropertyManager.getInstance().saveProperty(property);
} else {
property.setTextValue(props);
PropertyManager.getInstance().updateProperty(property);
}
}
/**
* search x-stream serialization in properties table, create new if not found
* @param identity
* @return
*/
private DbPrefs getPreferencesFor(final Identity identity) {
Property guiProperty = getPreferencesProperty(identity);
if (guiProperty == null) {
return createEmptyDbPrefs(identity,false);
} else {
return getPreferencesForProperty(identity, guiProperty);
}
}
private Property getPreferencesProperty(Identity identity) {
Property guiProperty = null;
try {
guiProperty = PropertyManager.getInstance().findProperty(identity, null, null, null, USER_PROPERTY_KEY);
} catch (Exception e) {
// OLAT-6429 detect and delete multiple prefs objects, keep the first one only
List