1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-19 07:35:48 +02:00
bitwarden-browser/src/angular/components/share.component.ts

102 lines
3.8 KiB
TypeScript
Raw Normal View History

2018-10-23 16:22:53 +02:00
import {
EventEmitter,
Input,
OnInit,
Output,
} from '@angular/core';
2019-05-10 20:10:28 +02:00
import { OrganizationUserStatusType } from '../../enums/organizationUserStatusType';
2018-10-23 16:22:53 +02:00
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<any>;
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();
2019-05-10 20:10:28 +02:00
this.writeableCollections = allCollections.map((c) => c).filter((c) => !c.readOnly);
2018-10-23 16:22:53 +02:00
const orgs = await this.userService.getAllOrganizations();
2019-05-10 20:10:28 +02:00
this.organizations = orgs.sort(Utils.getSortFunction(this.i18nService, 'name'))
.filter((o) => o.enabled && o.status === OrganizationUserStatusType.Confirmed);
2018-10-23 16:22:53 +02:00
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);
}
}
2019-05-10 20:10:28 +02:00
async submit(): Promise<boolean> {
2019-06-26 23:43:03 +02:00
const selectedCollectionIds = this.collections
.filter((c) => !!(c as any).checked)
.map((c) => c.id);
if (selectedCollectionIds.length === 0) {
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('selectOneCollection'));
}
2018-10-23 16:22:53 +02:00
const cipherDomain = await this.cipherService.get(this.cipherId);
const cipherView = await cipherDomain.decrypt();
try {
this.formPromise = this.cipherService.shareWithServer(cipherView, this.organizationId,
2019-06-26 23:43:03 +02:00
selectedCollectionIds).then(async () => {
this.onSharedCipher.emit();
this.platformUtilsService.eventTrack('Shared Cipher');
this.platformUtilsService.showToast('success', null, this.i18nService.t('sharedItem'));
});
2018-10-23 16:22:53 +02:00
await this.formPromise;
2019-05-10 20:10:28 +02:00
return true;
2018-10-23 16:22:53 +02:00
} catch { }
2019-05-10 20:10:28 +02:00
return false;
2018-10-23 16:22:53 +02:00
}
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;
}
}