mirror of
https://github.com/moparisthebest/k-9
synced 2025-02-17 07:30:16 -05:00
Added first version of input validation for account settings
This commit is contained in:
parent
42987cee51
commit
a57e605496
@ -1,7 +1,9 @@
|
|||||||
package com.fsck.k9.preferences;
|
package com.fsck.k9.preferences;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import com.fsck.k9.Account;
|
import com.fsck.k9.Account;
|
||||||
@ -163,6 +165,19 @@ public class AccountSettings {
|
|||||||
return Settings.validate(SETTINGS, importedSettings, useDefaultValues);
|
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 {
|
public static class StorageProviderDefaultValue implements IDefaultValue {
|
||||||
@Override
|
@Override
|
||||||
public Object computeDefaultValue(String key, Map<String, String> validatedSettings) {
|
public Object computeDefaultValue(String key, Map<String, String> validatedSettings) {
|
||||||
|
@ -70,7 +70,7 @@ public class StorageImporter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class AccountDescriptionPair {
|
private static class AccountDescriptionPair {
|
||||||
public final AccountDescription original;
|
public final AccountDescription original;
|
||||||
public final AccountDescription imported;
|
public final AccountDescription imported;
|
||||||
|
|
||||||
@ -275,26 +275,37 @@ public class StorageImporter {
|
|||||||
|
|
||||||
AccountDescription original = new AccountDescription(account.name, account.uuid);
|
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);
|
Preferences prefs = Preferences.getPreferences(context);
|
||||||
Account[] accounts = prefs.getAccounts();
|
Account[] accounts = prefs.getAccounts();
|
||||||
|
|
||||||
String uuid = account.uuid;
|
String uuid = account.uuid;
|
||||||
Account existingAccount = prefs.getAccount(uuid);
|
Account existingAccount = prefs.getAccount(uuid);
|
||||||
|
boolean mergeImportedAccount = (overwrite && existingAccount != null);
|
||||||
|
|
||||||
if (!overwrite && existingAccount != null) {
|
if (!overwrite && existingAccount != null) {
|
||||||
// An account with this UUID already exists, but we're not allowed to overwrite it.
|
// An account with this UUID already exists, but we're not allowed to overwrite it.
|
||||||
// So generate a new UUID.
|
// So generate a new UUID.
|
||||||
uuid = UUID.randomUUID().toString();
|
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;
|
String accountName = account.name;
|
||||||
if (isAccountNameUsed(accountName, accounts)) {
|
if (isAccountNameUsed(accountName, accounts)) {
|
||||||
// Account name is already in use. So generate a new one by appending " (x)", where x
|
// 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);
|
editor.putString(accountKeyPrefix + Account.ACCOUNT_DESCRIPTION_KEY, accountName);
|
||||||
|
|
||||||
// Write account settings
|
// 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 key = accountKeyPrefix + setting.getKey();
|
||||||
String value = setting.getValue();
|
String value = setting.getValue();
|
||||||
editor.putString(key, value);
|
editor.putString(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If it's a new account generate and write a new "accountNumber"
|
// 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);
|
int newAccountNumber = Account.generateAccountNumber(prefs);
|
||||||
editor.putString(accountKeyPrefix + "accountNumber", Integer.toString(newAccountNumber));
|
editor.putString(accountKeyPrefix + "accountNumber", Integer.toString(newAccountNumber));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user