From 47ab71e73098d1b83a1bdcbae3cd3e2b1d9ccacc Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 5 Jul 2018 10:48:19 -0400 Subject: [PATCH] admin functions for cipher attachments --- src/abstractions/cipher.service.ts | 5 +++-- .../components/attachments.component.ts | 19 +++++++++++++++---- src/services/cipher.service.ts | 19 +++++++++++++------ 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/abstractions/cipher.service.ts b/src/abstractions/cipher.service.ts index 5fa873017d..39176d1204 100644 --- a/src/abstractions/cipher.service.ts +++ b/src/abstractions/cipher.service.ts @@ -30,8 +30,9 @@ export abstract class CipherService { shareManyWithServer: (ciphers: CipherView[], organizationId: string, collectionIds: string[]) => Promise; shareAttachmentWithServer: (attachmentView: AttachmentView, cipherId: string, organizationId: string) => Promise; - saveAttachmentWithServer: (cipher: Cipher, unencryptedFile: any) => Promise; - saveAttachmentRawWithServer: (cipher: Cipher, filename: string, data: ArrayBuffer) => Promise; + saveAttachmentWithServer: (cipher: Cipher, unencryptedFile: any, admin?: boolean) => Promise; + saveAttachmentRawWithServer: (cipher: Cipher, filename: string, data: ArrayBuffer, + admin?: boolean) => Promise; saveCollectionsWithServer: (cipher: Cipher) => Promise; upsert: (cipher: CipherData | CipherData[]) => Promise; replace: (ciphers: { [id: string]: CipherData; }) => Promise; diff --git a/src/angular/components/attachments.component.ts b/src/angular/components/attachments.component.ts index fff40c4b0b..6429c1cdd9 100644 --- a/src/angular/components/attachments.component.ts +++ b/src/angular/components/attachments.component.ts @@ -37,7 +37,7 @@ export class AttachmentsComponent implements OnInit { protected platformUtilsService: PlatformUtilsService, protected win: Window) { } async ngOnInit() { - this.cipherDomain = await this.cipherService.get(this.cipherId); + this.cipherDomain = await this.loadCipher(); this.cipher = await this.cipherDomain.decrypt(); const key = await this.cryptoService.getEncKey(); @@ -84,7 +84,7 @@ export class AttachmentsComponent implements OnInit { } try { - this.formPromise = this.cipherService.saveAttachmentWithServer(this.cipherDomain, files[0]); + this.formPromise = this.saveCipherAttachment(files[0]); this.cipherDomain = await this.formPromise; this.cipher = await this.cipherDomain.decrypt(); this.analytics.eventTrack.next({ action: 'Added Attachment' }); @@ -112,8 +112,7 @@ export class AttachmentsComponent implements OnInit { } try { - this.deletePromises[attachment.id] = this.cipherService.deleteAttachmentWithServer( - this.cipher.id, attachment.id); + this.deletePromises[attachment.id] = this.deleteCipherAttachment(attachment.id); await this.deletePromises[attachment.id]; this.analytics.eventTrack.next({ action: 'Deleted Attachment' }); this.toasterService.popAsync('success', null, this.i18nService.t('deletedAttachment')); @@ -158,4 +157,16 @@ export class AttachmentsComponent implements OnInit { a.downloading = false; } + + protected loadCipher() { + return this.cipherService.get(this.cipherId); + } + + protected saveCipherAttachment(file: File) { + return this.cipherService.saveAttachmentWithServer(this.cipherDomain, file); + } + + protected deleteCipherAttachment(attachmentId: string) { + return this.cipherService.deleteAttachmentWithServer(this.cipher.id, attachmentId); + } } diff --git a/src/services/cipher.service.ts b/src/services/cipher.service.ts index 6215fbd2fa..54cc1517b8 100644 --- a/src/services/cipher.service.ts +++ b/src/services/cipher.service.ts @@ -418,14 +418,14 @@ export class CipherService implements CipherServiceAbstraction { } } - saveAttachmentWithServer(cipher: Cipher, unencryptedFile: any): Promise { + saveAttachmentWithServer(cipher: Cipher, unencryptedFile: any, admin = false): Promise { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.readAsArrayBuffer(unencryptedFile); reader.onload = async (evt: any) => { try { const cData = await this.saveAttachmentRawWithServer(cipher, - unencryptedFile.name, evt.target.result); + unencryptedFile.name, evt.target.result, admin); resolve(cData); } catch (e) { reject(e); @@ -437,7 +437,8 @@ export class CipherService implements CipherServiceAbstraction { }); } - async saveAttachmentRawWithServer(cipher: Cipher, filename: string, data: ArrayBuffer): Promise { + async saveAttachmentRawWithServer(cipher: Cipher, filename: string, + data: ArrayBuffer, admin = false): Promise { const key = await this.cryptoService.getOrgKey(cipher.organizationId); const encFileName = await this.cryptoService.encrypt(filename, key); const encData = await this.cryptoService.encryptToBytes(data, key); @@ -459,20 +460,26 @@ export class CipherService implements CipherServiceAbstraction { let response: CipherResponse; try { - response = await this.apiService.postCipherAttachment(cipher.id, fd); + if (admin) { + response = await this.apiService.postCipherAttachmentAdmin(cipher.id, fd); + } else { + response = await this.apiService.postCipherAttachment(cipher.id, fd); + } } catch (e) { throw new Error((e as ErrorResponse).getSingleMessage()); } const userId = await this.userService.getUserId(); const cData = new CipherData(response, userId, cipher.collectionIds); - this.upsert(cData); + if (!admin) { + this.upsert(cData); + } return new Cipher(cData); } async saveCollectionsWithServer(cipher: Cipher): Promise { const request = new CipherCollectionsRequest(cipher.collectionIds); - const response = await this.apiService.putCipherCollections(cipher.id, request); + await this.apiService.putCipherCollections(cipher.id, request); const userId = await this.userService.getUserId(); const data = cipher.toCipherData(userId); await this.upsert(data);