1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-09-28 04:08:47 +02:00
bitwarden-browser/libs/common/src/state-migrations/migrations/43-move-auto-confirm-finger-prints-to-state-provider.ts
Addison Beck bf2d2cfbed
Migrate autoConfirmFingerPrints to StateProvider (#8337)
* Fix a typo in the `StateDefinition` description

* Introduce `OrganizationManagementPreferencesService`

* Declare `OrganizationManagementPreferencesService` in DI

* Update `autoConfirmFingerPrints` logic in emergency access files

* Update `autoConfirmFingerPrints` logic in `people` files

* Remove `autoConfirmFingerPrints` from `StateService` and `Account`

* Migrate existing client data for `autoConfirmFingerPrints`

* Update apps/web/src/app/admin-console/organizations/manage/user-confirm.component.ts

Co-authored-by: Matt Gibson <mgibson@bitwarden.com>

* Update apps/web/src/app/admin-console/organizations/manage/user-confirm.component.ts

Co-authored-by: Matt Gibson <mgibson@bitwarden.com>

* Use `set` instead of `update` for function names

---------

Co-authored-by: Matt Gibson <mgibson@bitwarden.com>
2024-03-19 17:37:35 -04:00

64 lines
1.9 KiB
TypeScript

import { KeyDefinitionLike, MigrationHelper, StateDefinitionLike } from "../migration-helper";
import { Migrator } from "../migrator";
type ExpectedAccountState = {
settings?: { autoConfirmFingerPrints?: boolean };
};
const ORGANIZATION_MANAGEMENT_PREFERENCES: StateDefinitionLike = {
name: "organizationManagementPreferences",
};
const AUTO_CONFIRM_FINGERPRINTS: KeyDefinitionLike = {
key: "autoConfirmFingerPrints",
stateDefinition: ORGANIZATION_MANAGEMENT_PREFERENCES,
};
export class AutoConfirmFingerPrintsMigrator extends Migrator<42, 43> {
async migrate(helper: MigrationHelper): Promise<void> {
const legacyAccounts = await helper.getAccounts<ExpectedAccountState>();
await Promise.all(
legacyAccounts.map(async ({ userId, account }) => {
if (account?.settings?.autoConfirmFingerPrints != null) {
await helper.setToUser(
userId,
AUTO_CONFIRM_FINGERPRINTS,
account.settings.autoConfirmFingerPrints,
);
delete account?.settings?.autoConfirmFingerPrints;
await helper.set(userId, account);
}
}),
);
}
async rollback(helper: MigrationHelper): Promise<void> {
async function rollbackUser(userId: string, account: ExpectedAccountState) {
let updatedAccount = false;
const autoConfirmFingerPrints = await helper.getFromUser<boolean>(
userId,
AUTO_CONFIRM_FINGERPRINTS,
);
if (autoConfirmFingerPrints) {
if (!account) {
account = {};
}
updatedAccount = true;
account.settings.autoConfirmFingerPrints = autoConfirmFingerPrints;
await helper.setToUser(userId, AUTO_CONFIRM_FINGERPRINTS, null);
}
if (updatedAccount) {
await helper.set(userId, account);
}
}
const accounts = await helper.getAccounts<ExpectedAccountState>();
await Promise.all(accounts.map(({ userId, account }) => rollbackUser(userId, account)));
}
}