mirror of
https://github.com/bitwarden/browser
synced 2025-12-18 17:23:37 +00:00
[PM-5269] Key Connector state migration (#8327)
* key connector migration initial * migrator complete * fix dependencies * finalized tests * fix deps and sync main * clean up definition file * fixing tests * fixed tests * fixing CLI, Browser, Desktop builds * fixed factory options * reverting exports * implemented UserKeyDefinition clearOn * Update KeyConnector MIgration * updated migrator and tests to match profile object * removed unused service and updated clear * dep fix * dep fixes * clear usesKeyConnector on logout
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
import { KeyDefinitionLike, MigrationHelper } from "../migration-helper";
|
||||
import { Migrator } from "../migrator";
|
||||
|
||||
type ExpectedAccountType = {
|
||||
profile?: {
|
||||
usesKeyConnector?: boolean;
|
||||
convertAccountToKeyConnector?: boolean;
|
||||
};
|
||||
};
|
||||
|
||||
const usesKeyConnectorKeyDefinition: KeyDefinitionLike = {
|
||||
key: "usesKeyConnector",
|
||||
stateDefinition: {
|
||||
name: "keyConnector",
|
||||
},
|
||||
};
|
||||
|
||||
const convertAccountToKeyConnectorKeyDefinition: KeyDefinitionLike = {
|
||||
key: "convertAccountToKeyConnector",
|
||||
stateDefinition: {
|
||||
name: "keyConnector",
|
||||
},
|
||||
};
|
||||
|
||||
export class KeyConnectorMigrator extends Migrator<49, 50> {
|
||||
async migrate(helper: MigrationHelper): Promise<void> {
|
||||
const accounts = await helper.getAccounts<ExpectedAccountType>();
|
||||
async function migrateAccount(userId: string, account: ExpectedAccountType): Promise<void> {
|
||||
const usesKeyConnector = account?.profile?.usesKeyConnector;
|
||||
const convertAccountToKeyConnector = account?.profile?.convertAccountToKeyConnector;
|
||||
if (usesKeyConnector == null && convertAccountToKeyConnector == null) {
|
||||
return;
|
||||
}
|
||||
if (usesKeyConnector != null) {
|
||||
await helper.setToUser(userId, usesKeyConnectorKeyDefinition, usesKeyConnector);
|
||||
delete account.profile.usesKeyConnector;
|
||||
}
|
||||
if (convertAccountToKeyConnector != null) {
|
||||
await helper.setToUser(
|
||||
userId,
|
||||
convertAccountToKeyConnectorKeyDefinition,
|
||||
convertAccountToKeyConnector,
|
||||
);
|
||||
delete account.profile.convertAccountToKeyConnector;
|
||||
}
|
||||
await helper.set(userId, account);
|
||||
}
|
||||
await Promise.all([...accounts.map(({ userId, account }) => migrateAccount(userId, account))]);
|
||||
}
|
||||
|
||||
async rollback(helper: MigrationHelper): Promise<void> {
|
||||
const accounts = await helper.getAccounts<ExpectedAccountType>();
|
||||
async function rollbackAccount(userId: string, account: ExpectedAccountType): Promise<void> {
|
||||
const usesKeyConnector: boolean = await helper.getFromUser(
|
||||
userId,
|
||||
usesKeyConnectorKeyDefinition,
|
||||
);
|
||||
const convertAccountToKeyConnector: boolean = await helper.getFromUser(
|
||||
userId,
|
||||
convertAccountToKeyConnectorKeyDefinition,
|
||||
);
|
||||
if (usesKeyConnector == null && convertAccountToKeyConnector == null) {
|
||||
return;
|
||||
}
|
||||
if (usesKeyConnector != null) {
|
||||
account.profile.usesKeyConnector = usesKeyConnector;
|
||||
await helper.setToUser(userId, usesKeyConnectorKeyDefinition, null);
|
||||
}
|
||||
if (convertAccountToKeyConnector != null) {
|
||||
account.profile.convertAccountToKeyConnector = convertAccountToKeyConnector;
|
||||
await helper.setToUser(userId, convertAccountToKeyConnectorKeyDefinition, null);
|
||||
}
|
||||
await helper.set(userId, account);
|
||||
}
|
||||
|
||||
await Promise.all([...accounts.map(({ userId, account }) => rollbackAccount(userId, account))]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user