2022-07-25 15:13:54 +02:00
|
|
|
import { Inject, Injectable } from "@angular/core";
|
|
|
|
|
|
|
|
import {
|
|
|
|
MEMORY_STORAGE,
|
|
|
|
SECURE_STORAGE,
|
|
|
|
STATE_FACTORY,
|
|
|
|
STATE_SERVICE_USE_CACHE,
|
|
|
|
} from "@bitwarden/angular/services/jslib-services.module";
|
|
|
|
import { LogService } from "@bitwarden/common/abstractions/log.service";
|
|
|
|
import { StateMigrationService } from "@bitwarden/common/abstractions/stateMigration.service";
|
|
|
|
import { AbstractStorageService } from "@bitwarden/common/abstractions/storage.service";
|
|
|
|
import { StateFactory } from "@bitwarden/common/factories/stateFactory";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { CipherData } from "@bitwarden/common/models/data/cipherData";
|
|
|
|
import { CollectionData } from "@bitwarden/common/models/data/collectionData";
|
|
|
|
import { FolderData } from "@bitwarden/common/models/data/folderData";
|
|
|
|
import { SendData } from "@bitwarden/common/models/data/sendData";
|
|
|
|
import { StorageOptions } from "@bitwarden/common/models/domain/storageOptions";
|
|
|
|
import { StateService as BaseStateService } from "@bitwarden/common/services/state.service";
|
2022-02-24 12:10:07 +01:00
|
|
|
|
|
|
|
import { Account } from "../models/account";
|
|
|
|
import { GlobalState } from "../models/globalState";
|
2022-02-11 09:21:51 +01:00
|
|
|
|
2022-07-25 15:13:54 +02:00
|
|
|
@Injectable()
|
|
|
|
export class StateService extends BaseStateService<GlobalState, Account> {
|
|
|
|
constructor(
|
|
|
|
storageService: AbstractStorageService,
|
|
|
|
@Inject(SECURE_STORAGE) secureStorageService: AbstractStorageService,
|
|
|
|
@Inject(MEMORY_STORAGE) memoryStorageService: AbstractStorageService,
|
|
|
|
logService: LogService,
|
|
|
|
stateMigrationService: StateMigrationService,
|
|
|
|
@Inject(STATE_FACTORY) stateFactory: StateFactory<GlobalState, Account>,
|
|
|
|
@Inject(STATE_SERVICE_USE_CACHE) useAccountCache = true
|
|
|
|
) {
|
|
|
|
super(
|
|
|
|
storageService,
|
|
|
|
secureStorageService,
|
|
|
|
memoryStorageService,
|
|
|
|
logService,
|
|
|
|
stateMigrationService,
|
|
|
|
stateFactory,
|
|
|
|
useAccountCache
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-04 17:15:58 +01:00
|
|
|
async addAccount(account: Account) {
|
|
|
|
// Apply web overides to default account values
|
|
|
|
account = new Account(account);
|
|
|
|
await super.addAccount(account);
|
|
|
|
}
|
2022-02-02 17:20:15 +01:00
|
|
|
|
|
|
|
async getRememberEmail(options?: StorageOptions) {
|
|
|
|
return (
|
|
|
|
await this.getGlobals(this.reconcileOptions(options, await this.defaultOnDiskLocalOptions()))
|
|
|
|
)?.rememberEmail;
|
|
|
|
}
|
|
|
|
|
|
|
|
async setRememberEmail(value: boolean, options?: StorageOptions): Promise<void> {
|
|
|
|
const globals = await this.getGlobals(
|
|
|
|
this.reconcileOptions(options, await this.defaultOnDiskLocalOptions())
|
|
|
|
);
|
|
|
|
globals.rememberEmail = value;
|
|
|
|
await this.saveGlobals(
|
|
|
|
globals,
|
|
|
|
this.reconcileOptions(options, await this.defaultOnDiskLocalOptions())
|
|
|
|
);
|
|
|
|
}
|
2022-02-11 09:21:51 +01:00
|
|
|
|
|
|
|
async getEncryptedCiphers(options?: StorageOptions): Promise<{ [id: string]: CipherData }> {
|
2022-06-27 19:38:12 +02:00
|
|
|
options = this.reconcileOptions(options, await this.defaultInMemoryOptions());
|
2022-02-11 09:21:51 +01:00
|
|
|
return await super.getEncryptedCiphers(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
async setEncryptedCiphers(
|
|
|
|
value: { [id: string]: CipherData },
|
|
|
|
options?: StorageOptions
|
|
|
|
): Promise<void> {
|
2022-06-27 19:38:12 +02:00
|
|
|
options = this.reconcileOptions(options, await this.defaultInMemoryOptions());
|
2022-02-11 09:21:51 +01:00
|
|
|
return await super.setEncryptedCiphers(value, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getEncryptedCollections(
|
|
|
|
options?: StorageOptions
|
|
|
|
): Promise<{ [id: string]: CollectionData }> {
|
2022-06-27 19:38:12 +02:00
|
|
|
options = this.reconcileOptions(options, await this.defaultInMemoryOptions());
|
2022-02-11 09:21:51 +01:00
|
|
|
return await super.getEncryptedCollections(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
async setEncryptedCollections(
|
|
|
|
value: { [id: string]: CollectionData },
|
|
|
|
options?: StorageOptions
|
|
|
|
): Promise<void> {
|
2022-06-27 19:38:12 +02:00
|
|
|
options = this.reconcileOptions(options, await this.defaultInMemoryOptions());
|
2022-02-11 09:21:51 +01:00
|
|
|
return await super.setEncryptedCollections(value, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getEncryptedFolders(options?: StorageOptions): Promise<{ [id: string]: FolderData }> {
|
2022-06-27 19:38:12 +02:00
|
|
|
options = this.reconcileOptions(options, await this.defaultInMemoryOptions());
|
2022-02-11 09:21:51 +01:00
|
|
|
return await super.getEncryptedFolders(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
async setEncryptedFolders(
|
|
|
|
value: { [id: string]: FolderData },
|
|
|
|
options?: StorageOptions
|
|
|
|
): Promise<void> {
|
2022-06-27 19:38:12 +02:00
|
|
|
options = this.reconcileOptions(options, await this.defaultInMemoryOptions());
|
2022-02-11 09:21:51 +01:00
|
|
|
return await super.setEncryptedFolders(value, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getEncryptedSends(options?: StorageOptions): Promise<{ [id: string]: SendData }> {
|
2022-06-27 19:38:12 +02:00
|
|
|
options = this.reconcileOptions(options, await this.defaultInMemoryOptions());
|
2022-02-11 09:21:51 +01:00
|
|
|
return await super.getEncryptedSends(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
async setEncryptedSends(
|
|
|
|
value: { [id: string]: SendData },
|
|
|
|
options?: StorageOptions
|
|
|
|
): Promise<void> {
|
2022-06-27 19:38:12 +02:00
|
|
|
options = this.reconcileOptions(options, await this.defaultInMemoryOptions());
|
2022-02-11 09:21:51 +01:00
|
|
|
return await super.setEncryptedSends(value, options);
|
|
|
|
}
|
2022-02-11 09:39:48 +01:00
|
|
|
|
|
|
|
override async getLastSync(options?: StorageOptions): Promise<string> {
|
2022-06-27 19:38:12 +02:00
|
|
|
options = this.reconcileOptions(options, await this.defaultInMemoryOptions());
|
2022-02-11 09:39:48 +01:00
|
|
|
return await super.getLastSync(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
override async setLastSync(value: string, options?: StorageOptions): Promise<void> {
|
2022-06-27 19:38:12 +02:00
|
|
|
options = this.reconcileOptions(options, await this.defaultInMemoryOptions());
|
2022-02-11 09:39:48 +01:00
|
|
|
return await super.setLastSync(value, options);
|
|
|
|
}
|
2022-01-04 17:15:58 +01:00
|
|
|
}
|