From 7d42c4eaa0a0a88f79453a261382233bb86d393d Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 5 Jul 2018 12:56:58 -0400 Subject: [PATCH] org vault collections --- src/app/app.module.ts | 3 + .../organizations/collections.component.ts | 57 +++++++++++++++++++ src/app/organizations/vault.component.ts | 27 +++++++++ src/app/vault/collections.component.ts | 43 +++++++++----- 4 files changed, 117 insertions(+), 13 deletions(-) create mode 100644 src/app/organizations/collections.component.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index bc64db0f00..26105e3f64 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -35,6 +35,7 @@ import { TwoFactorComponent } from './accounts/two-factor.component'; import { AddEditComponent as OrgAddEditComponent } from './organizations/add-edit.component'; import { AttachmentsComponent as OrgAttachmentsComponent } from './organizations/attachments.component'; import { CiphersComponent as OrgCiphersComponent } from './organizations/ciphers.component'; +import { CollectionsComponent as OrgCollectionsComponent } from './organizations/collections.component'; import { GroupingsComponent as OrgGroupingsComponent } from './organizations/groupings.component'; import { VaultComponent as OrgVaultComponent } from './organizations/vault.component'; @@ -157,6 +158,7 @@ import { SearchCiphersPipe } from 'jslib/angular/pipes/search-ciphers.pipe'; OrgAddEditComponent, OrgAttachmentsComponent, OrgCiphersComponent, + OrgCollectionsComponent, OrgGroupingsComponent, OrganizationsComponent, OrganizationLayoutComponent, @@ -203,6 +205,7 @@ import { SearchCiphersPipe } from 'jslib/angular/pipes/search-ciphers.pipe'; ModalComponent, OrgAddEditComponent, OrgAttachmentsComponent, + OrgCollectionsComponent, PasswordGeneratorHistoryComponent, PurgeVaultComponent, ShareComponent, diff --git a/src/app/organizations/collections.component.ts b/src/app/organizations/collections.component.ts new file mode 100644 index 0000000000..8004b0defc --- /dev/null +++ b/src/app/organizations/collections.component.ts @@ -0,0 +1,57 @@ +import { Component } from '@angular/core'; + +import { ToasterService } from 'angular2-toaster'; +import { Angulartics2 } from 'angulartics2'; + +import { ApiService } from 'jslib/abstractions/api.service'; +import { CipherService } from 'jslib/abstractions/cipher.service'; +import { CollectionService } from 'jslib/abstractions/collection.service'; +import { I18nService } from 'jslib/abstractions/i18n.service'; + +import { CipherData } from 'jslib/models/data/cipherData'; +import { Cipher } from 'jslib/models/domain/cipher'; +import { Organization } from 'jslib/models/domain/organization'; +import { CipherCollectionsRequest } from 'jslib/models/request/cipherCollectionsRequest'; + +import { CollectionsComponent as BaseCollectionsComponent } from '../vault/collections.component'; + +@Component({ + selector: 'app-org-vault-collections', + templateUrl: '../vault/collections.component.html', +}) +export class CollectionsComponent extends BaseCollectionsComponent { + organization: Organization; + + constructor(collectionService: CollectionService, analytics: Angulartics2, + toasterService: ToasterService, i18nService: I18nService, + cipherService: CipherService, private apiService: ApiService) { + super(collectionService, analytics, toasterService, i18nService, cipherService); + } + + protected async loadCipher() { + if (!this.organization.isAdmin) { + return await super.loadCipher(); + } + const response = await this.apiService.getCipherAdmin(this.cipherId); + return new Cipher(new CipherData(response)); + } + + protected loadCipherCollections() { + if (!this.organization.isAdmin) { + return super.loadCipherCollections(); + } + return this.collectionIds; + } + + protected loadCollections() { + if (!this.organization.isAdmin) { + return super.loadCollections(); + } + return Promise.resolve(this.collections); + } + + protected saveCollections() { + const request = new CipherCollectionsRequest(this.cipherDomain.collectionIds); + return this.apiService.putCipherCollectionsAdmin(this.cipherId, request); + } +} diff --git a/src/app/organizations/vault.component.ts b/src/app/organizations/vault.component.ts index 8ec55c5fd4..4d32f46c0b 100644 --- a/src/app/organizations/vault.component.ts +++ b/src/app/organizations/vault.component.ts @@ -25,6 +25,7 @@ import { ModalComponent } from '../modal.component'; import { AddEditComponent } from './add-edit.component'; import { AttachmentsComponent } from './attachments.component'; import { CiphersComponent } from './ciphers.component'; +import { CollectionsComponent } from './collections.component'; import { GroupingsComponent } from './groupings.component'; @Component({ @@ -36,6 +37,7 @@ export class VaultComponent implements OnInit { @ViewChild(CiphersComponent) ciphersComponent: CiphersComponent; @ViewChild('attachments', { read: ViewContainerRef }) attachmentsModalRef: ViewContainerRef; @ViewChild('cipherAddEdit', { read: ViewContainerRef }) cipherAddEditModalRef: ViewContainerRef; + @ViewChild('collections', { read: ViewContainerRef }) collectionsModalRef: ViewContainerRef; organization: Organization; collectionId: string; @@ -151,6 +153,31 @@ export class VaultComponent implements OnInit { }); } + 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, 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; + }); + } + addCipher() { const component = this.editCipher(null); component.type = this.type; diff --git a/src/app/vault/collections.component.ts b/src/app/vault/collections.component.ts index bd2b86cab5..c2201af28d 100644 --- a/src/app/vault/collections.component.ts +++ b/src/app/vault/collections.component.ts @@ -29,26 +29,26 @@ export class CollectionsComponent implements OnInit, OnDestroy { formPromise: Promise; cipher: CipherView; + collectionIds: string[]; collections: CollectionView[] = []; - private cipherDomain: Cipher; + protected cipherDomain: Cipher; - constructor(private collectionService: CollectionService, private analytics: Angulartics2, - private toasterService: ToasterService, private i18nService: I18nService, - private cipherService: CipherService) { } + constructor(protected collectionService: CollectionService, protected analytics: Angulartics2, + protected toasterService: ToasterService, protected i18nService: I18nService, + protected cipherService: CipherService) { } async ngOnInit() { - this.cipherDomain = await this.cipherService.get(this.cipherId); + this.cipherDomain = await this.loadCipher(); + this.collectionIds = this.loadCipherCollections(); this.cipher = await this.cipherDomain.decrypt(); - const allCollections = await this.collectionService.getAllDecrypted(); - this.collections = allCollections.filter((c) => - !c.readOnly && c.organizationId === this.cipher.organizationId); + this.collections = await this.loadCollections(); this.unselectAll(); - if (this.cipherDomain.collectionIds != null) { - for (const collection of this.collections) { - (collection as any).checked = this.cipherDomain.collectionIds.indexOf(collection.id) > -1; - } + if (this.collectionIds != null) { + this.collections.forEach((c) => { + (c as any).checked = this.collectionIds.indexOf(c.id) > -1; + }); } } @@ -60,7 +60,7 @@ export class CollectionsComponent implements OnInit, OnDestroy { this.cipherDomain.collectionIds = this.collections .filter((c) => !!(c as any).checked) .map((c) => c.id); - this.formPromise = this.cipherService.saveCollectionsWithServer(this.cipherDomain); + this.formPromise = this.saveCollections(); await this.formPromise; this.onSavedCollections.emit(); this.analytics.eventTrack.next({ action: 'Edited Cipher Collections' }); @@ -82,4 +82,21 @@ export class CollectionsComponent implements OnInit, OnDestroy { (c as any).checked = false; } } + + protected loadCipher() { + return this.cipherService.get(this.cipherId); + } + + protected loadCipherCollections() { + return this.cipherDomain.collectionIds; + } + + protected async loadCollections() { + const allCollections = await this.collectionService.getAllDecrypted(); + return allCollections.filter((c) => !c.readOnly && c.organizationId === this.cipher.organizationId); + } + + protected saveCollections() { + return this.cipherService.saveCollectionsWithServer(this.cipherDomain); + } }