admin functions for cipher attachments

This commit is contained in:
Kyle Spearrin 2018-07-05 10:48:19 -04:00
parent ef5eebba66
commit 47ab71e730
3 changed files with 31 additions and 12 deletions

View File

@ -30,8 +30,9 @@ export abstract class CipherService {
shareManyWithServer: (ciphers: CipherView[], organizationId: string, collectionIds: string[]) => Promise<any>;
shareAttachmentWithServer: (attachmentView: AttachmentView, cipherId: string,
organizationId: string) => Promise<any>;
saveAttachmentWithServer: (cipher: Cipher, unencryptedFile: any) => Promise<Cipher>;
saveAttachmentRawWithServer: (cipher: Cipher, filename: string, data: ArrayBuffer) => Promise<Cipher>;
saveAttachmentWithServer: (cipher: Cipher, unencryptedFile: any, admin?: boolean) => Promise<Cipher>;
saveAttachmentRawWithServer: (cipher: Cipher, filename: string, data: ArrayBuffer,
admin?: boolean) => Promise<Cipher>;
saveCollectionsWithServer: (cipher: Cipher) => Promise<any>;
upsert: (cipher: CipherData | CipherData[]) => Promise<any>;
replace: (ciphers: { [id: string]: CipherData; }) => Promise<any>;

View File

@ -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);
}
}

View File

@ -418,14 +418,14 @@ export class CipherService implements CipherServiceAbstraction {
}
}
saveAttachmentWithServer(cipher: Cipher, unencryptedFile: any): Promise<Cipher> {
saveAttachmentWithServer(cipher: Cipher, unencryptedFile: any, admin = false): Promise<Cipher> {
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<Cipher> {
async saveAttachmentRawWithServer(cipher: Cipher, filename: string,
data: ArrayBuffer, admin = false): Promise<Cipher> {
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<any> {
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);