This repository has been archived by the owner on Dec 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
credentials.js
95 lines (81 loc) · 2.71 KB
/
credentials.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* jshint module: true */
import events from './events.js';
function ensure(instance, data) {
if (!instance._credentials) {
const json = instance.appStorage.getItem(instance.key) || '{}';
console.log(`credentials initialized with: ${json}`);
instance._credentials = JSON.parse(json);
instance._credentials.Servers = instance._credentials.Servers || [];
}
}
function set(instance, data) {
instance._credentials = data;
const json = JSON.stringify(data);
instance.appStorage.setItem(instance.key, json);
events.trigger(instance, 'credentialsupdated', [{
credentials: data,
credentialsJson: json
}]);
}
export default class Credentials {
constructor(appStorage, key) {
this.key = key || 'servercredentials3';
this.appStorage = appStorage;
}
clear() {
this._credentials = null;
this.appStorage.removeItem(this.key);
}
credentials(data) {
if (data) {
set(this, data);
}
ensure(this);
return this._credentials;
}
addOrUpdateServer(list, server) {
if (!server.Id) {
throw new Error('Server.Id cannot be null or empty');
}
const existing = list.filter(({ Id }) => Id === server.Id)[0];
if (existing) {
// Merge the data
existing.DateLastAccessed = Math.max(
existing.DateLastAccessed || 0,
server.DateLastAccessed || 0
);
if (server.AccessToken) {
existing.AccessToken = server.AccessToken;
existing.UserId = server.UserId;
}
if (server.ExchangeToken) {
existing.ExchangeToken = server.ExchangeToken;
}
if (server.RemoteAddress) {
existing.RemoteAddress = server.RemoteAddress;
}
if (server.ManualAddress) {
existing.ManualAddress = server.ManualAddress;
}
if (server.LocalAddress) {
existing.LocalAddress = server.LocalAddress;
}
if (server.Name) {
existing.Name = server.Name;
}
if (server.WakeOnLanInfos && server.WakeOnLanInfos.length) {
existing.WakeOnLanInfos = server.WakeOnLanInfos;
}
if (server.LastConnectionMode != null) {
existing.LastConnectionMode = server.LastConnectionMode;
}
if (server.ConnectServerId) {
existing.ConnectServerId = server.ConnectServerId;
}
return existing;
} else {
list.push(server);
return server;
}
}
}