2016-01-28 13:45:52 -07:00
|
|
|
|
define(['events', 'appStorage'], function (Events, appStorage) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-12-23 10:46:01 -07:00
|
|
|
|
return function (key) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
var self = this;
|
2015-06-08 14:32:20 -07:00
|
|
|
|
var credentials = null;
|
2015-06-13 07:46:59 -07:00
|
|
|
|
key = key || 'servercredentials3';
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
|
|
|
|
function ensure() {
|
|
|
|
|
|
2015-06-08 14:32:20 -07:00
|
|
|
|
if (!credentials) {
|
2015-09-10 11:28:22 -07:00
|
|
|
|
|
2015-06-08 14:32:20 -07:00
|
|
|
|
var json = appStorage.getItem(key) || '{}';
|
2015-05-28 19:53:05 -07:00
|
|
|
|
|
2015-12-23 10:46:01 -07:00
|
|
|
|
console.log('credentials initialized with: ' + json);
|
2015-06-08 14:32:20 -07:00
|
|
|
|
credentials = JSON.parse(json);
|
2015-07-24 14:44:25 -07:00
|
|
|
|
credentials.Servers = credentials.Servers || [];
|
2015-06-08 14:32:20 -07:00
|
|
|
|
}
|
2014-10-23 21:54:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function get() {
|
|
|
|
|
|
|
|
|
|
ensure();
|
|
|
|
|
return credentials;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function set(data) {
|
2015-02-16 13:56:57 -07:00
|
|
|
|
|
2014-12-03 22:24:41 -07:00
|
|
|
|
if (data) {
|
|
|
|
|
credentials = data;
|
2015-05-20 10:29:26 -07:00
|
|
|
|
appStorage.setItem(key, JSON.stringify(data));
|
2014-12-03 22:24:41 -07:00
|
|
|
|
} else {
|
|
|
|
|
self.clear();
|
|
|
|
|
}
|
2015-05-28 16:37:43 -07:00
|
|
|
|
|
|
|
|
|
Events.trigger(self, 'credentialsupdated');
|
2014-10-23 21:54:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-27 14:45:50 -07:00
|
|
|
|
self.clear = function () {
|
2014-10-25 11:32:58 -07:00
|
|
|
|
credentials = null;
|
2015-05-20 10:29:26 -07:00
|
|
|
|
appStorage.removeItem(key);
|
2014-10-25 11:32:58 -07:00
|
|
|
|
};
|
|
|
|
|
|
2014-10-23 21:54:35 -07:00
|
|
|
|
self.credentials = function (data) {
|
|
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
|
set(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return get();
|
|
|
|
|
};
|
|
|
|
|
|
2014-10-27 14:45:50 -07:00
|
|
|
|
self.addOrUpdateServer = function (list, server) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-05-24 11:33:28 -07:00
|
|
|
|
if (!server.Id) {
|
|
|
|
|
throw new Error('Server.Id cannot be null or empty');
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-27 14:45:50 -07:00
|
|
|
|
var existing = list.filter(function (s) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
return s.Id == server.Id;
|
|
|
|
|
})[0];
|
|
|
|
|
|
2014-10-27 14:45:50 -07:00
|
|
|
|
if (existing) {
|
2015-02-16 13:56:57 -07:00
|
|
|
|
|
2014-10-23 21:54:35 -07:00
|
|
|
|
// Merge the data
|
2015-02-16 13:56:57 -07:00
|
|
|
|
existing.DateLastAccessed = Math.max(existing.DateLastAccessed || 0, server.DateLastAccessed || 0);
|
2014-10-23 21:54:35 -07:00
|
|
|
|
|
2015-06-08 14:32:20 -07:00
|
|
|
|
existing.UserLinkType = server.UserLinkType;
|
|
|
|
|
|
2014-10-27 14:45:50 -07:00
|
|
|
|
if (server.AccessToken) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
existing.AccessToken = server.AccessToken;
|
|
|
|
|
existing.UserId = server.UserId;
|
|
|
|
|
}
|
2014-10-27 14:45:50 -07:00
|
|
|
|
if (server.ExchangeToken) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
existing.ExchangeToken = server.ExchangeToken;
|
|
|
|
|
}
|
2014-10-27 14:45:50 -07:00
|
|
|
|
if (server.RemoteAddress) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
existing.RemoteAddress = server.RemoteAddress;
|
|
|
|
|
}
|
2015-06-04 13:27:46 -07:00
|
|
|
|
if (server.ManualAddress) {
|
|
|
|
|
existing.ManualAddress = server.ManualAddress;
|
|
|
|
|
}
|
2014-10-27 14:45:50 -07:00
|
|
|
|
if (server.LocalAddress) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
existing.LocalAddress = server.LocalAddress;
|
|
|
|
|
}
|
2014-10-27 14:45:50 -07:00
|
|
|
|
if (server.Name) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
existing.Name = server.Name;
|
|
|
|
|
}
|
2014-10-27 14:45:50 -07:00
|
|
|
|
if (server.WakeOnLanInfos && server.WakeOnLanInfos.length) {
|
2014-10-23 21:54:35 -07:00
|
|
|
|
existing.WakeOnLanInfos = server.WakeOnLanInfos;
|
|
|
|
|
}
|
2015-05-24 11:33:28 -07:00
|
|
|
|
if (server.LastConnectionMode != null) {
|
|
|
|
|
existing.LastConnectionMode = server.LastConnectionMode;
|
|
|
|
|
}
|
2015-08-29 17:40:52 -07:00
|
|
|
|
if (server.ConnectServerId) {
|
|
|
|
|
existing.ConnectServerId = server.ConnectServerId;
|
|
|
|
|
}
|
2015-05-24 11:33:28 -07:00
|
|
|
|
|
|
|
|
|
return existing;
|
2014-10-23 21:54:35 -07:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
list.push(server);
|
2015-05-24 11:33:28 -07:00
|
|
|
|
return server;
|
2014-10-23 21:54:35 -07:00
|
|
|
|
}
|
|
|
|
|
};
|
2015-09-10 11:28:22 -07:00
|
|
|
|
|
|
|
|
|
self.addOrUpdateUser = function (server, user) {
|
|
|
|
|
|
|
|
|
|
server.Users = server.Users || [];
|
|
|
|
|
|
|
|
|
|
var existing = server.Users.filter(function (s) {
|
|
|
|
|
return s.Id == user.Id;
|
|
|
|
|
})[0];
|
|
|
|
|
|
|
|
|
|
if (existing) {
|
|
|
|
|
|
|
|
|
|
// Merge the data
|
|
|
|
|
existing.IsSignedInOffline = true;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
server.Users.push(user);
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-10-23 21:54:35 -07:00
|
|
|
|
};
|
2015-12-23 10:46:01 -07:00
|
|
|
|
});
|