2021-04-29 13:31:21 +02:00
|
|
|
import { CipherRepromptType } from "../../enums/cipherRepromptType";
|
2018-02-19 19:07:19 +01:00
|
|
|
import { CipherType } from "../../enums/cipherType";
|
2021-11-02 23:03:37 +01:00
|
|
|
import { LinkedIdType } from "../../enums/linkedIdType";
|
2018-02-19 19:07:19 +01:00
|
|
|
import { Cipher } from "../domain/cipher";
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2018-01-24 17:33:15 +01:00
|
|
|
import { AttachmentView } from "./attachmentView";
|
|
|
|
import { CardView } from "./cardView";
|
|
|
|
import { FieldView } from "./fieldView";
|
|
|
|
import { IdentityView } from "./identityView";
|
|
|
|
import { LoginView } from "./loginView";
|
2018-07-27 22:44:20 +02:00
|
|
|
import { PasswordHistoryView } from "./passwordHistoryView";
|
2018-01-24 17:33:15 +01:00
|
|
|
import { SecureNoteView } from "./secureNoteView";
|
|
|
|
import { View } from "./view";
|
|
|
|
|
|
|
|
export class CipherView implements View {
|
2019-01-25 15:30:21 +01:00
|
|
|
id: string = null;
|
|
|
|
organizationId: string = null;
|
|
|
|
folderId: string = null;
|
|
|
|
name: string = null;
|
|
|
|
notes: string = null;
|
|
|
|
type: CipherType = null;
|
2018-06-25 20:15:34 +02:00
|
|
|
favorite = false;
|
|
|
|
organizationUseTotp = false;
|
2019-01-25 15:30:21 +01:00
|
|
|
edit = false;
|
|
|
|
viewPassword = true;
|
|
|
|
localData: any;
|
|
|
|
login = new LoginView();
|
2018-01-24 17:33:15 +01:00
|
|
|
identity = new IdentityView();
|
|
|
|
card = new CardView();
|
2018-06-20 05:40:10 +02:00
|
|
|
secureNote = new SecureNoteView();
|
2020-05-21 15:49:56 +02:00
|
|
|
attachments: AttachmentView[] = null;
|
2019-01-25 15:30:21 +01:00
|
|
|
fields: FieldView[] = null;
|
2020-05-21 15:49:56 +02:00
|
|
|
passwordHistory: PasswordHistoryView[] = null;
|
2018-01-24 17:33:15 +01:00
|
|
|
collectionIds: string[] = null;
|
2018-07-28 05:37:36 +02:00
|
|
|
revisionDate: Date = null;
|
2020-04-03 22:32:15 +02:00
|
|
|
deletedDate: Date = null;
|
2021-07-02 20:53:14 +02:00
|
|
|
reprompt: CipherRepromptType = CipherRepromptType.None;
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2018-01-24 22:58:34 +01:00
|
|
|
constructor(c?: Cipher) {
|
2018-01-24 19:27:32 +01:00
|
|
|
if (!c) {
|
2021-11-02 23:03:37 +01:00
|
|
|
return;
|
2018-01-24 17:33:15 +01:00
|
|
|
}
|
2018-01-25 05:27:04 +01:00
|
|
|
|
2018-01-24 17:33:15 +01:00
|
|
|
this.id = c.id;
|
|
|
|
this.organizationId = c.organizationId;
|
|
|
|
this.folderId = c.folderId;
|
|
|
|
this.favorite = c.favorite;
|
2018-06-20 05:40:10 +02:00
|
|
|
this.organizationUseTotp = c.organizationUseTotp;
|
|
|
|
this.edit = c.edit;
|
2018-07-28 05:37:36 +02:00
|
|
|
this.viewPassword = c.viewPassword;
|
2018-01-24 17:33:15 +01:00
|
|
|
this.type = c.type;
|
|
|
|
this.localData = c.localData;
|
2021-11-02 23:03:37 +01:00
|
|
|
this.collectionIds = c.collectionIds;
|
2018-07-31 04:01:21 +02:00
|
|
|
this.revisionDate = c.revisionDate;
|
2020-04-03 22:32:15 +02:00
|
|
|
this.deletedDate = c.deletedDate;
|
2021-11-02 23:03:37 +01:00
|
|
|
// Old locally stored ciphers might have reprompt == null. If so set it to None.
|
2021-05-05 09:54:17 +02:00
|
|
|
this.reprompt = c.reprompt ?? CipherRepromptType.None;
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2021-11-02 23:03:37 +01:00
|
|
|
private get item() {
|
2018-01-24 22:58:34 +01:00
|
|
|
switch (this.type) {
|
2021-11-02 23:03:37 +01:00
|
|
|
case CipherType.Login:
|
2018-07-31 04:01:21 +02:00
|
|
|
return this.login;
|
2018-01-24 22:58:34 +01:00
|
|
|
case CipherType.SecureNote:
|
2021-11-02 23:03:37 +01:00
|
|
|
return this.secureNote;
|
2018-01-24 22:58:34 +01:00
|
|
|
case CipherType.Card:
|
2021-11-02 23:03:37 +01:00
|
|
|
return this.card;
|
|
|
|
case CipherType.Identity:
|
|
|
|
return this.identity;
|
2021-12-16 13:36:21 +01:00
|
|
|
default:
|
|
|
|
break;
|
2021-11-02 23:03:37 +01:00
|
|
|
}
|
|
|
|
|
2018-07-27 22:44:20 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-25 05:27:04 +01:00
|
|
|
get subTitle(): string {
|
|
|
|
return this.item.subTitle;
|
|
|
|
}
|
|
|
|
|
2018-11-14 21:19:59 +01:00
|
|
|
get hasPasswordHistory(): boolean {
|
|
|
|
return this.passwordHistory && this.passwordHistory.length > 0;
|
|
|
|
}
|
|
|
|
|
2018-01-25 05:27:04 +01:00
|
|
|
get hasAttachments(): boolean {
|
|
|
|
return this.attachments && this.attachments.length > 0;
|
|
|
|
}
|
2018-02-28 21:15:10 +01:00
|
|
|
|
2018-07-28 05:37:36 +02:00
|
|
|
get hasOldAttachments(): boolean {
|
|
|
|
if (this.hasAttachments) {
|
2019-01-25 15:30:21 +01:00
|
|
|
for (let i = 0; i < this.attachments.length; i++) {
|
|
|
|
if (this.attachments[i].key == null) {
|
2018-07-28 05:37:36 +02:00
|
|
|
return true;
|
|
|
|
}
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
2018-07-28 05:37:36 +02:00
|
|
|
}
|
2020-04-03 22:32:15 +02:00
|
|
|
return false;
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2020-04-03 22:32:15 +02:00
|
|
|
get hasFields(): boolean {
|
|
|
|
return this.fields && this.fields.length > 0;
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2020-04-03 22:32:15 +02:00
|
|
|
get passwordRevisionDisplayDate(): Date {
|
|
|
|
if (this.type !== CipherType.Login || this.login == null) {
|
|
|
|
return null;
|
2018-07-28 05:37:36 +02:00
|
|
|
} else if (this.login.password == null || this.login.password === "") {
|
|
|
|
return null;
|
2020-04-03 22:32:15 +02:00
|
|
|
}
|
2018-07-31 04:01:21 +02:00
|
|
|
return this.login.passwordRevisionDate;
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
2021-11-02 23:03:37 +01:00
|
|
|
|
|
|
|
get isDeleted(): boolean {
|
|
|
|
return this.deletedDate != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
get linkedFieldOptions() {
|
|
|
|
return this.item.linkedFieldOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
linkedFieldValue(id: LinkedIdType) {
|
|
|
|
const linkedFieldOption = this.linkedFieldOptions?.get(id);
|
|
|
|
if (linkedFieldOption == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const item = this.item;
|
|
|
|
return this.item[linkedFieldOption.propertyKey as keyof typeof item];
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
|
2021-11-02 23:03:37 +01:00
|
|
|
linkedFieldI18nKey(id: LinkedIdType): string {
|
|
|
|
return this.linkedFieldOptions.get(id)?.i18nKey;
|
|
|
|
}
|
2022-06-24 02:38:55 +02:00
|
|
|
|
2022-06-29 02:23:01 +02:00
|
|
|
toJSON(): any {
|
|
|
|
const result: any = {
|
|
|
|
id: this.id,
|
|
|
|
organizationId: this.organizationId,
|
|
|
|
folderId: this.folderId,
|
|
|
|
name: this.name,
|
|
|
|
notes: this.notes,
|
|
|
|
type: this.type,
|
|
|
|
favorite: this.favorite,
|
|
|
|
organizationUseTotp: this.organizationUseTotp,
|
|
|
|
edit: this.edit,
|
|
|
|
viewPassword: this.viewPassword,
|
|
|
|
localData: this.localData,
|
|
|
|
collectionIds: this.collectionIds,
|
|
|
|
reprompt: this.reprompt,
|
|
|
|
|
|
|
|
attachments: this.attachments,
|
|
|
|
fields: this.fields,
|
|
|
|
passwordHistory: this.passwordHistory,
|
|
|
|
|
|
|
|
revisionDate: this.revisionDate,
|
|
|
|
deletedDate: this.deletedDate,
|
2022-06-28 03:36:45 +02:00
|
|
|
};
|
2022-06-24 02:38:55 +02:00
|
|
|
|
|
|
|
switch (this.type) {
|
|
|
|
case CipherType.Card:
|
2022-06-29 02:23:01 +02:00
|
|
|
result.card = this.card;
|
2022-06-24 02:38:55 +02:00
|
|
|
break;
|
|
|
|
case CipherType.Identity:
|
2022-06-29 02:23:01 +02:00
|
|
|
result.identity = this.identity;
|
2022-06-24 02:38:55 +02:00
|
|
|
break;
|
|
|
|
case CipherType.Login:
|
2022-06-29 02:23:01 +02:00
|
|
|
result.login = this.login;
|
2022-06-24 02:38:55 +02:00
|
|
|
break;
|
|
|
|
case CipherType.SecureNote:
|
2022-06-29 02:23:01 +02:00
|
|
|
result.secureNote = this.secureNote;
|
2022-06-24 02:38:55 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-06-29 02:23:01 +02:00
|
|
|
return result;
|
2022-06-24 02:38:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static fromJSON(obj: any): CipherView {
|
2022-06-29 02:23:01 +02:00
|
|
|
const view = new CipherView();
|
|
|
|
view.id = obj.id;
|
|
|
|
view.organizationId = obj.organizationId;
|
|
|
|
view.folderId = obj.folderId;
|
|
|
|
view.name = obj.name;
|
|
|
|
view.notes = obj.notes;
|
|
|
|
view.type = obj.type;
|
|
|
|
view.favorite = obj.favorite;
|
|
|
|
view.organizationUseTotp = obj.organizationUseTotp;
|
|
|
|
view.edit = obj.edit;
|
|
|
|
view.viewPassword = obj.viewPassword;
|
|
|
|
view.localData = obj.localData;
|
|
|
|
view.collectionIds = obj.collectionIds;
|
|
|
|
view.reprompt = obj.reprompt;
|
2022-06-24 02:38:55 +02:00
|
|
|
|
|
|
|
// Dates
|
2022-06-28 00:17:42 +02:00
|
|
|
view.revisionDate = obj.revisionDate == null ? null : new Date(obj.revisionDate);
|
|
|
|
view.deletedDate = obj.deletedDate == null ? null : new Date(obj.deletedDate);
|
2022-06-24 02:38:55 +02:00
|
|
|
|
|
|
|
// Nested objects
|
|
|
|
view.attachments = obj.attachments?.map((a: any) => AttachmentView.fromJSON(a));
|
|
|
|
view.fields = obj.fields?.map((f: any) => FieldView.fromJSON(f));
|
|
|
|
view.passwordHistory = obj.passwordHistory?.map((ph: any) => PasswordHistoryView.fromJSON(ph));
|
|
|
|
|
|
|
|
switch (view.type) {
|
|
|
|
case CipherType.Card:
|
|
|
|
view.card = CardView.fromJSON(obj.card);
|
|
|
|
break;
|
|
|
|
case CipherType.Identity:
|
2022-06-24 04:36:43 +02:00
|
|
|
view.identity = IdentityView.fromJSON(obj.identity);
|
2022-06-24 02:38:55 +02:00
|
|
|
break;
|
|
|
|
case CipherType.Login:
|
2022-06-24 04:36:43 +02:00
|
|
|
view.login = LoginView.fromJSON(obj.login);
|
2022-06-24 02:38:55 +02:00
|
|
|
break;
|
|
|
|
case CipherType.SecureNote:
|
|
|
|
view.secureNote = SecureNoteView.fromJSON(obj.secureNote);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
2018-01-24 17:33:15 +01:00
|
|
|
}
|