2018-06-13 06:03:48 +02:00
|
|
|
import { Component, EventEmitter, Input, OnInit, Output } from "@angular/core";
|
|
|
|
|
2022-06-14 17:10:53 +02:00
|
|
|
import { CipherService } from "@bitwarden/common/abstractions/cipher.service";
|
|
|
|
import { CollectionService } from "@bitwarden/common/abstractions/collection.service";
|
|
|
|
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
|
|
|
|
import { LogService } from "@bitwarden/common/abstractions/log.service";
|
|
|
|
import { OrganizationService } from "@bitwarden/common/abstractions/organization.service";
|
|
|
|
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
|
|
|
|
import { Organization } from "@bitwarden/common/models/domain/organization";
|
|
|
|
import { CipherView } from "@bitwarden/common/models/view/cipherView";
|
|
|
|
import { CollectionView } from "@bitwarden/common/models/view/collectionView";
|
2018-06-13 06:03:48 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: "app-vault-bulk-share",
|
|
|
|
templateUrl: "bulk-share.component.html",
|
|
|
|
})
|
|
|
|
export class BulkShareComponent implements OnInit {
|
|
|
|
@Input() ciphers: CipherView[] = [];
|
|
|
|
@Input() organizationId: string;
|
|
|
|
@Output() onShared = new EventEmitter();
|
|
|
|
|
|
|
|
nonShareableCount = 0;
|
|
|
|
collections: CollectionView[] = [];
|
|
|
|
organizations: Organization[] = [];
|
2018-07-09 22:56:09 +02:00
|
|
|
shareableCiphers: CipherView[] = [];
|
2018-06-13 06:03:48 +02:00
|
|
|
formPromise: Promise<any>;
|
|
|
|
|
|
|
|
private writeableCollections: CollectionView[] = [];
|
|
|
|
|
2021-12-07 20:41:45 +01:00
|
|
|
constructor(
|
|
|
|
private cipherService: CipherService,
|
|
|
|
private platformUtilsService: PlatformUtilsService,
|
2021-04-14 23:43:40 +02:00
|
|
|
private i18nService: I18nService,
|
|
|
|
private collectionService: CollectionService,
|
2021-12-14 17:10:26 +01:00
|
|
|
private organizationService: OrganizationService,
|
|
|
|
private logService: LogService
|
|
|
|
) {}
|
2018-06-13 06:03:48 +02:00
|
|
|
|
|
|
|
async ngOnInit() {
|
2021-02-03 18:41:33 +01:00
|
|
|
this.shareableCiphers = this.ciphers.filter(
|
|
|
|
(c) => !c.hasOldAttachments && c.organizationId == null
|
|
|
|
);
|
2018-06-13 06:03:48 +02:00
|
|
|
this.nonShareableCount = this.ciphers.length - this.shareableCiphers.length;
|
|
|
|
const allCollections = await this.collectionService.getAllDecrypted();
|
2021-02-03 18:41:33 +01:00
|
|
|
this.writeableCollections = allCollections.filter((c) => !c.readOnly);
|
2021-12-14 17:10:26 +01:00
|
|
|
this.organizations = await this.organizationService.getAll();
|
2018-06-13 06:03:48 +02:00
|
|
|
if (this.organizationId == null && this.organizations.length > 0) {
|
|
|
|
this.organizationId = this.organizations[0].id;
|
|
|
|
}
|
|
|
|
this.filterCollections();
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2018-06-13 06:03:48 +02:00
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.selectAll(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
filterCollections() {
|
|
|
|
this.selectAll(false);
|
|
|
|
if (this.organizationId == null || this.writeableCollections.length === 0) {
|
|
|
|
this.collections = [];
|
|
|
|
} else {
|
2021-02-03 18:41:33 +01:00
|
|
|
this.collections = this.writeableCollections.filter(
|
|
|
|
(c) => c.organizationId === this.organizationId
|
|
|
|
);
|
2018-06-13 06:03:48 +02:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2018-06-13 06:03:48 +02:00
|
|
|
|
|
|
|
async submit() {
|
2021-02-03 18:41:33 +01:00
|
|
|
const checkedCollectionIds = this.collections
|
|
|
|
.filter((c) => (c as any).checked)
|
|
|
|
.map((c) => c.id);
|
2018-08-08 05:46:31 +02:00
|
|
|
try {
|
|
|
|
this.formPromise = this.cipherService.shareManyWithServer(
|
|
|
|
this.shareableCiphers,
|
|
|
|
this.organizationId,
|
|
|
|
checkedCollectionIds
|
|
|
|
);
|
|
|
|
await this.formPromise;
|
|
|
|
this.onShared.emit();
|
2021-06-22 01:27:27 +02:00
|
|
|
const orgName =
|
|
|
|
this.organizations.find((o) => o.id === this.organizationId)?.name ??
|
|
|
|
this.i18nService.t("organization");
|
2021-12-07 20:41:45 +01:00
|
|
|
this.platformUtilsService.showToast(
|
|
|
|
"success",
|
|
|
|
null,
|
|
|
|
this.i18nService.t("movedItemsToOrg", orgName)
|
|
|
|
);
|
2021-10-20 18:30:04 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
2018-06-13 06:03:48 +02:00
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2018-06-13 06:03:48 +02:00
|
|
|
|
2018-07-09 22:27:54 +02:00
|
|
|
check(c: CollectionView, select?: boolean) {
|
|
|
|
(c as any).checked = select == null ? !(c as any).checked : select;
|
2018-06-13 06:03:48 +02:00
|
|
|
}
|
|
|
|
|
2018-07-09 22:27:54 +02:00
|
|
|
selectAll(select: boolean) {
|
2018-06-13 06:03:48 +02:00
|
|
|
const collections = select ? this.collections : this.writeableCollections;
|
2021-02-03 18:41:33 +01:00
|
|
|
collections.forEach((c) => this.check(c, select));
|
2018-06-13 06:03:48 +02:00
|
|
|
}
|
2018-08-08 05:46:31 +02:00
|
|
|
|
|
|
|
get canSave() {
|
|
|
|
if (
|
|
|
|
this.shareableCiphers != null &&
|
|
|
|
this.shareableCiphers.length > 0 &&
|
|
|
|
this.collections != null
|
|
|
|
) {
|
|
|
|
for (let i = 0; i < this.collections.length; i++) {
|
|
|
|
if ((this.collections[i] as any).checked) {
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2018-08-08 05:46:31 +02:00
|
|
|
}
|
|
|
|
return false;
|
2021-12-17 15:57:11 +01:00
|
|
|
}
|
2018-06-13 06:03:48 +02:00
|
|
|
}
|