1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-23 18:02:15 -05:00

Added first version of input validation for account settings

This commit is contained in:
cketti 2011-06-06 19:44:01 +02:00
parent 42987cee51
commit a57e605496
2 changed files with 38 additions and 12 deletions

View File

@ -1,7 +1,9 @@
package com.fsck.k9.preferences;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import android.content.SharedPreferences;
import android.net.Uri;
import android.util.Log;
import com.fsck.k9.Account;
@ -163,6 +165,19 @@ public class AccountSettings {
return Settings.validate(SETTINGS, importedSettings, useDefaultValues);
}
public static Map<String, String> getAccountSettings(SharedPreferences storage, String uuid) {
Map<String, String> result = new HashMap<String, String>();
String prefix = uuid + ".";
for (String key : SETTINGS.keySet()) {
String value = storage.getString(prefix + key, null);
if (value != null) {
result.put(key, value);
}
}
return result;
}
public static class StorageProviderDefaultValue implements IDefaultValue {
@Override
public Object computeDefaultValue(String key, Map<String, String> validatedSettings) {

View File

@ -70,7 +70,7 @@ public class StorageImporter {
}
}
public static class AccountDescriptionPair {
private static class AccountDescriptionPair {
public final AccountDescription original;
public final AccountDescription imported;
@ -275,26 +275,37 @@ public class StorageImporter {
AccountDescription original = new AccountDescription(account.name, account.uuid);
// Validate input and ignore malformed values when possible
Map<String, String> validatedSettings =
AccountSettings.validate(account.settings.settings, true);
//TODO: validate account name
//TODO: validate identity settings
//TODO: validate folder settings
Preferences prefs = Preferences.getPreferences(context);
Account[] accounts = prefs.getAccounts();
String uuid = account.uuid;
Account existingAccount = prefs.getAccount(uuid);
boolean mergeImportedAccount = (overwrite && existingAccount != null);
if (!overwrite && existingAccount != null) {
// An account with this UUID already exists, but we're not allowed to overwrite it.
// So generate a new UUID.
uuid = UUID.randomUUID().toString();
}
Map<String, String> validatedSettings =
AccountSettings.validate(account.settings.settings, !mergeImportedAccount);
Map<String, String> writeSettings;
if (mergeImportedAccount) {
writeSettings = new HashMap<String, String>(
AccountSettings.getAccountSettings(prefs.getPreferences(), uuid));
writeSettings.putAll(validatedSettings);
} else {
writeSettings = new HashMap<String, String>(validatedSettings);
}
//TODO: validate account name
//TODO: validate identity settings
//TODO: validate folder settings
String accountName = account.name;
if (isAccountNameUsed(accountName, accounts)) {
// Account name is already in use. So generate a new one by appending " (x)", where x
@ -311,14 +322,14 @@ public class StorageImporter {
editor.putString(accountKeyPrefix + Account.ACCOUNT_DESCRIPTION_KEY, accountName);
// Write account settings
for (Map.Entry<String, String> setting : validatedSettings.entrySet()) {
for (Map.Entry<String, String> setting : writeSettings.entrySet()) {
String key = accountKeyPrefix + setting.getKey();
String value = setting.getValue();
editor.putString(key, value);
}
// If it's a new account generate and write a new "accountNumber"
if (existingAccount == null || !uuid.equals(account.uuid)) {
if (!mergeImportedAccount) {
int newAccountNumber = Account.generateAccountNumber(prefs);
editor.putString(accountKeyPrefix + "accountNumber", Integer.toString(newAccountNumber));
}