bitwarden-estensione-browser/src/popup/vault/add-edit.component.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

234 lines
7.2 KiB
TypeScript
Raw Normal View History

2018-04-06 05:49:04 +02:00
import { Location } from "@angular/common";
import { Component } from "@angular/core";
2018-04-06 05:49:04 +02:00
import { ActivatedRoute, Router } from "@angular/router";
import { first } from "rxjs/operators";
2022-02-24 18:14:04 +01:00
import { AddEditComponent as BaseAddEditComponent } from "jslib-angular/components/add-edit.component";
import { AuditService } from "jslib-common/abstractions/audit.service";
import { CipherService } from "jslib-common/abstractions/cipher.service";
import { CollectionService } from "jslib-common/abstractions/collection.service";
import { EventService } from "jslib-common/abstractions/event.service";
import { FolderService } from "jslib-common/abstractions/folder.service";
import { I18nService } from "jslib-common/abstractions/i18n.service";
import { LogService } from "jslib-common/abstractions/log.service";
import { MessagingService } from "jslib-common/abstractions/messaging.service";
[Account Switching] Base changes for account switching (#2250) * Pull in jslib * Create new state models * Create browser specific stateService * Remove registration deprecated services, register stateService * Replace usage of deprecated services (user, constants) * Add missing properties to BrowserGroupingsComponentState * Remove StorageService from initFactory * Clear the correct state * Add null check when restoring send-grouping state * add remember email * Initialize stateservice in services.module * Fix 'lock now' not working * Comment to remove setting defaults on install * Pull jslib * Remove setting defaults on install * Bump jslib * Pass the current userId to services when logging out * Bump jslib * Override vaultTimeout default on account addition * Pull latest jslib * Retrieve vaultTimeout from stateService * Record activity per Account * Add userId to logout and add fallback if not present * Register AccountFactory * Pass userId in messages * Base changes for account switching di fixes (#2280) * [bug] Null checks on Account init * [bug] Use same stateService instance for all operations We override the stateService in browser, but currently don't pull the background service into popup and allow jslib to create its own instance of the base StateService for jslib services. This causes a split in in memory state between the three isntances that results in many errors, namely locking not working. * [chore] Update jslib * Pull in jslib * Pull in jslib * Pull in latest jslib to multiple stateservice inits * Check vault states before executing processReload * Adjust iterator * Update native messaging to include the userId (#2290) * Re-Add UserVerificationService * Fix email not being remembered by base component * Improve readability of reloadProcess * Removed unneeded null check * Fix constructor dependency (stateService) * Added missing await * Simplify dependency registration * Fixed typos * Reverted back to simple loop * Use vaultTimeoutService to retrieve Timeout Co-authored-by: Addison Beck <abeck@bitwarden.com> Co-authored-by: Oscar Hinton <oscar@oscarhinton.com>
2022-01-27 22:22:51 +01:00
import { OrganizationService } from "jslib-common/abstractions/organization.service";
import { PasswordRepromptService } from "jslib-common/abstractions/passwordReprompt.service";
import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service";
import { PolicyService } from "jslib-common/abstractions/policy.service";
import { StateService } from "jslib-common/abstractions/state.service";
2022-02-24 18:14:04 +01:00
import { CipherType } from "jslib-common/enums/cipherType";
import { LoginUriView } from "jslib-common/models/view/loginUriView";
2022-02-24 18:14:04 +01:00
import { BrowserApi } from "../../browser/browserApi";
import { PopupUtilsService } from "../services/popup-utils.service";
2018-04-06 05:49:04 +02:00
@Component({
selector: "app-vault-add-edit",
2018-04-06 17:48:45 +02:00
templateUrl: "add-edit.component.html",
2018-04-06 05:49:04 +02:00
})
export class AddEditComponent extends BaseAddEditComponent {
currentUris: string[];
2018-07-25 05:23:44 +02:00
showAttachments = true;
openAttachmentsInPopup: boolean;
2021-05-17 05:46:39 +02:00
showAutoFillOnPageLoadOptions: boolean;
2021-12-21 15:43:35 +01:00
2018-04-06 05:49:04 +02:00
constructor(
cipherService: CipherService,
folderService: FolderService,
i18nService: I18nService,
platformUtilsService: PlatformUtilsService,
2018-04-09 23:35:16 +02:00
auditService: AuditService,
stateService: StateService,
collectionService: CollectionService,
messagingService: MessagingService,
private route: ActivatedRoute,
2019-07-12 16:41:14 +02:00
private router: Router,
private location: Location,
eventService: EventService,
policyService: PolicyService,
private popupUtilsService: PopupUtilsService,
[Account Switching] Base changes for account switching (#2250) * Pull in jslib * Create new state models * Create browser specific stateService * Remove registration deprecated services, register stateService * Replace usage of deprecated services (user, constants) * Add missing properties to BrowserGroupingsComponentState * Remove StorageService from initFactory * Clear the correct state * Add null check when restoring send-grouping state * add remember email * Initialize stateservice in services.module * Fix 'lock now' not working * Comment to remove setting defaults on install * Pull jslib * Remove setting defaults on install * Bump jslib * Pass the current userId to services when logging out * Bump jslib * Override vaultTimeout default on account addition * Pull latest jslib * Retrieve vaultTimeout from stateService * Record activity per Account * Add userId to logout and add fallback if not present * Register AccountFactory * Pass userId in messages * Base changes for account switching di fixes (#2280) * [bug] Null checks on Account init * [bug] Use same stateService instance for all operations We override the stateService in browser, but currently don't pull the background service into popup and allow jslib to create its own instance of the base StateService for jslib services. This causes a split in in memory state between the three isntances that results in many errors, namely locking not working. * [chore] Update jslib * Pull in jslib * Pull in jslib * Pull in latest jslib to multiple stateservice inits * Check vault states before executing processReload * Adjust iterator * Update native messaging to include the userId (#2290) * Re-Add UserVerificationService * Fix email not being remembered by base component * Improve readability of reloadProcess * Removed unneeded null check * Fix constructor dependency (stateService) * Added missing await * Simplify dependency registration * Fixed typos * Reverted back to simple loop * Use vaultTimeoutService to retrieve Timeout Co-authored-by: Addison Beck <abeck@bitwarden.com> Co-authored-by: Oscar Hinton <oscar@oscarhinton.com>
2022-01-27 22:22:51 +01:00
organizationService: OrganizationService,
passwordRepromptService: PasswordRepromptService,
logService: LogService
2018-04-06 05:49:04 +02:00
) {
super(
cipherService,
2018-04-10 06:04:49 +02:00
folderService,
2021-03-02 19:31:52 +01:00
i18nService,
platformUtilsService,
auditService,
stateService,
collectionService,
messagingService,
eventService,
policyService,
[Account Switching] Base changes for account switching (#2250) * Pull in jslib * Create new state models * Create browser specific stateService * Remove registration deprecated services, register stateService * Replace usage of deprecated services (user, constants) * Add missing properties to BrowserGroupingsComponentState * Remove StorageService from initFactory * Clear the correct state * Add null check when restoring send-grouping state * add remember email * Initialize stateservice in services.module * Fix 'lock now' not working * Comment to remove setting defaults on install * Pull jslib * Remove setting defaults on install * Bump jslib * Pass the current userId to services when logging out * Bump jslib * Override vaultTimeout default on account addition * Pull latest jslib * Retrieve vaultTimeout from stateService * Record activity per Account * Add userId to logout and add fallback if not present * Register AccountFactory * Pass userId in messages * Base changes for account switching di fixes (#2280) * [bug] Null checks on Account init * [bug] Use same stateService instance for all operations We override the stateService in browser, but currently don't pull the background service into popup and allow jslib to create its own instance of the base StateService for jslib services. This causes a split in in memory state between the three isntances that results in many errors, namely locking not working. * [chore] Update jslib * Pull in jslib * Pull in jslib * Pull in latest jslib to multiple stateservice inits * Check vault states before executing processReload * Adjust iterator * Update native messaging to include the userId (#2290) * Re-Add UserVerificationService * Fix email not being remembered by base component * Improve readability of reloadProcess * Removed unneeded null check * Fix constructor dependency (stateService) * Added missing await * Simplify dependency registration * Fixed typos * Reverted back to simple loop * Use vaultTimeoutService to retrieve Timeout Co-authored-by: Addison Beck <abeck@bitwarden.com> Co-authored-by: Oscar Hinton <oscar@oscarhinton.com>
2022-01-27 22:22:51 +01:00
logService,
passwordRepromptService,
[Account Switching] Base changes for account switching (#2250) * Pull in jslib * Create new state models * Create browser specific stateService * Remove registration deprecated services, register stateService * Replace usage of deprecated services (user, constants) * Add missing properties to BrowserGroupingsComponentState * Remove StorageService from initFactory * Clear the correct state * Add null check when restoring send-grouping state * add remember email * Initialize stateservice in services.module * Fix 'lock now' not working * Comment to remove setting defaults on install * Pull jslib * Remove setting defaults on install * Bump jslib * Pass the current userId to services when logging out * Bump jslib * Override vaultTimeout default on account addition * Pull latest jslib * Retrieve vaultTimeout from stateService * Record activity per Account * Add userId to logout and add fallback if not present * Register AccountFactory * Pass userId in messages * Base changes for account switching di fixes (#2280) * [bug] Null checks on Account init * [bug] Use same stateService instance for all operations We override the stateService in browser, but currently don't pull the background service into popup and allow jslib to create its own instance of the base StateService for jslib services. This causes a split in in memory state between the three isntances that results in many errors, namely locking not working. * [chore] Update jslib * Pull in jslib * Pull in jslib * Pull in latest jslib to multiple stateservice inits * Check vault states before executing processReload * Adjust iterator * Update native messaging to include the userId (#2290) * Re-Add UserVerificationService * Fix email not being remembered by base component * Improve readability of reloadProcess * Removed unneeded null check * Fix constructor dependency (stateService) * Added missing await * Simplify dependency registration * Fixed typos * Reverted back to simple loop * Use vaultTimeoutService to retrieve Timeout Co-authored-by: Addison Beck <abeck@bitwarden.com> Co-authored-by: Oscar Hinton <oscar@oscarhinton.com>
2022-01-27 22:22:51 +01:00
organizationService
2021-12-21 15:43:35 +01:00
);
}
async ngOnInit() {
await super.ngOnInit();
2021-12-21 15:43:35 +01:00
this.route.queryParams.pipe(first()).subscribe(async (params) => {
if (params.cipherId) {
this.cipherId = params.cipherId;
2018-04-10 06:04:49 +02:00
}
if (params.folderId) {
this.folderId = params.folderId;
}
if (params.collectionId) {
const collection = this.writeableCollections.find((c) => c.id === params.collectionId);
if (collection != null) {
this.collectionIds = [collection.id];
2021-03-02 19:31:52 +01:00
this.organizationId = collection.organizationId;
}
2021-12-21 15:43:35 +01:00
}
if (params.type) {
const type = parseInt(params.type, null);
2018-04-10 06:04:49 +02:00
this.type = type;
2021-12-21 15:43:35 +01:00
}
2018-04-06 05:49:04 +02:00
this.editMode = !params.cipherId;
2021-12-21 15:43:35 +01:00
if (params.cloneMode != null) {
this.cloneMode = params.cloneMode === "true";
2021-12-21 15:43:35 +01:00
}
await this.load();
2021-12-21 15:43:35 +01:00
2018-04-10 06:04:49 +02:00
if (!this.editMode || this.cloneMode) {
if (
!this.popupUtilsService.inPopout(window) &&
params.name &&
(this.cipher.name == null || this.cipher.name === "")
2021-05-17 06:32:48 +02:00
) {
2021-05-17 05:46:39 +02:00
this.cipher.name = params.name;
2021-12-21 15:43:35 +01:00
}
if (
2021-05-17 05:46:39 +02:00
!this.popupUtilsService.inPopout(window) &&
2021-12-21 15:43:35 +01:00
params.uri &&
2021-05-17 05:46:39 +02:00
(this.cipher.login.uris[0].uri == null || this.cipher.login.uris[0].uri === "")
2021-12-21 15:43:35 +01:00
) {
2021-05-17 05:46:39 +02:00
this.cipher.login.uris[0].uri = params.uri;
2018-04-06 05:49:04 +02:00
}
2021-12-21 15:43:35 +01:00
}
2018-04-06 05:51:58 +02:00
this.openAttachmentsInPopup = this.popupUtilsService.inPopup(window);
2018-04-06 05:49:04 +02:00
});
2018-04-11 19:51:09 +02:00
if (!this.editMode) {
const tabs = await BrowserApi.tabsQuery({ windowType: "normal" });
this.currentUris =
2021-03-02 19:31:52 +01:00
tabs == null
2021-12-21 15:43:35 +01:00
? null
2021-03-02 19:31:52 +01:00
: tabs.filter((tab) => tab.url != null && tab.url !== "").map((tab) => tab.url);
2021-12-21 15:43:35 +01:00
}
window.setTimeout(() => {
if (!this.editMode) {
if (this.cipher.name != null && this.cipher.name !== "") {
document.getElementById("loginUsername").focus();
} else {
document.getElementById("name").focus();
}
2021-12-21 15:43:35 +01:00
}
}, 200);
}
2021-05-17 06:32:48 +02:00
async load() {
await super.load();
2021-05-17 05:46:39 +02:00
this.showAutoFillOnPageLoadOptions =
2018-04-10 06:04:49 +02:00
this.cipher.type === CipherType.Login &&
[Account Switching] Base changes for account switching (#2250) * Pull in jslib * Create new state models * Create browser specific stateService * Remove registration deprecated services, register stateService * Replace usage of deprecated services (user, constants) * Add missing properties to BrowserGroupingsComponentState * Remove StorageService from initFactory * Clear the correct state * Add null check when restoring send-grouping state * add remember email * Initialize stateservice in services.module * Fix 'lock now' not working * Comment to remove setting defaults on install * Pull jslib * Remove setting defaults on install * Bump jslib * Pass the current userId to services when logging out * Bump jslib * Override vaultTimeout default on account addition * Pull latest jslib * Retrieve vaultTimeout from stateService * Record activity per Account * Add userId to logout and add fallback if not present * Register AccountFactory * Pass userId in messages * Base changes for account switching di fixes (#2280) * [bug] Null checks on Account init * [bug] Use same stateService instance for all operations We override the stateService in browser, but currently don't pull the background service into popup and allow jslib to create its own instance of the base StateService for jslib services. This causes a split in in memory state between the three isntances that results in many errors, namely locking not working. * [chore] Update jslib * Pull in jslib * Pull in jslib * Pull in latest jslib to multiple stateservice inits * Check vault states before executing processReload * Adjust iterator * Update native messaging to include the userId (#2290) * Re-Add UserVerificationService * Fix email not being remembered by base component * Improve readability of reloadProcess * Removed unneeded null check * Fix constructor dependency (stateService) * Added missing await * Simplify dependency registration * Fixed typos * Reverted back to simple loop * Use vaultTimeoutService to retrieve Timeout Co-authored-by: Addison Beck <abeck@bitwarden.com> Co-authored-by: Oscar Hinton <oscar@oscarhinton.com>
2022-01-27 22:22:51 +01:00
(await this.stateService.getEnableAutoFillOnPageLoad());
2021-12-21 15:43:35 +01:00
}
2018-04-06 05:51:58 +02:00
async submit(): Promise<boolean> {
2018-04-06 05:49:04 +02:00
if (await super.submit()) {
if (this.cloneMode) {
2018-04-11 22:27:20 +02:00
this.router.navigate(["/tabs/vault"]);
2021-12-21 15:43:35 +01:00
} else {
2018-04-06 05:49:04 +02:00
this.location.back();
2021-12-21 15:43:35 +01:00
}
2018-04-06 05:51:58 +02:00
return true;
2018-04-11 19:51:09 +02:00
}
2018-04-06 05:51:58 +02:00
return false;
2021-12-21 15:43:35 +01:00
}
2018-10-23 18:16:27 +02:00
attachments() {
2018-10-23 21:41:55 +02:00
super.attachments();
2018-10-23 18:16:27 +02:00
if (this.openAttachmentsInPopup) {
const destinationUrl = this.router
2018-04-06 05:49:04 +02:00
.createUrlTree(["/attachments"], { queryParams: { cipherId: this.cipher.id } })
.toString();
const currentBaseUrl = window.location.href.replace(this.router.url, "");
this.popupUtilsService.popOut(window, currentBaseUrl + destinationUrl);
2021-12-21 15:43:35 +01:00
} else {
this.router.navigate(["/attachments"], { queryParams: { cipherId: this.cipher.id } });
2018-04-06 05:49:04 +02:00
}
2021-12-21 15:43:35 +01:00
}
2018-04-09 23:35:16 +02:00
2018-04-10 20:20:03 +02:00
editCollections() {
super.editCollections();
if (this.cipher.organizationId != null) {
2018-04-10 20:20:03 +02:00
this.router.navigate(["/collections"], { queryParams: { cipherId: this.cipher.id } });
2018-04-09 23:35:16 +02:00
}
2021-12-21 15:43:35 +01:00
}
2018-04-12 04:33:32 +02:00
cancel() {
2018-04-06 05:49:04 +02:00
super.cancel();
this.location.back();
2021-12-21 15:43:35 +01:00
}
async generateUsername(): Promise<boolean> {
const confirmed = await super.generateUsername();
if (confirmed) {
await this.saveCipherState();
this.router.navigate(["generator"], { queryParams: { type: "username" } });
}
return confirmed;
}
2018-04-12 04:33:32 +02:00
async generatePassword(): Promise<boolean> {
const confirmed = await super.generatePassword();
if (confirmed) {
await this.saveCipherState();
this.router.navigate(["generator"], { queryParams: { type: "password" } });
}
2018-04-12 04:33:32 +02:00
return confirmed;
2021-12-21 15:43:35 +01:00
}
async delete(): Promise<boolean> {
const confirmed = await super.delete();
if (confirmed) {
this.router.navigate(["/tabs/vault"]);
}
2018-04-12 04:33:32 +02:00
return confirmed;
2021-12-21 15:43:35 +01:00
}
toggleUriInput(uri: LoginUriView) {
const u = uri as any;
u.showCurrentUris = !u.showCurrentUris;
2021-12-21 15:43:35 +01:00
}
allowOwnershipOptions(): boolean {
2021-12-21 15:43:35 +01:00
return (
(!this.editMode || this.cloneMode) &&
this.ownershipOptions &&
(this.ownershipOptions.length > 1 || !this.allowPersonal)
2021-12-21 15:43:35 +01:00
);
}
private saveCipherState() {
return this.stateService.setAddEditCipherInfo({
cipher: this.cipher,
collectionIds:
this.collections == null
? []
: this.collections.filter((c) => (c as any).checked).map((c) => c.id),
});
}
2018-04-06 05:49:04 +02:00
}