1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-20 02:03:39 +00:00

Include missing migration (#7840)

This migration missing from #7825 does not suggest missing data since no client has been released in the interim.
This commit is contained in:
Matt Gibson
2024-02-14 14:25:08 -05:00
committed by GitHub
parent e5d4d4ad00
commit 7a6d7b3a68
3 changed files with 183 additions and 2 deletions

View File

@@ -0,0 +1,56 @@
import { KeyDefinitionLike, MigrationHelper } from "../migration-helper";
import { Migrator } from "../migrator";
type ExpectedAccountType = {
settings?: {
requirePasswordOnStart?: boolean;
};
};
// Biometric text, no auto prompt text, fingerprint validated, and prompt cancelled are refreshed on every app start, so we don't need to migrate them
export const REQUIRE_PASSWORD_ON_START: KeyDefinitionLike = {
key: "requirePasswordOnStart",
stateDefinition: { name: "biometricSettings" },
};
export class RequirePasswordOnStartMigrator extends Migrator<18, 19> {
async migrate(helper: MigrationHelper): Promise<void> {
const legacyAccounts = await helper.getAccounts<ExpectedAccountType>();
await Promise.all(
legacyAccounts.map(async ({ userId, account }) => {
// Move account data
if (account?.settings?.requirePasswordOnStart != null) {
await helper.setToUser(
userId,
REQUIRE_PASSWORD_ON_START,
account.settings.requirePasswordOnStart,
);
// Delete old account data
delete account.settings.requirePasswordOnStart;
await helper.set(userId, account);
}
}),
);
}
async rollback(helper: MigrationHelper): Promise<void> {
async function rollbackUser(userId: string, account: ExpectedAccountType) {
const requirePassword = await helper.getFromUser<boolean>(userId, REQUIRE_PASSWORD_ON_START);
if (requirePassword) {
account ??= {};
account.settings ??= {};
account.settings.requirePasswordOnStart = requirePassword;
await helper.setToUser(userId, REQUIRE_PASSWORD_ON_START, null);
await helper.set(userId, account);
}
}
const accounts = await helper.getAccounts<ExpectedAccountType>();
await Promise.all(accounts.map(({ userId, account }) => rollbackUser(userId, account)));
}
}