Skip to content

Commit

Permalink
[PS-2280] Retain app settings on logout (#2366)
Browse files Browse the repository at this point in the history
* [PS-2280] Retain app settings on logout

* adjustments
  • Loading branch information
mpbw2 authored Feb 15, 2023
1 parent 44e5682 commit e7d6783
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 178 deletions.
5 changes: 0 additions & 5 deletions src/Android/Services/DeviceActionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -502,11 +502,6 @@ public async Task OnAccountSwitchCompleteAsync()

public async Task SetScreenCaptureAllowedAsync()
{
if (CoreHelpers.ForceScreenCaptureEnabled())
{
return;
}

var activity = CrossCurrentActivity.Current?.Activity;
if (await _stateService.GetScreenCaptureAllowedAsync())
{
Expand Down
5 changes: 0 additions & 5 deletions src/App/Pages/Settings/SettingsPage/SettingsPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -806,11 +806,6 @@ private VaultTimeoutAction GetVaultTimeoutActionFromKey(string key)

public async Task SetScreenCaptureAllowedAsync()
{
if (CoreHelpers.ForceScreenCaptureEnabled())
{
return;
}

try
{
if (!_screenCaptureAllowed
Expand Down
12 changes: 6 additions & 6 deletions src/Core/Abstractions/IStateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public interface IStateService
Task SetInvalidUnlockAttemptsAsync(int? value, string userId = null);
Task<string> GetLastBuildAsync();
Task SetLastBuildAsync(string value);
Task<bool?> GetDisableFaviconAsync(string userId = null);
Task SetDisableFaviconAsync(bool? value, string userId = null);
Task<bool?> GetDisableFaviconAsync();
Task SetDisableFaviconAsync(bool? value);
Task<bool?> GetDisableAutoTotpCopyAsync(string userId = null);
Task SetDisableAutoTotpCopyAsync(bool? value, string userId = null);
Task<bool?> GetInlineAutofillEnabledAsync(string userId = null);
Expand Down Expand Up @@ -109,10 +109,10 @@ public interface IStateService
Task SetRememberedEmailAsync(string value);
Task<string> GetRememberedOrgIdentifierAsync();
Task SetRememberedOrgIdentifierAsync(string value);
Task<string> GetThemeAsync(string userId = null);
Task SetThemeAsync(string value, string userId = null);
Task<string> GetAutoDarkThemeAsync(string userId = null);
Task SetAutoDarkThemeAsync(string value, string userId = null);
Task<string> GetThemeAsync();
Task SetThemeAsync(string value);
Task<string> GetAutoDarkThemeAsync();
Task SetAutoDarkThemeAsync(string value);
Task<bool?> GetAddSitePromptShownAsync(string userId = null);
Task SetAddSitePromptShownAsync(bool? value, string userId = null);
Task<bool?> GetPushInitialPromptShownAsync();
Expand Down
10 changes: 7 additions & 3 deletions src/Core/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public static class Constants
{
public const int MaxAccounts = 5;
public const int VaultTimeoutDefault = 15;
public const string AndroidAppProtocol = "androidapp://";
public const string iOSAppProtocol = "iosapp://";
public const string DefaultUsernameGenerated = "-";
Expand All @@ -29,6 +30,9 @@ public static class Constants
public static string RememberedEmailKey = "rememberedEmail";
public static string RememberedOrgIdentifierKey = "rememberedOrgIdentifier";
public static string PasswordlessLoginNotificationKey = "passwordlessLoginNotificationKey";
public const string ThemeKey = "theme";
public const string AutoDarkThemeKey = "autoDarkTheme";
public const string DisableFaviconKey = "disableFavicon";
public const string PasswordlessNotificationId = "26072022";
public const string AndroidNotificationChannelId = "general_notification_channel";
public const string iOSNotificationCategoryId = "dismissableCategory";
Expand Down Expand Up @@ -65,6 +69,8 @@ public static class Constants
iOSShareExtensionClearCiphersCacheKey
};

public static string VaultTimeoutKey(string userId) => $"vaultTimeout_{userId}";
public static string VaultTimeoutActionKey(string userId) => $"vaultTimeoutAction_{userId}";
public static string CiphersKey(string userId) => $"ciphers_{userId}";
public static string FoldersKey(string userId) => $"folders_{userId}";
public static string CollectionsKey(string userId) => $"collections_{userId}";
Expand All @@ -89,10 +95,7 @@ public static class Constants
public static string AutofillBlacklistedUrisKey(string userId) => $"autofillBlacklistedUris_{userId}";
public static string ClearClipboardKey(string userId) => $"clearClipboard_{userId}";
public static string SyncOnRefreshKey(string userId) => $"syncOnRefresh_{userId}";
public static string DisableFaviconKey(string userId) => $"disableFavicon_{userId}";
public static string DefaultUriMatchKey(string userId) => $"defaultUriMatch_{userId}";
public static string ThemeKey(string userId) => $"theme_{userId}";
public static string AutoDarkThemeKey(string userId) => $"autoDarkTheme_{userId}";
public static string DisableAutoTotpCopyKey(string userId) => $"disableAutoTotpCopy_{userId}";
public static string PreviousPageKey(string userId) => $"previousPage_{userId}";
public static string PasswordRepromptAutofillKey(string userId) => $"passwordRepromptAutofillKey_{userId}";
Expand All @@ -107,5 +110,6 @@ public static class Constants
public static string PushLastRegistrationDateKey(string userId) => $"pushLastRegistrationDate_{userId}";
public static string PushCurrentTokenKey(string userId) => $"pushCurrentToken_{userId}";
public static string ShouldConnectToWatchKey(string userId) => $"shouldConnectToWatch_{userId}";
public static string ScreenCaptureAllowedKey(string userId) => $"screenCaptureAllowed_{userId}";
}
}
8 changes: 6 additions & 2 deletions src/Core/Models/Domain/Account.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bit.Core.Enums;
using System;
using Bit.Core.Enums;
using Bit.Core.Models.Data;

namespace Bit.Core.Models.Domain
Expand Down Expand Up @@ -104,9 +105,12 @@ public AccountSettings(AccountSettings copy)
}

public EnvironmentUrlData EnvironmentUrls;
[Obsolete("Feb 10 2023: VaultTimeout has been deprecated in favor of stored prefs to retain value after logout. It remains here to allow for migration during app upgrade.")]
public int? VaultTimeout;
[Obsolete("Feb 10 2023: VaultTimeoutAction has been deprecated in favor of stored prefs to retain value after logout. It remains here to allow for migration during app upgrade.")]
public VaultTimeoutAction? VaultTimeoutAction;
public bool ScreenCaptureAllowed;
[Obsolete("Feb 10 2023: ScreenCaptureAllowed has been deprecated in favor of stored prefs to retain value after logout. It remains here to allow for migration during app upgrade.")]
public bool? ScreenCaptureAllowed;
}

public class AccountVolatileData
Expand Down
90 changes: 87 additions & 3 deletions src/Core/Services/StateMigrationService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bit.Core.Abstractions;
using Bit.Core.Enums;
Expand All @@ -11,7 +12,7 @@ namespace Bit.Core.Services
{
public class StateMigrationService : IStateMigrationService
{
private const int StateVersion = 3;
private const int StateVersion = 4;

private readonly IStorageService _preferencesStorageService;
private readonly IStorageService _liteDbStorageService;
Expand Down Expand Up @@ -54,6 +55,9 @@ public async Task Migrate()
goto case 2;
case 2:
await MigrateFrom2To3Async();
goto case 3;
case 3:
await MigrateFrom3To4Async();
break;
}
}
Expand Down Expand Up @@ -220,9 +224,9 @@ await SetValueAsync(Storage.LiteDb, Constants.AutofillDisableSavePromptKey(userI
await GetValueAsync<List<string>>(Storage.LiteDb, V2Keys.AutofillBlacklistedUrisKey);
await SetValueAsync(Storage.LiteDb, Constants.AutofillBlacklistedUrisKey(userId), autofillBlacklistedUris);
var disableFavicon = await GetValueAsync<bool?>(Storage.Prefs, V2Keys.DisableFaviconKey);
await SetValueAsync(Storage.LiteDb, Constants.DisableFaviconKey(userId), disableFavicon);
await SetValueAsync(Storage.LiteDb, V3Keys.DisableFaviconKey(userId), disableFavicon);
var theme = await GetValueAsync<string>(Storage.Prefs, V2Keys.ThemeKey);
await SetValueAsync(Storage.LiteDb, Constants.ThemeKey(userId), theme);
await SetValueAsync(Storage.LiteDb, V3Keys.ThemeKey(userId), theme);
var clearClipboard = await GetValueAsync<int?>(Storage.Prefs, V2Keys.ClearClipboardKey);
await SetValueAsync(Storage.LiteDb, Constants.ClearClipboardKey(userId), clearClipboard);
var previousPage = await GetValueAsync<PreviousPageInfo>(Storage.LiteDb, V2Keys.PreviousPageKey);
Expand Down Expand Up @@ -314,6 +318,86 @@ await SetValueAsync(Storage.LiteDb, Constants.PasswordVerifiedAutofillKey(userId
await RemoveValueAsync(Storage.LiteDb, V2Keys.Keys_PassGenHistory);
}

// v3 to v4 Migration

private class V3Keys
{
internal static string ThemeKey(string userId) => $"theme_{userId}";
internal static string AutoDarkThemeKey(string userId) => $"autoDarkTheme_{userId}";
internal static string DisableFaviconKey(string userId) => $"disableFavicon_{userId}";
}

private async Task MigrateFrom3To4Async()
{
var state = await GetValueAsync<State>(Storage.LiteDb, Constants.StateKey);
if (state?.Accounts is null)
{
// Update stored version
await SetLastStateVersionAsync(4);
return;
}

string firstUserId = null;

// move values from state to standalone values in LiteDB
foreach (var account in state.Accounts.Where(a => a.Value?.Profile?.UserId != null))
{
var userId = account.Value.Profile.UserId;
if (firstUserId == null)
{
firstUserId = userId;
}
var vaultTimeout = account.Value.Settings?.VaultTimeout;
await SetValueAsync(Storage.LiteDb, V4Keys.VaultTimeoutKey(userId), vaultTimeout);

var vaultTimeoutAction = account.Value.Settings?.VaultTimeoutAction;
await SetValueAsync(Storage.LiteDb, V4Keys.VaultTimeoutActionKey(userId), vaultTimeoutAction);

var screenCaptureAllowed = account.Value.Settings?.ScreenCaptureAllowed;
await SetValueAsync(Storage.LiteDb, V4Keys.ScreenCaptureAllowedKey(userId), screenCaptureAllowed);
}

// use values from first userId to apply globals
if (firstUserId != null)
{
var theme = await GetValueAsync<int?>(Storage.LiteDb, V3Keys.ThemeKey(firstUserId));
await SetValueAsync(Storage.LiteDb, V4Keys.ThemeKey, theme);

var autoDarkTheme = await GetValueAsync<int?>(Storage.LiteDb, V3Keys.AutoDarkThemeKey(firstUserId));
await SetValueAsync(Storage.LiteDb, V4Keys.AutoDarkThemeKey, autoDarkTheme);

var disableFavicon = await GetValueAsync<bool?>(Storage.LiteDb, V3Keys.DisableFaviconKey(firstUserId));
await SetValueAsync(Storage.LiteDb, V4Keys.DisableFaviconKey, disableFavicon);
}

// Update stored version
await SetLastStateVersionAsync(4);

// Remove old data
foreach (var account in state.Accounts)
{
var userId = account.Value?.Profile?.UserId;
if (userId != null)
{
await RemoveValueAsync(Storage.LiteDb, V3Keys.ThemeKey(userId));
await RemoveValueAsync(Storage.LiteDb, V3Keys.AutoDarkThemeKey(userId));
await RemoveValueAsync(Storage.LiteDb, V3Keys.DisableFaviconKey(userId));
}
}

// Removal of old state data will happen organically as state is rebuilt in app
}

private class V4Keys
{
internal static string VaultTimeoutKey(string userId) => $"vaultTimeout_{userId}";
internal static string VaultTimeoutActionKey(string userId) => $"vaultTimeoutAction_{userId}";
internal static string ScreenCaptureAllowedKey(string userId) => $"screenCaptureAllowed_{userId}";
internal const string ThemeKey = "theme";
internal const string AutoDarkThemeKey = "autoDarkTheme";
internal const string DisableFaviconKey = "disableFavicon";
}

// Helpers

private async Task<int> GetLastStateVersionAsync()
Expand Down
Loading

0 comments on commit e7d6783

Please sign in to comment.