2018-06-25 22:44:06 +02:00
|
|
|
import { Component, OnInit } from "@angular/core";
|
2023-04-15 01:11:33 +02:00
|
|
|
import { FormBuilder } from "@angular/forms";
|
|
|
|
import { concatMap, filter, map, Observable, Subject, takeUntil, tap } from "rxjs";
|
2018-06-25 22:44:06 +02:00
|
|
|
|
2022-06-23 13:36:05 +02:00
|
|
|
import { AbstractThemingService } from "@bitwarden/angular/services/theming/theming.service.abstraction";
|
2023-04-28 21:07:26 +02:00
|
|
|
import { SettingsService } from "@bitwarden/common/abstractions/settings.service";
|
2022-08-30 22:30:43 +02:00
|
|
|
import { VaultTimeoutSettingsService } from "@bitwarden/common/abstractions/vaultTimeout/vaultTimeoutSettings.service";
|
2023-04-15 01:11:33 +02:00
|
|
|
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
|
|
|
|
import { PolicyType } from "@bitwarden/common/admin-console/enums";
|
[AC-1266] Enums filename conventions (#5140)
* refactor: update clientType enum
* refactor: update deviceType filename
* refactor: update encryptedExportType filename
* refactor: update encryptionType filename
* refactor: update eventType filename
* refactor: update fieldType filename
* refactor: update fileUploadType filename
* refactor: update hashPurpose filename
* refactor: update htmlStorageLocation filename
* refactor: update kdfType filename
* refactor: update keySuffixOptions filename
* refactor: update linkedIdType filename
* refactor: update logLevelType filename
* refactor: update nativeMessagingVersion filename
* refactor: update notificationType filename
* refactor: update productType filename
* refactor: update secureNoteType filename
* refactor: update stateVersion filename
* refactor: update storageLocation filename
* refactor: update themeType filename
* refactor: update uriMatchType filename
* fix: update kdfType classes missed in initial pass, refs AC-1266
* fix: missing import update for device-type
* refactor: add barrel file for enums and update pathed import statements, refs AC-1266
* fix: incorrect import statements for web, refs AC-1266
* fix: missed import statement updates (browser), refs AC-1266
* fix: missed import statement changes (cli), refs AC-1266
* fix: missed import statement changes (desktop), refs AC-1266
* fix: prettier, refs AC-1266
* refactor: (libs) update relative paths to use barrel file, refs AC-1266
* fix: missed find/replace import statements for SecureNoteType, refs AC-1266
* refactor: apply .enum suffix to enums folder and modify leftover relative paths, refs AC-1266
* fix: find/replace errors for native-messaging-version, refs AC-1266
2023-04-05 05:42:21 +02:00
|
|
|
import { ThemeType } from "@bitwarden/common/enums";
|
2023-04-15 01:11:33 +02:00
|
|
|
import { VaultTimeoutAction } from "@bitwarden/common/enums/vault-timeout-action.enum";
|
2023-06-06 22:34:53 +02:00
|
|
|
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
|
|
|
import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service";
|
|
|
|
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
|
|
|
|
import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
|
|
|
|
import { Utils } from "@bitwarden/common/platform/misc/utils";
|
2023-08-16 14:26:56 +02:00
|
|
|
import { DialogService } from "@bitwarden/components";
|
2018-07-24 04:45:43 +02:00
|
|
|
|
2018-06-25 22:44:06 +02:00
|
|
|
@Component({
|
2022-05-09 14:21:52 +02:00
|
|
|
selector: "app-preferences",
|
|
|
|
templateUrl: "preferences.component.html",
|
2018-06-25 22:44:06 +02:00
|
|
|
})
|
2022-05-09 14:21:52 +02:00
|
|
|
export class PreferencesComponent implements OnInit {
|
2023-04-15 01:11:33 +02:00
|
|
|
// For use in template
|
|
|
|
protected readonly VaultTimeoutAction = VaultTimeoutAction;
|
|
|
|
|
|
|
|
vaultTimeoutPolicyCallout: Observable<{
|
|
|
|
timeout: { hours: number; minutes: number };
|
|
|
|
action: VaultTimeoutAction;
|
|
|
|
}>;
|
|
|
|
vaultTimeoutOptions: { name: string; value: number }[];
|
2018-06-25 22:44:06 +02:00
|
|
|
localeOptions: any[];
|
2021-09-30 00:06:20 +02:00
|
|
|
themeOptions: any[];
|
2018-06-25 22:44:06 +02:00
|
|
|
|
2018-06-26 15:04:12 +02:00
|
|
|
private startingLocale: string;
|
2022-01-28 17:30:45 +01:00
|
|
|
private startingTheme: ThemeType;
|
2023-04-15 01:11:33 +02:00
|
|
|
private destroy$ = new Subject<void>();
|
|
|
|
|
|
|
|
form = this.formBuilder.group({
|
|
|
|
vaultTimeout: [null as number | null],
|
|
|
|
vaultTimeoutAction: [VaultTimeoutAction.Lock],
|
|
|
|
enableFavicons: true,
|
|
|
|
enableFullWidth: false,
|
|
|
|
theme: [ThemeType.Light],
|
|
|
|
locale: [null as string | null],
|
|
|
|
});
|
2018-06-26 15:04:12 +02:00
|
|
|
|
2021-12-14 17:10:26 +01:00
|
|
|
constructor(
|
2023-04-15 01:11:33 +02:00
|
|
|
private formBuilder: FormBuilder,
|
|
|
|
private policyService: PolicyService,
|
2021-12-14 17:10:26 +01:00
|
|
|
private stateService: StateService,
|
2021-12-07 20:41:45 +01:00
|
|
|
private i18nService: I18nService,
|
2022-08-30 22:30:43 +02:00
|
|
|
private vaultTimeoutSettingsService: VaultTimeoutSettingsService,
|
2021-12-14 17:10:26 +01:00
|
|
|
private platformUtilsService: PlatformUtilsService,
|
2022-06-23 13:36:05 +02:00
|
|
|
private messagingService: MessagingService,
|
2023-04-28 21:07:26 +02:00
|
|
|
private themingService: AbstractThemingService,
|
2023-05-02 18:46:03 +02:00
|
|
|
private settingsService: SettingsService,
|
2023-08-16 14:26:56 +02:00
|
|
|
private dialogService: DialogService
|
2021-12-14 17:10:26 +01:00
|
|
|
) {
|
2023-04-15 01:11:33 +02:00
|
|
|
this.vaultTimeoutOptions = [
|
2018-07-27 21:08:59 +02:00
|
|
|
{ name: i18nService.t("oneMinute"), value: 1 },
|
|
|
|
{ name: i18nService.t("fiveMinutes"), value: 5 },
|
|
|
|
{ name: i18nService.t("fifteenMinutes"), value: 15 },
|
|
|
|
{ name: i18nService.t("thirtyMinutes"), value: 30 },
|
2020-03-30 16:59:47 +02:00
|
|
|
{ name: i18nService.t("oneHour"), value: 60 },
|
2019-09-06 15:33:35 +02:00
|
|
|
{ name: i18nService.t("fourHours"), value: 240 },
|
|
|
|
{ name: i18nService.t("onRefresh"), value: -1 },
|
2018-07-24 04:45:43 +02:00
|
|
|
];
|
|
|
|
if (this.platformUtilsService.isDev()) {
|
2023-04-15 01:11:33 +02:00
|
|
|
this.vaultTimeoutOptions.push({ name: i18nService.t("never"), value: null });
|
2018-06-25 22:44:06 +02:00
|
|
|
}
|
|
|
|
|
2021-12-14 17:10:26 +01:00
|
|
|
const localeOptions: any[] = [];
|
|
|
|
i18nService.supportedTranslationLocales.forEach((locale) => {
|
|
|
|
let name = locale;
|
|
|
|
if (i18nService.localeNames.has(locale)) {
|
|
|
|
name += " - " + i18nService.localeNames.get(locale);
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2021-12-14 17:10:26 +01:00
|
|
|
localeOptions.push({ name: name, value: locale });
|
2021-12-17 15:57:11 +01:00
|
|
|
});
|
2021-12-14 17:10:26 +01:00
|
|
|
localeOptions.sort(Utils.getSortFunction(i18nService, "name"));
|
|
|
|
localeOptions.splice(0, 0, { name: i18nService.t("default"), value: null });
|
|
|
|
this.localeOptions = localeOptions;
|
|
|
|
this.themeOptions = [
|
|
|
|
{ name: i18nService.t("themeLight"), value: ThemeType.Light },
|
|
|
|
{ name: i18nService.t("themeDark"), value: ThemeType.Dark },
|
|
|
|
{ name: i18nService.t("themeSystem"), value: ThemeType.System },
|
2021-12-17 15:57:11 +01:00
|
|
|
];
|
2018-06-25 22:44:06 +02:00
|
|
|
}
|
|
|
|
|
2018-06-26 15:04:12 +02:00
|
|
|
async ngOnInit() {
|
2023-04-15 01:11:33 +02:00
|
|
|
this.vaultTimeoutPolicyCallout = this.policyService.get$(PolicyType.MaximumVaultTimeout).pipe(
|
|
|
|
filter((policy) => policy != null),
|
|
|
|
map((policy) => {
|
|
|
|
let timeout;
|
|
|
|
if (policy.data?.minutes) {
|
|
|
|
timeout = {
|
|
|
|
hours: Math.floor(policy.data?.minutes / 60),
|
|
|
|
minutes: policy.data?.minutes % 60,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return { timeout: timeout, action: policy.data?.action };
|
|
|
|
}),
|
|
|
|
tap((policy) => {
|
|
|
|
if (policy.action) {
|
|
|
|
this.form.controls.vaultTimeoutAction.disable({ emitEvent: false });
|
|
|
|
} else {
|
|
|
|
this.form.controls.vaultTimeoutAction.enable({ emitEvent: false });
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2022-01-31 15:11:25 +01:00
|
|
|
|
2023-04-15 01:11:33 +02:00
|
|
|
this.form.controls.vaultTimeoutAction.valueChanges
|
|
|
|
.pipe(
|
|
|
|
concatMap(async (action) => {
|
|
|
|
if (action === VaultTimeoutAction.LogOut) {
|
2023-05-02 18:46:03 +02:00
|
|
|
const confirmed = await this.dialogService.openSimpleDialog({
|
|
|
|
title: { key: "vaultTimeoutLogOutConfirmationTitle" },
|
|
|
|
content: { key: "vaultTimeoutLogOutConfirmation" },
|
2023-08-16 14:26:56 +02:00
|
|
|
type: "warning",
|
2023-05-02 18:46:03 +02:00
|
|
|
});
|
|
|
|
|
2023-04-15 01:11:33 +02:00
|
|
|
if (!confirmed) {
|
|
|
|
this.form.controls.vaultTimeoutAction.patchValue(VaultTimeoutAction.Lock, {
|
|
|
|
emitEvent: false,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
takeUntil(this.destroy$)
|
|
|
|
)
|
|
|
|
.subscribe();
|
|
|
|
const initialFormValues = {
|
|
|
|
vaultTimeout: await this.vaultTimeoutSettingsService.getVaultTimeout(),
|
|
|
|
vaultTimeoutAction: await this.vaultTimeoutSettingsService.getVaultTimeoutAction(),
|
2023-04-28 21:07:26 +02:00
|
|
|
enableFavicons: !(await this.settingsService.getDisableFavicon()),
|
2023-04-15 01:11:33 +02:00
|
|
|
enableFullWidth: await this.stateService.getEnableFullWidth(),
|
|
|
|
theme: await this.stateService.getTheme(),
|
|
|
|
locale: (await this.stateService.getLocale()) ?? null,
|
|
|
|
};
|
|
|
|
this.startingLocale = initialFormValues.locale;
|
|
|
|
this.startingTheme = initialFormValues.theme;
|
|
|
|
this.form.setValue(initialFormValues, { emitEvent: false });
|
2021-09-10 15:27:00 +02:00
|
|
|
}
|
|
|
|
|
2018-06-25 22:44:06 +02:00
|
|
|
async submit() {
|
2023-04-15 01:11:33 +02:00
|
|
|
if (!this.form.controls.vaultTimeout.valid) {
|
2022-06-08 06:41:27 +02:00
|
|
|
this.platformUtilsService.showToast(
|
|
|
|
"error",
|
|
|
|
null,
|
|
|
|
this.i18nService.t("vaultTimeoutRangeError")
|
|
|
|
);
|
2021-12-17 15:57:11 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-04-15 01:11:33 +02:00
|
|
|
const values = this.form.value;
|
2021-12-17 15:57:11 +01:00
|
|
|
|
2022-08-30 22:30:43 +02:00
|
|
|
await this.vaultTimeoutSettingsService.setVaultTimeoutOptions(
|
2023-04-15 01:11:33 +02:00
|
|
|
values.vaultTimeout,
|
|
|
|
values.vaultTimeoutAction
|
2021-12-17 15:57:11 +01:00
|
|
|
);
|
2023-04-28 21:07:26 +02:00
|
|
|
await this.settingsService.setDisableFavicon(!values.enableFavicons);
|
2023-04-15 01:11:33 +02:00
|
|
|
await this.stateService.setEnableFullWidth(values.enableFullWidth);
|
2020-05-30 10:30:35 +02:00
|
|
|
this.messagingService.send("setFullWidth");
|
2023-04-15 01:11:33 +02:00
|
|
|
if (values.theme !== this.startingTheme) {
|
|
|
|
await this.themingService.updateConfiguredTheme(values.theme);
|
|
|
|
this.startingTheme = values.theme;
|
2021-09-30 00:06:20 +02:00
|
|
|
}
|
2023-04-15 01:11:33 +02:00
|
|
|
await this.stateService.setLocale(values.locale);
|
|
|
|
if (values.locale !== this.startingLocale) {
|
2018-06-26 15:04:12 +02:00
|
|
|
window.location.reload();
|
|
|
|
} else {
|
2022-05-09 14:21:52 +02:00
|
|
|
this.platformUtilsService.showToast(
|
|
|
|
"success",
|
|
|
|
null,
|
|
|
|
this.i18nService.t("preferencesUpdated")
|
|
|
|
);
|
2018-06-25 22:44:06 +02:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2020-05-18 15:51:20 +02:00
|
|
|
|
2023-04-15 01:11:33 +02:00
|
|
|
ngOnDestroy() {
|
|
|
|
this.destroy$.next();
|
|
|
|
this.destroy$.complete();
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2018-06-25 22:44:06 +02:00
|
|
|
}
|