1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-09 05:57:40 +02:00
bitwarden-browser/src/app/vault/ciphers.component.ts

186 lines
6.5 KiB
TypeScript
Raw Normal View History

2018-06-08 18:04:03 +02:00
import {
Component,
EventEmitter,
Input,
2018-07-28 04:05:03 +02:00
OnDestroy,
2018-06-08 18:04:03 +02:00
Output,
} from '@angular/core';
import { ToasterService } from 'angular2-toaster';
import { Angulartics2 } from 'angulartics2';
2018-06-06 23:25:57 +02:00
import { CipherService } from 'jslib/abstractions/cipher.service';
2019-07-12 21:34:27 +02:00
import { EventService } from 'jslib/abstractions/event.service';
2018-06-08 18:04:03 +02:00
import { I18nService } from 'jslib/abstractions/i18n.service';
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
2018-08-13 22:27:17 +02:00
import { SearchService } from 'jslib/abstractions/search.service';
import { TotpService } from 'jslib/abstractions/totp.service';
import { UserService } from 'jslib/abstractions/user.service';
2018-06-06 23:25:57 +02:00
import { CiphersComponent as BaseCiphersComponent } from 'jslib/angular/components/ciphers.component';
2018-06-07 23:12:11 +02:00
import { CipherType } from 'jslib/enums/cipherType';
2019-07-12 21:34:27 +02:00
import { EventType } from 'jslib/enums/eventType';
2018-06-07 23:12:11 +02:00
import { CipherView } from 'jslib/models/view/cipherView';
const MaxCheckedCount = 500;
2018-06-06 23:25:57 +02:00
@Component({
selector: 'app-vault-ciphers',
templateUrl: 'ciphers.component.html',
})
2018-07-28 04:05:03 +02:00
export class CiphersComponent extends BaseCiphersComponent implements OnDestroy {
@Input() showAddNew = true;
2018-06-08 18:04:03 +02:00
@Output() onAttachmentsClicked = new EventEmitter<CipherView>();
2018-06-12 17:46:11 +02:00
@Output() onShareClicked = new EventEmitter<CipherView>();
@Output() onCollectionsClicked = new EventEmitter<CipherView>();
@Output() onCloneClicked = new EventEmitter<CipherView>();
2018-06-07 23:12:11 +02:00
cipherType = CipherType;
actionPromise: Promise<any>;
userHasPremiumAccess = false;
2018-06-07 23:12:11 +02:00
2018-08-13 22:27:17 +02:00
constructor(searchService: SearchService, protected analytics: Angulartics2,
protected toasterService: ToasterService, protected i18nService: I18nService,
2019-07-12 21:34:27 +02:00
protected platformUtilsService: PlatformUtilsService, protected cipherService: CipherService,
protected eventService: EventService, protected totpService: TotpService, protected userService: UserService) {
2018-08-13 22:27:17 +02:00
super(searchService);
2019-03-19 19:55:19 +01:00
this.pageSize = 200;
2018-06-06 23:25:57 +02:00
}
2018-06-07 23:12:11 +02:00
async ngOnInit() {
this.userHasPremiumAccess = await this.userService.canAccessPremium();
}
2018-07-28 04:05:03 +02:00
ngOnDestroy() {
this.selectAll(false);
}
2019-04-26 15:12:37 +02:00
launch(uri: string) {
this.platformUtilsService.eventTrack('Launched Login URI');
2019-04-26 15:12:37 +02:00
this.platformUtilsService.launchUri(uri);
}
2018-06-08 18:04:03 +02:00
attachments(c: CipherView) {
this.onAttachmentsClicked.emit(c);
}
share(c: CipherView) {
2018-06-12 17:46:11 +02:00
this.onShareClicked.emit(c);
2018-06-08 18:04:03 +02:00
}
collections(c: CipherView) {
2018-06-12 17:46:11 +02:00
this.onCollectionsClicked.emit(c);
2018-06-08 18:04:03 +02:00
}
clone(c: CipherView) {
this.onCloneClicked.emit(c);
}
2018-06-12 20:15:19 +02:00
async delete(c: CipherView): Promise<boolean> {
if (this.actionPromise != null) {
return;
}
const permanent = c.isDeleted;
2018-06-12 20:15:19 +02:00
const confirmed = await this.platformUtilsService.showDialog(
this.i18nService.t(permanent ? 'permanentlyDeleteItemConfirmation' : 'deleteItemConfirmation'),
this.i18nService.t(permanent ? 'permanentlyDeleteItem' : 'deleteItem'),
2018-06-12 20:15:19 +02:00
this.i18nService.t('yes'), this.i18nService.t('no'), 'warning');
if (!confirmed) {
return false;
}
try {
this.actionPromise = this.deleteCipher(c.id, permanent);
await this.actionPromise;
this.analytics.eventTrack.next({ action: 'Deleted Cipher' });
this.toasterService.popAsync('success', null, this.i18nService.t(permanent ? 'permanentlyDeletedItem'
: 'deletedItem'));
this.refresh();
} catch { }
this.actionPromise = null;
}
async restore(c: CipherView): Promise<boolean> {
if (this.actionPromise != null || !c.isDeleted) {
return;
}
const confirmed = await this.platformUtilsService.showDialog(
this.i18nService.t('restoreItemConfirmation'),
this.i18nService.t('restoreItem'),
this.i18nService.t('yes'), this.i18nService.t('no'), 'warning');
if (!confirmed) {
return false;
}
try {
this.actionPromise = this.cipherService.restoreWithServer(c.id);
await this.actionPromise;
this.analytics.eventTrack.next({ action: 'Restored Cipher' });
this.toasterService.popAsync('success', null, this.i18nService.t('restoredItem'));
this.refresh();
} catch { }
this.actionPromise = null;
2018-06-08 18:04:03 +02:00
}
async copy(cipher: CipherView, value: string, typeI18nKey: string, aType: string) {
if (value == null || aType === 'TOTP' && !this.displayTotpCopyButton(cipher)) {
2018-06-08 18:04:03 +02:00
return;
} else if (value === cipher.login.totp) {
value = await this.totpService.getCode(value);
2018-06-08 18:04:03 +02:00
}
this.analytics.eventTrack.next({ action: 'Copied ' + aType.toLowerCase() + ' from listing.' });
2018-08-17 18:25:21 +02:00
this.platformUtilsService.copyToClipboard(value, { window: window });
2018-06-08 18:04:03 +02:00
this.toasterService.popAsync('info', null,
this.i18nService.t('valueCopied', this.i18nService.t(typeI18nKey)));
2019-07-12 21:34:27 +02:00
if (typeI18nKey === 'password' || typeI18nKey === 'verificationCodeTotp') {
2019-07-12 21:34:27 +02:00
this.eventService.collect(EventType.Cipher_ClientToggledHiddenFieldVisible, cipher.id);
} else if (typeI18nKey === 'securityCode') {
this.eventService.collect(EventType.Cipher_ClientCopiedCardCode, cipher.id);
}
2018-06-08 18:04:03 +02:00
}
selectAll(select: boolean) {
if (select) {
this.selectAll(false);
}
const selectCount = select && this.ciphers.length > MaxCheckedCount
? MaxCheckedCount
: this.ciphers.length;
for (let i = 0; i < selectCount; i++) {
this.checkCipher(this.ciphers[i], select);
}
}
checkCipher(c: CipherView, select?: boolean) {
(c as any).checked = select == null ? !(c as any).checked : select;
}
getSelected(): CipherView[] {
if (this.ciphers == null) {
return [];
}
return this.ciphers.filter((c) => !!(c as any).checked);
}
getSelectedIds(): string[] {
return this.getSelected().map((c) => c.id);
}
displayTotpCopyButton(cipher: CipherView) {
return (cipher?.login?.hasTotp ?? false) &&
(cipher.organizationUseTotp || this.userHasPremiumAccess);
}
protected deleteCipher(id: string, permanent: boolean) {
return permanent ? this.cipherService.deleteWithServer(id) : this.cipherService.softDeleteWithServer(id);
}
protected showFixOldAttachments(c: CipherView) {
return c.hasOldAttachments && c.organizationId == null;
}
2018-06-06 23:25:57 +02:00
}