From 89c23522d5697c722dcbe7d074cd12ebb6dc8783 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 23 Oct 2018 10:22:53 -0400 Subject: [PATCH] share component --- src/angular/components/add-edit.component.ts | 2 +- src/angular/components/share.component.ts | 99 ++++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 src/angular/components/share.component.ts diff --git a/src/angular/components/add-edit.component.ts b/src/angular/components/add-edit.component.ts index 059e702592..25071de329 100644 --- a/src/angular/components/add-edit.component.ts +++ b/src/angular/components/add-edit.component.ts @@ -67,7 +67,7 @@ export class AddEditComponent implements OnInit { uriMatchOptions: any[]; ownershipOptions: any[] = []; - private writeableCollections: CollectionView[]; + protected writeableCollections: CollectionView[]; constructor(protected cipherService: CipherService, protected folderService: FolderService, protected i18nService: I18nService, protected platformUtilsService: PlatformUtilsService, diff --git a/src/angular/components/share.component.ts b/src/angular/components/share.component.ts new file mode 100644 index 0000000000..0bd64784fb --- /dev/null +++ b/src/angular/components/share.component.ts @@ -0,0 +1,99 @@ +import { + EventEmitter, + Input, + OnInit, + Output, +} from '@angular/core'; + +import { CipherService } from '../../abstractions/cipher.service'; +import { CollectionService } from '../../abstractions/collection.service'; +import { I18nService } from '../../abstractions/i18n.service'; +import { PlatformUtilsService } from '../../abstractions/platformUtils.service'; +import { UserService } from '../../abstractions/user.service'; + +import { Organization } from '../../models/domain/organization'; +import { CipherView } from '../../models/view/cipherView'; +import { CollectionView } from '../../models/view/collectionView'; + +import { Utils } from '../../misc/utils'; + +export class ShareComponent implements OnInit { + @Input() cipherId: string; + @Input() organizationId: string; + @Output() onSharedCipher = new EventEmitter(); + + formPromise: Promise; + cipher: CipherView; + collections: CollectionView[] = []; + organizations: Organization[] = []; + + protected writeableCollections: CollectionView[] = []; + + constructor(protected collectionService: CollectionService, protected platformUtilsService: PlatformUtilsService, + protected i18nService: I18nService, protected userService: UserService, + protected cipherService: CipherService) { } + + async ngOnInit() { + await this.load(); + } + + async load() { + const allCollections = await this.collectionService.getAllDecrypted(); + this.writeableCollections = allCollections.map((c) => c).filter((c) => !c.readOnly) + .sort(Utils.getSortFunction(this.i18nService, 'name')); + const orgs = await this.userService.getAllOrganizations(); + this.organizations = orgs.sort(Utils.getSortFunction(this.i18nService, 'name')); + + const cipherDomain = await this.cipherService.get(this.cipherId); + this.cipher = await cipherDomain.decrypt(); + if (this.organizationId == null && this.organizations.length > 0) { + this.organizationId = this.organizations[0].id; + } + this.filterCollections(); + } + + filterCollections() { + this.writeableCollections.forEach((c) => (c as any).checked = false); + if (this.organizationId == null || this.writeableCollections.length === 0) { + this.collections = []; + } else { + this.collections = this.writeableCollections.filter((c) => c.organizationId === this.organizationId); + } + } + + async submit() { + const cipherDomain = await this.cipherService.get(this.cipherId); + const cipherView = await cipherDomain.decrypt(); + + const attachmentPromises: Array> = []; + if (cipherView.attachments != null) { + for (const attachment of cipherView.attachments) { + const promise = this.cipherService.shareAttachmentWithServer(attachment, cipherView.id, + this.organizationId); + attachmentPromises.push(promise); + } + } + + const checkedCollectionIds = this.collections.filter((c) => (c as any).checked).map((c) => c.id); + try { + this.formPromise = Promise.all(attachmentPromises).then(async () => { + await this.cipherService.shareWithServer(cipherView, this.organizationId, checkedCollectionIds); + this.onSharedCipher.emit(); + this.platformUtilsService.eventTrack('Shared Cipher'); + this.platformUtilsService.showToast('success', null, this.i18nService.t('sharedItem')); + }); + await this.formPromise; + } catch { } + } + + get canSave() { + if (this.collections != null) { + for (let i = 0; i < this.collections.length; i++) { + if ((this.collections[i] as any).checked) { + return true; + } + } + } + return false; + } +}