1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-10 09:59:48 +01:00
bitwarden-browser/src/app/organizations/vault/vault.component.ts

357 lines
14 KiB
TypeScript
Raw Normal View History

2018-07-03 18:34:20 +02:00
import {
2018-08-21 04:21:13 +02:00
ChangeDetectorRef,
2018-07-03 21:11:58 +02:00
Component,
2018-07-05 15:42:50 +02:00
ComponentFactoryResolver,
2018-08-21 04:21:13 +02:00
NgZone,
OnDestroy,
2018-07-03 21:11:58 +02:00
OnInit,
2018-07-04 05:33:12 +02:00
ViewChild,
2018-07-05 15:42:50 +02:00
ViewContainerRef,
2018-07-03 18:34:20 +02:00
} from '@angular/core';
2018-07-04 05:33:12 +02:00
import {
ActivatedRoute,
Router,
} from '@angular/router';
import { I18nService } from 'jslib/abstractions/i18n.service';
2018-07-18 15:21:23 +02:00
import { MessagingService } from 'jslib/abstractions/messaging.service';
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
2018-07-04 05:33:12 +02:00
import { SyncService } from 'jslib/abstractions/sync.service';
import { UserService } from 'jslib/abstractions/user.service';
2018-08-21 04:21:13 +02:00
import { BroadcasterService } from 'jslib/angular/services/broadcaster.service';
2018-07-04 05:33:12 +02:00
import { Organization } from 'jslib/models/domain/organization';
import { CipherView } from 'jslib/models/view/cipherView';
import { CipherType } from 'jslib/enums/cipherType';
2018-07-05 19:14:33 +02:00
import { ModalComponent } from '../../modal.component';
2018-07-05 15:42:50 +02:00
import { EntityEventsComponent } from '../manage/entity-events.component';
2018-07-05 15:42:50 +02:00
import { AddEditComponent } from './add-edit.component';
2018-07-05 16:48:51 +02:00
import { AttachmentsComponent } from './attachments.component';
2018-07-04 05:33:12 +02:00
import { CiphersComponent } from './ciphers.component';
2018-07-05 18:56:58 +02:00
import { CollectionsComponent } from './collections.component';
2018-07-04 05:33:12 +02:00
import { GroupingsComponent } from './groupings.component';
2018-07-03 18:34:20 +02:00
2018-08-21 04:21:13 +02:00
const BroadcasterSubscriptionId = 'OrgVaultComponent';
2018-07-03 18:34:20 +02:00
@Component({
selector: 'app-org-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('attachments', { read: ViewContainerRef, static: true }) attachmentsModalRef: ViewContainerRef;
@ViewChild('cipherAddEdit', { read: ViewContainerRef, static: true }) cipherAddEditModalRef: ViewContainerRef;
@ViewChild('collections', { read: ViewContainerRef, static: true }) collectionsModalRef: ViewContainerRef;
@ViewChild('eventsTemplate', { read: ViewContainerRef, static: true }) eventsModalRef: ViewContainerRef;
2018-07-04 05:33:12 +02:00
organization: Organization;
collectionId: string = null;
type: CipherType = null;
deleted: boolean = false;
trashCleanupWarning: string = null;
2018-07-04 05:33:12 +02:00
modal: ModalComponent = null;
2018-07-05 15:42:50 +02:00
2018-07-04 05:33:12 +02:00
constructor(private route: ActivatedRoute, private userService: UserService,
private router: Router, private changeDetectorRef: ChangeDetectorRef,
2018-07-05 15:42:50 +02:00
private syncService: SyncService, private i18nService: I18nService,
2018-08-21 04:21:13 +02:00
private componentFactoryResolver: ComponentFactoryResolver, private messagingService: MessagingService,
private broadcasterService: BroadcasterService, private ngZone: NgZone,
private platformUtilsService: PlatformUtilsService) { }
2018-07-03 18:34:20 +02:00
2018-07-03 21:11:58 +02:00
ngOnInit() {
this.trashCleanupWarning = this.i18nService.t(
this.platformUtilsService.isSelfHost() ? 'trashCleanupWarningSelfHosted' : 'trashCleanupWarning'
);
const queryParams = this.route.parent.params.subscribe(async params => {
2018-07-04 05:33:12 +02:00
this.organization = await this.userService.getOrganization(params.organizationId);
this.groupingsComponent.organization = this.organization;
this.ciphersComponent.organization = this.organization;
const queryParamsSub = this.route.queryParams.subscribe(async qParams => {
2018-08-16 04:26:39 +02:00
this.ciphersComponent.searchText = this.groupingsComponent.searchText = qParams.search;
if (!this.organization.canManageAllCollections) {
2018-07-04 05:33:12 +02:00
await this.syncService.fullSync(false);
2018-08-21 04:21:13 +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.ciphersComponent.refresh(),
]);
this.changeDetectorRef.detectChanges();
}
break;
}
});
});
2018-07-04 05:33:12 +02:00
}
await this.groupingsComponent.load();
if (qParams == null) {
this.groupingsComponent.selectedAll = true;
2019-03-19 17:44:22 +01:00
await this.ciphersComponent.reload();
} else {
if (qParams.deleted) {
this.groupingsComponent.selectedTrash = true;
await this.filterDeleted(true);
} else if (qParams.type) {
const t = parseInt(qParams.type, null);
this.groupingsComponent.selectedType = t;
await this.filterCipherType(t, true);
} else if (qParams.collectionId) {
this.groupingsComponent.selectedCollectionId = qParams.collectionId;
await this.filterCollection(qParams.collectionId, true);
} else {
this.groupingsComponent.selectedAll = true;
2019-03-19 17:44:22 +01:00
await this.ciphersComponent.reload();
}
2018-07-04 05:33:12 +02:00
}
2018-07-03 21:11:58 +02:00
if (qParams.viewEvents != null) {
const cipher = this.ciphersComponent.ciphers.filter(c => c.id === qParams.viewEvents);
if (cipher.length > 0) {
this.viewEvents(cipher[0]);
}
2018-07-04 05:33:12 +02:00
}
2019-01-17 05:30:32 +01:00
if (queryParamsSub != null) {
queryParamsSub.unsubscribe();
}
2018-07-04 05:33:12 +02:00
});
if (queryParams != null) {
queryParams.unsubscribe();
}
2018-07-03 21:11:58 +02:00
});
}
2018-07-04 05:33:12 +02:00
2018-08-21 04:21:13 +02:00
ngOnDestroy() {
this.broadcasterService.unsubscribe(BroadcasterSubscriptionId);
}
2018-07-04 05:33:12 +02:00
async clearGroupingFilters() {
2018-10-22 22:46:48 +02:00
this.ciphersComponent.showAddNew = true;
this.ciphersComponent.deleted = false;
2018-07-04 05:33:12 +02:00
this.groupingsComponent.searchPlaceholder = this.i18nService.t('searchVault');
await this.ciphersComponent.applyFilter();
this.clearFilters();
this.go();
}
async filterCipherType(type: CipherType, load = false) {
2018-10-22 22:46:48 +02:00
this.ciphersComponent.showAddNew = true;
this.ciphersComponent.deleted = false;
2018-07-04 05:33:12 +02:00
this.groupingsComponent.searchPlaceholder = this.i18nService.t('searchType');
const filter = (c: CipherView) => c.type === type;
if (load) {
2019-03-19 17:44:22 +01:00
await this.ciphersComponent.reload(filter);
2018-07-04 05:33:12 +02:00
} else {
await this.ciphersComponent.applyFilter(filter);
}
this.clearFilters();
this.type = type;
this.go();
}
async filterCollection(collectionId: string, load = false) {
2018-10-22 22:46:48 +02:00
this.ciphersComponent.showAddNew = true;
this.ciphersComponent.deleted = false;
2018-07-04 05:33:12 +02:00
this.groupingsComponent.searchPlaceholder = this.i18nService.t('searchCollection');
2018-07-04 05:43:57 +02:00
const filter = (c: CipherView) => {
if (collectionId === 'unassigned') {
return c.collectionIds == null || c.collectionIds.length === 0;
} else {
2018-09-11 14:46:04 +02:00
return c.collectionIds != null && c.collectionIds.indexOf(collectionId) > -1;
2018-07-04 05:43:57 +02:00
}
};
2018-07-04 05:33:12 +02:00
if (load) {
2019-03-19 17:44:22 +01:00
await this.ciphersComponent.reload(filter);
2018-07-04 05:33:12 +02:00
} else {
await this.ciphersComponent.applyFilter(filter);
}
this.clearFilters();
this.collectionId = collectionId;
this.go();
}
async filterDeleted(load: boolean = false) {
this.ciphersComponent.showAddNew = false;
this.ciphersComponent.deleted = true;
this.groupingsComponent.searchPlaceholder = this.i18nService.t('searchTrash');
if (load) {
await this.ciphersComponent.reload(null, true);
} else {
await this.ciphersComponent.applyFilter(null);
}
this.clearFilters();
this.deleted = true;
this.go();
}
2018-07-04 05:33:12 +02:00
filterSearchText(searchText: string) {
this.ciphersComponent.searchText = searchText;
2018-08-13 22:27:17 +02:00
this.ciphersComponent.search(200);
2018-07-04 05:33:12 +02:00
}
2018-07-05 16:48:51 +02:00
editCipherAttachments(cipher: CipherView) {
2018-07-18 15:21:23 +02:00
if (this.organization.maxStorageGb == null || this.organization.maxStorageGb === 0) {
this.messagingService.send('upgradeOrganization', { organizationId: cipher.organizationId });
return;
}
2018-07-05 16:48:51 +02:00
if (this.modal != null) {
this.modal.close();
}
const factory = this.componentFactoryResolver.resolveComponentFactory(ModalComponent);
this.modal = this.attachmentsModalRef.createComponent(factory).instance;
const childComponent = this.modal.show<AttachmentsComponent>(AttachmentsComponent, this.attachmentsModalRef);
childComponent.organization = this.organization;
childComponent.cipherId = cipher.id;
let madeAttachmentChanges = false;
childComponent.onUploadedAttachment.subscribe(() => madeAttachmentChanges = true);
childComponent.onDeletedAttachment.subscribe(() => madeAttachmentChanges = true);
this.modal.onClosed.subscribe(async () => {
this.modal = null;
if (madeAttachmentChanges) {
await this.ciphersComponent.refresh();
}
madeAttachmentChanges = false;
});
}
2018-07-05 18:56:58 +02:00
editCipherCollections(cipher: CipherView) {
if (this.modal != null) {
this.modal.close();
}
const factory = this.componentFactoryResolver.resolveComponentFactory(ModalComponent);
this.modal = this.collectionsModalRef.createComponent(factory).instance;
const childComponent = this.modal.show<CollectionsComponent>(CollectionsComponent, this.collectionsModalRef);
if (this.organization.canManageAllCollections) {
2018-07-05 18:56:58 +02:00
childComponent.collectionIds = cipher.collectionIds;
childComponent.collections = this.groupingsComponent.collections.filter(c => !c.readOnly);
2018-07-05 18:56:58 +02:00
}
childComponent.organization = this.organization;
childComponent.cipherId = cipher.id;
childComponent.onSavedCollections.subscribe(async () => {
this.modal.close();
await this.ciphersComponent.refresh();
});
this.modal.onClosed.subscribe(async () => {
this.modal = null;
});
}
2018-07-05 15:42:50 +02:00
addCipher() {
const component = this.editCipher(null);
2018-10-22 22:46:48 +02:00
component.organizationId = this.organization.id;
2018-07-05 15:42:50 +02:00
component.type = this.type;
if (this.organization.canManageAllCollections) {
component.collections = this.groupingsComponent.collections.filter(c => !c.readOnly);
2018-10-22 22:46:48 +02:00
}
if (this.collectionId != null) {
component.collectionIds = [this.collectionId];
}
2018-07-05 15:42:50 +02:00
}
editCipher(cipher: CipherView) {
if (this.modal != null) {
this.modal.close();
}
const factory = this.componentFactoryResolver.resolveComponentFactory(ModalComponent);
this.modal = this.cipherAddEditModalRef.createComponent(factory).instance;
const childComponent = this.modal.show<AddEditComponent>(AddEditComponent, this.cipherAddEditModalRef);
childComponent.organization = this.organization;
childComponent.cipherId = cipher == null ? null : cipher.id;
childComponent.onSavedCipher.subscribe(async (c: CipherView) => {
this.modal.close();
await this.ciphersComponent.refresh();
});
childComponent.onDeletedCipher.subscribe(async (c: CipherView) => {
this.modal.close();
await this.ciphersComponent.refresh();
});
childComponent.onRestoredCipher.subscribe(async (c: CipherView) => {
this.modal.close();
await this.ciphersComponent.refresh();
});
2018-07-05 15:42:50 +02:00
this.modal.onClosed.subscribe(() => {
this.modal = null;
});
return childComponent;
}
cloneCipher(cipher: CipherView) {
const component = this.editCipher(cipher);
component.cloneMode = true;
component.organizationId = this.organization.id;
if (this.organization.canManageAllCollections) {
component.collections = this.groupingsComponent.collections.filter(c => !c.readOnly);
}
// Regardless of Admin state, the collection Ids need to passed manually as they are not assigned value
// in the add-edit componenet
component.collectionIds = cipher.collectionIds;
}
async viewEvents(cipher: CipherView) {
if (this.modal != null) {
this.modal.close();
}
const factory = this.componentFactoryResolver.resolveComponentFactory(ModalComponent);
this.modal = this.eventsModalRef.createComponent(factory).instance;
const childComponent = this.modal.show<EntityEventsComponent>(
EntityEventsComponent, this.eventsModalRef);
childComponent.name = cipher.name;
childComponent.organizationId = this.organization.id;
childComponent.entityId = cipher.id;
childComponent.showUser = true;
childComponent.entity = 'cipher';
this.modal.onClosed.subscribe(() => {
this.modal = null;
});
}
2018-07-04 05:33:12 +02:00
private clearFilters() {
this.collectionId = null;
this.type = null;
this.deleted = false;
2018-07-04 05:33:12 +02:00
}
private go(queryParams: any = null) {
if (queryParams == null) {
queryParams = {
type: this.type,
collectionId: this.collectionId,
deleted: this.deleted ? true : null,
2018-07-04 05:33:12 +02:00
};
}
this.router.navigate([], {
relativeTo: this.route,
queryParams: queryParams,
replaceUrl: true,
});
2018-07-04 05:33:12 +02:00
}
2018-07-03 18:34:20 +02:00
}