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

267 lines
10 KiB
TypeScript
Raw Normal View History

2018-07-04 05:33:12 +02:00
import { Location } from '@angular/common';
2018-07-03 18:34:20 +02:00
import {
2018-07-03 21:11:58 +02:00
Component,
2018-07-05 15:42:50 +02:00
ComponentFactoryResolver,
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';
2018-07-04 05:33:12 +02:00
import { SyncService } from 'jslib/abstractions/sync.service';
import { UserService } from 'jslib/abstractions/user.service';
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
@Component({
selector: 'app-org-vault',
templateUrl: 'vault.component.html',
})
2018-07-03 21:11:58 +02:00
export class VaultComponent implements OnInit {
2018-07-04 05:33:12 +02:00
@ViewChild(GroupingsComponent) groupingsComponent: GroupingsComponent;
@ViewChild(CiphersComponent) ciphersComponent: CiphersComponent;
2018-07-05 16:48:51 +02:00
@ViewChild('attachments', { read: ViewContainerRef }) attachmentsModalRef: ViewContainerRef;
2018-07-05 15:42:50 +02:00
@ViewChild('cipherAddEdit', { read: ViewContainerRef }) cipherAddEditModalRef: ViewContainerRef;
2018-07-05 18:56:58 +02:00
@ViewChild('collections', { read: ViewContainerRef }) collectionsModalRef: ViewContainerRef;
@ViewChild('eventsTemplate', { read: ViewContainerRef }) eventsModalRef: ViewContainerRef;
2018-07-04 05:33:12 +02:00
organization: Organization;
collectionId: string;
type: CipherType;
2018-07-05 15:42:50 +02:00
private modal: ModalComponent = null;
2018-07-04 05:33:12 +02:00
constructor(private route: ActivatedRoute, private userService: UserService,
private location: Location, private router: Router,
2018-07-05 15:42:50 +02:00
private syncService: SyncService, private i18nService: I18nService,
2018-07-18 15:21:23 +02:00
private componentFactoryResolver: ComponentFactoryResolver, private messagingService: MessagingService) { }
2018-07-03 18:34:20 +02:00
2018-07-03 21:11:58 +02:00
ngOnInit() {
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;
this.route.queryParams.subscribe(async (qParams) => {
if (!this.organization.isAdmin) {
await this.syncService.fullSync(false);
}
await this.groupingsComponent.load();
if (qParams == null) {
this.groupingsComponent.selectedAll = true;
await this.ciphersComponent.load();
} 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;
await this.ciphersComponent.load();
}
2018-07-04 05:33:12 +02:00
}
2018-07-03 21:11:58 +02:00
this.ciphersComponent.searchText = this.groupingsComponent.searchText = qParams.search;
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
}
});
2018-07-03 21:11:58 +02:00
});
}
2018-07-04 05:33:12 +02:00
async clearGroupingFilters() {
this.ciphersComponent.showAddNew = true;
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) {
this.ciphersComponent.showAddNew = true;
2018-07-04 05:33:12 +02:00
this.groupingsComponent.searchPlaceholder = this.i18nService.t('searchType');
const filter = (c: CipherView) => c.type === type;
if (load) {
await this.ciphersComponent.load(filter);
} else {
await this.ciphersComponent.applyFilter(filter);
}
this.clearFilters();
this.type = type;
this.go();
}
async filterCollection(collectionId: string, load = false) {
this.ciphersComponent.showAddNew = 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 {
return c.collectionIds.indexOf(collectionId) > -1;
}
};
2018-07-04 05:33:12 +02:00
if (load) {
await this.ciphersComponent.load(filter);
} else {
await this.ciphersComponent.applyFilter(filter);
}
this.clearFilters();
this.collectionId = collectionId;
this.go();
}
filterSearchText(searchText: string) {
this.ciphersComponent.searchText = searchText;
}
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.isAdmin) {
childComponent.collectionIds = cipher.collectionIds;
childComponent.collections = this.groupingsComponent.collections.filter((c) => !c.readOnly);
}
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);
component.type = this.type;
}
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();
});
this.modal.onClosed.subscribe(() => {
this.modal = null;
});
return childComponent;
}
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;
}
private go(queryParams: any = null) {
if (queryParams == null) {
queryParams = {
type: this.type,
collectionId: this.collectionId,
};
}
const url = this.router.createUrlTree(['organizations', this.organization.id, 'vault'],
{ queryParams: queryParams }).toString();
this.location.go(url);
}
2018-07-03 18:34:20 +02:00
}