bitwarden-estensione-browser/src/app/vault/vault.component.ts

360 lines
15 KiB
TypeScript
Raw Normal View History

2018-06-05 05:10:41 +02:00
import {
2018-08-21 04:21:13 +02:00
ChangeDetectorRef,
2018-06-05 05:10:41 +02:00
Component,
2018-08-21 04:21:13 +02:00
NgZone,
OnDestroy,
2018-06-06 23:25:57 +02:00
OnInit,
ViewChild,
ViewContainerRef,
2018-06-05 05:10:41 +02:00
} from '@angular/core';
2018-06-06 23:25:57 +02:00
import {
ActivatedRoute,
Router,
} from '@angular/router';
2018-06-05 05:10:41 +02:00
import { first } from 'rxjs/operators';
import { CipherType } from 'jslib-common/enums/cipherType';
2018-06-05 19:31:25 +02:00
import { CipherView } from 'jslib-common/models/view/cipherView';
2018-06-06 23:25:57 +02:00
2018-07-11 17:39:48 +02:00
import { OrganizationsComponent } from '../settings/organizations.component';
2018-07-17 23:22:51 +02:00
import { UpdateKeyComponent } from '../settings/update-key.component';
2018-06-07 05:23:14 +02:00
import { AddEditComponent } from './add-edit.component';
2018-06-06 23:25:57 +02:00
import { AttachmentsComponent } from './attachments.component';
import { CiphersComponent } from './ciphers.component';
2018-06-12 19:08:47 +02:00
import { CollectionsComponent } from './collections.component';
2018-06-06 23:25:57 +02:00
import { FolderAddEditComponent } from './folder-add-edit.component';
import { GroupingsComponent } from './groupings.component';
2018-06-12 19:08:47 +02:00
import { ShareComponent } from './share.component';
2018-06-06 23:25:57 +02:00
2021-12-03 03:32:58 +01:00
import { BroadcasterService } from 'jslib-common/abstractions/broadcaster.service';
import { CryptoService } from 'jslib-common/abstractions/crypto.service';
import { I18nService } from 'jslib-common/abstractions/i18n.service';
import { MessagingService } from 'jslib-common/abstractions/messaging.service';
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
import { SyncService } from 'jslib-common/abstractions/sync.service';
import { TokenService } from 'jslib-common/abstractions/token.service';
import { UserService } from 'jslib-common/abstractions/user.service';
2018-06-06 23:25:57 +02:00
import { ModalService } from 'jslib-angular/services/modal.service';
2018-08-21 04:21:13 +02:00
const BroadcasterSubscriptionId = 'VaultComponent';
2018-06-05 05:10:41 +02:00
@Component({
selector: 'app-vault',
templateUrl: 'vault.component.html',
})
2018-08-21 04:21:13 +02:00
export class VaultComponent implements OnInit, OnDestroy {
@ViewChild(GroupingsComponent, { static: true }) groupingsComponent: GroupingsComponent;
@ViewChild(CiphersComponent, { static: true }) ciphersComponent: CiphersComponent;
@ViewChild(OrganizationsComponent, { static: true }) organizationsComponent: OrganizationsComponent;
@ViewChild('attachments', { read: ViewContainerRef, static: true }) attachmentsModalRef: ViewContainerRef;
@ViewChild('folderAddEdit', { read: ViewContainerRef, static: true }) folderAddEditModalRef: ViewContainerRef;
@ViewChild('cipherAddEdit', { read: ViewContainerRef, static: true }) cipherAddEditModalRef: ViewContainerRef;
@ViewChild('share', { read: ViewContainerRef, static: true }) shareModalRef: ViewContainerRef;
@ViewChild('collections', { read: ViewContainerRef, static: true }) collectionsModalRef: ViewContainerRef;
@ViewChild('updateKeyTemplate', { read: ViewContainerRef, static: true }) updateKeyModalRef: ViewContainerRef;
2018-06-06 23:25:57 +02:00
favorites: boolean = false;
type: CipherType = null;
folderId: string = null;
collectionId: string = null;
showVerifyEmail = false;
showBrowserOutdated = false;
showUpdateKey = false;
2018-07-20 16:44:17 +02:00
showPremiumCallout = false;
Feature/families for enterprise (#1300) * Added manual routing * Families for enterprise/account settings (#1290) * Added sponsored families page * Revert "Added manual routing" This reverts commit a970ba78ffa98545176b636630e48115efcf51cc. * Add messages to page * Remove stages and simplify design * Switch to new figma design * Add screen reader * Add calls to server * Reorder methods * Used to organization filters * Connected page to server * Add preliminary text to subscription page * Sponsor existing family organization flow * Update jslib Co-authored-by: Matt Gibson <mgibson@bitwarden.com> * Add revoke sponsorship flow * Add spinner to send offer button * Determine if subscription has sponsored items * Work on subscription button * Add message for new family organization * Families for enterprise/subscription page (#1292) * Work on subscription button * Determine if subscription has sponsored items * Work on subscriptions page * Add message for new family organization Co-authored-by: Matt Gibson <mgibson@bitwarden.com> * Families for enterprise/redeem card (#1295) * Add toast localization message * Use helpers to property display sponsorship items * Split table rows into component so buttons load (#1296) * Split table rows into component so buttons load * Update jslib * Families for enterprise/localizations (#1299) * Add more localizations * Remove unneeded comments * Fix help article * Run linting * Do not show redeem button if no orgs exist to redeem * Implement new process for accepting sponsorships * Hide business checkbox * Update jslib * Removed commented code * Remove commented html * Cleaned up imports * Use proper message * Remove merge conflict message * Remove confusing comment * Listened to PR feedback * Remove unused property * Update help text * Fix aria labels * Add try catch * Made toast before emit * Minor copy changes * Update jslib * Remove unneeded loading Co-authored-by: Matt Gibson <mgibson@bitwarden.com>
2021-11-22 14:41:40 +01:00
showRedeemSponsorship = false;
showProviders = false;
deleted: boolean = false;
trashCleanupWarning: string = null;
2018-06-06 23:25:57 +02:00
Feature/families for enterprise (#1300) * Added manual routing * Families for enterprise/account settings (#1290) * Added sponsored families page * Revert "Added manual routing" This reverts commit a970ba78ffa98545176b636630e48115efcf51cc. * Add messages to page * Remove stages and simplify design * Switch to new figma design * Add screen reader * Add calls to server * Reorder methods * Used to organization filters * Connected page to server * Add preliminary text to subscription page * Sponsor existing family organization flow * Update jslib Co-authored-by: Matt Gibson <mgibson@bitwarden.com> * Add revoke sponsorship flow * Add spinner to send offer button * Determine if subscription has sponsored items * Work on subscription button * Add message for new family organization * Families for enterprise/subscription page (#1292) * Work on subscription button * Determine if subscription has sponsored items * Work on subscriptions page * Add message for new family organization Co-authored-by: Matt Gibson <mgibson@bitwarden.com> * Families for enterprise/redeem card (#1295) * Add toast localization message * Use helpers to property display sponsorship items * Split table rows into component so buttons load (#1296) * Split table rows into component so buttons load * Update jslib * Families for enterprise/localizations (#1299) * Add more localizations * Remove unneeded comments * Fix help article * Run linting * Do not show redeem button if no orgs exist to redeem * Implement new process for accepting sponsorships * Hide business checkbox * Update jslib * Removed commented code * Remove commented html * Cleaned up imports * Use proper message * Remove merge conflict message * Remove confusing comment * Listened to PR feedback * Remove unused property * Update help text * Fix aria labels * Add try catch * Made toast before emit * Minor copy changes * Update jslib * Remove unneeded loading Co-authored-by: Matt Gibson <mgibson@bitwarden.com>
2021-11-22 14:41:40 +01:00
2018-06-06 23:25:57 +02:00
constructor(private syncService: SyncService, private route: ActivatedRoute,
private router: Router, private changeDetectorRef: ChangeDetectorRef,
private i18nService: I18nService, private modalService: ModalService,
2018-07-18 15:21:23 +02:00
private tokenService: TokenService, private cryptoService: CryptoService,
2018-07-20 16:44:17 +02:00
private messagingService: MessagingService, private userService: UserService,
private platformUtilsService: PlatformUtilsService, private broadcasterService: BroadcasterService,
private ngZone: NgZone) { }
2018-06-06 23:25:57 +02:00
async ngOnInit() {
this.showVerifyEmail = !(await this.tokenService.getEmailVerified());
this.showBrowserOutdated = window.navigator.userAgent.indexOf('MSIE') !== -1;
this.trashCleanupWarning = this.i18nService.t(
this.platformUtilsService.isSelfHost() ? 'trashCleanupWarningSelfHosted' : 'trashCleanupWarning'
);
this.route.queryParams.pipe(first()).subscribe(async params => {
2018-06-26 18:03:11 +02:00
await this.syncService.fullSync(false);
2018-08-31 23:23:36 +02:00
this.showUpdateKey = !(await this.cryptoService.hasEncKey());
const canAccessPremium = await this.userService.canAccessPremium();
this.showPremiumCallout = !this.showVerifyEmail && !canAccessPremium &&
!this.platformUtilsService.isSelfHost();
2018-08-21 04:21:13 +02:00
this.showProviders = (await this.userService.getAllProviders()).length > 0;
Feature/families for enterprise (#1300) * Added manual routing * Families for enterprise/account settings (#1290) * Added sponsored families page * Revert "Added manual routing" This reverts commit a970ba78ffa98545176b636630e48115efcf51cc. * Add messages to page * Remove stages and simplify design * Switch to new figma design * Add screen reader * Add calls to server * Reorder methods * Used to organization filters * Connected page to server * Add preliminary text to subscription page * Sponsor existing family organization flow * Update jslib Co-authored-by: Matt Gibson <mgibson@bitwarden.com> * Add revoke sponsorship flow * Add spinner to send offer button * Determine if subscription has sponsored items * Work on subscription button * Add message for new family organization * Families for enterprise/subscription page (#1292) * Work on subscription button * Determine if subscription has sponsored items * Work on subscriptions page * Add message for new family organization Co-authored-by: Matt Gibson <mgibson@bitwarden.com> * Families for enterprise/redeem card (#1295) * Add toast localization message * Use helpers to property display sponsorship items * Split table rows into component so buttons load (#1296) * Split table rows into component so buttons load * Update jslib * Families for enterprise/localizations (#1299) * Add more localizations * Remove unneeded comments * Fix help article * Run linting * Do not show redeem button if no orgs exist to redeem * Implement new process for accepting sponsorships * Hide business checkbox * Update jslib * Removed commented code * Remove commented html * Cleaned up imports * Use proper message * Remove merge conflict message * Remove confusing comment * Listened to PR feedback * Remove unused property * Update help text * Fix aria labels * Add try catch * Made toast before emit * Minor copy changes * Update jslib * Remove unneeded loading Co-authored-by: Matt Gibson <mgibson@bitwarden.com>
2021-11-22 14:41:40 +01:00
const allOrgs = await this.userService.getAllOrganizations();
this.showRedeemSponsorship = allOrgs.some(o => o.familySponsorshipAvailable) && !allOrgs.some(o => o.familySponsorshipFriendlyName != null);
2018-07-11 17:39:48 +02:00
await Promise.all([
this.groupingsComponent.load(),
this.organizationsComponent.load(),
]);
2018-06-06 23:25:57 +02:00
if (params == null) {
this.groupingsComponent.selectedAll = true;
2019-03-19 17:44:22 +01:00
await this.ciphersComponent.reload();
2018-06-06 23:25:57 +02:00
} else {
if (params.deleted) {
this.groupingsComponent.selectedTrash = true;
await this.filterDeleted();
} else if (params.favorites) {
this.groupingsComponent.selectedFavorites = true;
await this.filterFavorites();
} else if (params.type) {
const t = parseInt(params.type, null);
this.groupingsComponent.selectedType = t;
await this.filterCipherType(t);
} else if (params.folderId) {
this.groupingsComponent.selectedFolder = true;
this.groupingsComponent.selectedFolderId = params.folderId;
await this.filterFolder(params.folderId);
} else if (params.collectionId) {
this.groupingsComponent.selectedCollectionId = params.collectionId;
await this.filterCollection(params.collectionId);
} else {
this.groupingsComponent.selectedAll = true;
2019-03-19 17:44:22 +01:00
await this.ciphersComponent.reload();
}
2018-06-06 23:25:57 +02:00
}
2018-08-31 23:23:36 +02:00
this.broadcasterService.subscribe(BroadcasterSubscriptionId, (message: any) => {
this.ngZone.run(async () => {
switch (message.command) {
case 'syncCompleted':
if (message.successfully) {
await Promise.all([
this.groupingsComponent.load(),
this.organizationsComponent.load(),
this.ciphersComponent.load(this.ciphersComponent.filter),
]);
this.changeDetectorRef.detectChanges();
}
break;
}
});
});
2018-06-06 23:25:57 +02:00
});
}
2018-08-21 04:21:13 +02:00
ngOnDestroy() {
this.broadcasterService.unsubscribe(BroadcasterSubscriptionId);
}
2018-06-06 23:25:57 +02:00
async clearGroupingFilters() {
this.ciphersComponent.showAddNew = true;
2018-06-07 15:05:25 +02:00
this.groupingsComponent.searchPlaceholder = this.i18nService.t('searchVault');
2019-03-19 17:44:22 +01:00
await this.ciphersComponent.reload();
2018-06-06 23:25:57 +02:00
this.clearFilters();
this.go();
}
async filterFavorites() {
this.ciphersComponent.showAddNew = true;
2018-06-07 15:05:25 +02:00
this.groupingsComponent.searchPlaceholder = this.i18nService.t('searchFavorites');
await this.ciphersComponent.reload(c => c.favorite);
2018-06-06 23:25:57 +02:00
this.clearFilters();
this.favorites = true;
this.go();
}
async filterDeleted() {
this.ciphersComponent.showAddNew = false;
this.ciphersComponent.deleted = true;
this.groupingsComponent.searchPlaceholder = this.i18nService.t('searchTrash');
await this.ciphersComponent.reload(null, true);
this.clearFilters();
this.deleted = true;
this.go();
}
2018-06-06 23:25:57 +02:00
async filterCipherType(type: CipherType) {
this.ciphersComponent.showAddNew = true;
2018-06-07 15:05:25 +02:00
this.groupingsComponent.searchPlaceholder = this.i18nService.t('searchType');
await this.ciphersComponent.reload(c => c.type === type);
2018-06-06 23:25:57 +02:00
this.clearFilters();
this.type = type;
this.go();
}
async filterFolder(folderId: string) {
this.ciphersComponent.showAddNew = true;
2018-06-06 23:25:57 +02:00
folderId = folderId === 'none' ? null : folderId;
2018-06-07 15:05:25 +02:00
this.groupingsComponent.searchPlaceholder = this.i18nService.t('searchFolder');
await this.ciphersComponent.reload(c => c.folderId === folderId);
2018-06-06 23:25:57 +02:00
this.clearFilters();
this.folderId = folderId == null ? 'none' : folderId;
this.go();
}
async filterCollection(collectionId: string) {
2018-10-22 22:46:48 +02:00
this.ciphersComponent.showAddNew = true;
2018-06-07 15:05:25 +02:00
this.groupingsComponent.searchPlaceholder = this.i18nService.t('searchCollection');
await this.ciphersComponent.reload(c => c.collectionIds != null &&
2019-03-19 17:44:22 +01:00
c.collectionIds.indexOf(collectionId) > -1);
2018-06-06 23:25:57 +02:00
this.clearFilters();
this.collectionId = collectionId;
this.go();
}
2018-06-07 05:00:57 +02:00
filterSearchText(searchText: string) {
this.ciphersComponent.searchText = searchText;
2018-08-13 22:27:17 +02:00
this.ciphersComponent.search(200);
2018-06-07 05:00:57 +02:00
}
2018-07-18 15:21:23 +02:00
async editCipherAttachments(cipher: CipherView) {
const canAccessPremium = await this.userService.canAccessPremium();
if (cipher.organizationId == null && !canAccessPremium) {
2018-07-18 15:21:23 +02:00
this.messagingService.send('premiumRequired');
return;
} else if (cipher.organizationId != null) {
const org = await this.userService.getOrganization(cipher.organizationId);
if (org != null && (org.maxStorageGb == null || org.maxStorageGb === 0)) {
this.messagingService.send('upgradeOrganization', { organizationId: cipher.organizationId });
return;
}
}
2018-06-09 19:29:30 +02:00
let madeAttachmentChanges = false;
const [modal] = await this.modalService.openViewRef(AttachmentsComponent, this.attachmentsModalRef, comp => {
comp.cipherId = cipher.id;
comp.onUploadedAttachment.subscribe(() => madeAttachmentChanges = true);
comp.onDeletedAttachment.subscribe(() => madeAttachmentChanges = true);
comp.onReuploadedAttachment.subscribe(() => madeAttachmentChanges = true);
});
2018-06-06 23:25:57 +02:00
modal.onClosed.subscribe(async () => {
2018-06-09 19:29:30 +02:00
if (madeAttachmentChanges) {
await this.ciphersComponent.refresh();
}
madeAttachmentChanges = false;
2018-06-06 23:25:57 +02:00
});
}
async shareCipher(cipher: CipherView) {
const [modal] = await this.modalService.openViewRef(ShareComponent, this.shareModalRef, comp => {
comp.cipherId = cipher.id;
comp.onSharedCipher.subscribe(async () => {
modal.close();
await this.ciphersComponent.refresh();
});
2018-06-12 17:46:11 +02:00
});
}
async editCipherCollections(cipher: CipherView) {
const [modal] = await this.modalService.openViewRef(CollectionsComponent, this.collectionsModalRef, comp => {
comp.cipherId = cipher.id;
comp.onSavedCollections.subscribe(async () => {
modal.close();
await this.ciphersComponent.refresh();
});
2018-06-12 19:08:47 +02:00
});
}
2018-06-06 23:25:57 +02:00
async addFolder() {
const [modal] = await this.modalService.openViewRef(FolderAddEditComponent, this.folderAddEditModalRef, comp => {
comp.folderId = null;
comp.onSavedFolder.subscribe(async () => {
modal.close();
await this.groupingsComponent.loadFolders();
});
2018-06-06 23:25:57 +02:00
});
}
async editFolder(folderId: string) {
const [modal] = await this.modalService.openViewRef(FolderAddEditComponent, this.folderAddEditModalRef, comp => {
comp.folderId = folderId;
comp.onSavedFolder.subscribe(async () => {
modal.close();
await this.groupingsComponent.loadFolders();
});
comp.onDeletedFolder.subscribe(async () => {
modal.close();
await this.groupingsComponent.loadFolders();
await this.filterFolder('none');
this.groupingsComponent.selectedFolderId = null;
});
2018-06-06 23:25:57 +02:00
});
}
async addCipher() {
const component = await this.editCipher(null);
2018-06-07 05:23:14 +02:00
component.type = this.type;
component.folderId = this.folderId === 'none' ? null : this.folderId;
2018-10-22 22:46:48 +02:00
if (this.collectionId != null) {
const collection = this.groupingsComponent.collections.filter(c => c.id === this.collectionId);
2018-10-22 22:46:48 +02:00
if (collection.length > 0) {
component.organizationId = collection[0].organizationId;
component.collectionIds = [this.collectionId];
}
}
2018-06-07 05:23:14 +02:00
}
async editCipher(cipher: CipherView) {
const [modal, childComponent] = await this.modalService.openViewRef(AddEditComponent, this.cipherAddEditModalRef, comp => {
comp.cipherId = cipher == null ? null : cipher.id;
comp.onSavedCipher.subscribe(async (c: CipherView) => {
modal.close();
await this.ciphersComponent.refresh();
});
comp.onDeletedCipher.subscribe(async (c: CipherView) => {
modal.close();
await this.ciphersComponent.refresh();
});
comp.onRestoredCipher.subscribe(async (c: CipherView) => {
modal.close();
await this.ciphersComponent.refresh();
});
2018-06-07 05:23:14 +02:00
});
return childComponent;
}
async cloneCipher(cipher: CipherView) {
const component = await this.editCipher(cipher);
component.cloneMode = true;
}
async updateKey() {
await this.modalService.openViewRef(UpdateKeyComponent, this.updateKeyModalRef);
2018-07-17 23:22:51 +02:00
}
2018-06-06 23:25:57 +02:00
private clearFilters() {
this.folderId = null;
this.collectionId = null;
this.favorites = false;
this.type = null;
this.deleted = false;
2018-06-06 23:25:57 +02:00
}
private go(queryParams: any = null) {
if (queryParams == null) {
queryParams = {
favorites: this.favorites ? true : null,
type: this.type,
folderId: this.folderId,
collectionId: this.collectionId,
deleted: this.deleted ? true : null,
2018-06-06 23:25:57 +02:00
};
}
2018-06-05 05:10:41 +02:00
this.router.navigate([], {
relativeTo: this.route,
queryParams: queryParams,
replaceUrl: true,
});
2018-06-06 23:25:57 +02:00
}
2018-06-05 05:10:41 +02:00
}