mirror of
https://github.com/bitwarden/browser
synced 2025-12-17 08:43:33 +00:00
admin functions for cipher attachments
This commit is contained in:
@@ -30,8 +30,9 @@ export abstract class CipherService {
|
|||||||
shareManyWithServer: (ciphers: CipherView[], organizationId: string, collectionIds: string[]) => Promise<any>;
|
shareManyWithServer: (ciphers: CipherView[], organizationId: string, collectionIds: string[]) => Promise<any>;
|
||||||
shareAttachmentWithServer: (attachmentView: AttachmentView, cipherId: string,
|
shareAttachmentWithServer: (attachmentView: AttachmentView, cipherId: string,
|
||||||
organizationId: string) => Promise<any>;
|
organizationId: string) => Promise<any>;
|
||||||
saveAttachmentWithServer: (cipher: Cipher, unencryptedFile: any) => Promise<Cipher>;
|
saveAttachmentWithServer: (cipher: Cipher, unencryptedFile: any, admin?: boolean) => Promise<Cipher>;
|
||||||
saveAttachmentRawWithServer: (cipher: Cipher, filename: string, data: ArrayBuffer) => Promise<Cipher>;
|
saveAttachmentRawWithServer: (cipher: Cipher, filename: string, data: ArrayBuffer,
|
||||||
|
admin?: boolean) => Promise<Cipher>;
|
||||||
saveCollectionsWithServer: (cipher: Cipher) => Promise<any>;
|
saveCollectionsWithServer: (cipher: Cipher) => Promise<any>;
|
||||||
upsert: (cipher: CipherData | CipherData[]) => Promise<any>;
|
upsert: (cipher: CipherData | CipherData[]) => Promise<any>;
|
||||||
replace: (ciphers: { [id: string]: CipherData; }) => Promise<any>;
|
replace: (ciphers: { [id: string]: CipherData; }) => Promise<any>;
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ export class AttachmentsComponent implements OnInit {
|
|||||||
protected platformUtilsService: PlatformUtilsService, protected win: Window) { }
|
protected platformUtilsService: PlatformUtilsService, protected win: Window) { }
|
||||||
|
|
||||||
async ngOnInit() {
|
async ngOnInit() {
|
||||||
this.cipherDomain = await this.cipherService.get(this.cipherId);
|
this.cipherDomain = await this.loadCipher();
|
||||||
this.cipher = await this.cipherDomain.decrypt();
|
this.cipher = await this.cipherDomain.decrypt();
|
||||||
|
|
||||||
const key = await this.cryptoService.getEncKey();
|
const key = await this.cryptoService.getEncKey();
|
||||||
@@ -84,7 +84,7 @@ export class AttachmentsComponent implements OnInit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.formPromise = this.cipherService.saveAttachmentWithServer(this.cipherDomain, files[0]);
|
this.formPromise = this.saveCipherAttachment(files[0]);
|
||||||
this.cipherDomain = await this.formPromise;
|
this.cipherDomain = await this.formPromise;
|
||||||
this.cipher = await this.cipherDomain.decrypt();
|
this.cipher = await this.cipherDomain.decrypt();
|
||||||
this.analytics.eventTrack.next({ action: 'Added Attachment' });
|
this.analytics.eventTrack.next({ action: 'Added Attachment' });
|
||||||
@@ -112,8 +112,7 @@ export class AttachmentsComponent implements OnInit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.deletePromises[attachment.id] = this.cipherService.deleteAttachmentWithServer(
|
this.deletePromises[attachment.id] = this.deleteCipherAttachment(attachment.id);
|
||||||
this.cipher.id, attachment.id);
|
|
||||||
await this.deletePromises[attachment.id];
|
await this.deletePromises[attachment.id];
|
||||||
this.analytics.eventTrack.next({ action: 'Deleted Attachment' });
|
this.analytics.eventTrack.next({ action: 'Deleted Attachment' });
|
||||||
this.toasterService.popAsync('success', null, this.i18nService.t('deletedAttachment'));
|
this.toasterService.popAsync('success', null, this.i18nService.t('deletedAttachment'));
|
||||||
@@ -158,4 +157,16 @@ export class AttachmentsComponent implements OnInit {
|
|||||||
|
|
||||||
a.downloading = false;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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) => {
|
return new Promise((resolve, reject) => {
|
||||||
const reader = new FileReader();
|
const reader = new FileReader();
|
||||||
reader.readAsArrayBuffer(unencryptedFile);
|
reader.readAsArrayBuffer(unencryptedFile);
|
||||||
reader.onload = async (evt: any) => {
|
reader.onload = async (evt: any) => {
|
||||||
try {
|
try {
|
||||||
const cData = await this.saveAttachmentRawWithServer(cipher,
|
const cData = await this.saveAttachmentRawWithServer(cipher,
|
||||||
unencryptedFile.name, evt.target.result);
|
unencryptedFile.name, evt.target.result, admin);
|
||||||
resolve(cData);
|
resolve(cData);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
reject(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 key = await this.cryptoService.getOrgKey(cipher.organizationId);
|
||||||
const encFileName = await this.cryptoService.encrypt(filename, key);
|
const encFileName = await this.cryptoService.encrypt(filename, key);
|
||||||
const encData = await this.cryptoService.encryptToBytes(data, key);
|
const encData = await this.cryptoService.encryptToBytes(data, key);
|
||||||
@@ -459,20 +460,26 @@ export class CipherService implements CipherServiceAbstraction {
|
|||||||
|
|
||||||
let response: CipherResponse;
|
let response: CipherResponse;
|
||||||
try {
|
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) {
|
} catch (e) {
|
||||||
throw new Error((e as ErrorResponse).getSingleMessage());
|
throw new Error((e as ErrorResponse).getSingleMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
const userId = await this.userService.getUserId();
|
const userId = await this.userService.getUserId();
|
||||||
const cData = new CipherData(response, userId, cipher.collectionIds);
|
const cData = new CipherData(response, userId, cipher.collectionIds);
|
||||||
this.upsert(cData);
|
if (!admin) {
|
||||||
|
this.upsert(cData);
|
||||||
|
}
|
||||||
return new Cipher(cData);
|
return new Cipher(cData);
|
||||||
}
|
}
|
||||||
|
|
||||||
async saveCollectionsWithServer(cipher: Cipher): Promise<any> {
|
async saveCollectionsWithServer(cipher: Cipher): Promise<any> {
|
||||||
const request = new CipherCollectionsRequest(cipher.collectionIds);
|
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 userId = await this.userService.getUserId();
|
||||||
const data = cipher.toCipherData(userId);
|
const data = cipher.toCipherData(userId);
|
||||||
await this.upsert(data);
|
await this.upsert(data);
|
||||||
|
|||||||
Reference in New Issue
Block a user