Fix shared folders with offline teams

pull/1/head
yflory 4 years ago
parent 058922918d
commit 70b796c406

@ -440,6 +440,7 @@ define([
if (e) { return void cb({error: e}); } if (e) { return void cb({error: e}); }
store.rpc = call; store.rpc = call;
store.onRpcReadyEvt.fire();
Store.getPinLimit(null, null, function (obj) { Store.getPinLimit(null, null, function (obj) {
if (obj.error) { console.error(obj.error); } if (obj.error) { console.error(obj.error); }
@ -1598,6 +1599,50 @@ define([
}); });
}; };
Store.onRejected = function (allowed, _cb) {
var cb = Util.once(Util.mkAsync(_cb));
// There is an allow list: check if we can authenticate
if (!Array.isArray(allowed)) { return void cb('EINVAL'); }
if (!store.loggedIn || !store.proxy.edPublic) { return void cb('EFORBIDDEN'); }
var teamModule = store.modules['team'];
var teams = (teamModule && teamModule.getTeams()) || [];
var _store;
if (allowed.indexOf(store.proxy.edPublic) !== -1) {
// We are allowed: use our own rpc
_store = store;
} else if (teams.some(function (teamId) {
// We're not allowed: check our teams
var ed = Util.find(store, ['proxy', 'teams', teamId, 'keys', 'drive', 'edPublic']);
if (allowed.indexOf(ed) === -1) { return false; }
// This team is allowed: use its rpc
var t = teamModule.getTeam(teamId);
_store = t;
return true;
})) {}
var auth = function () {
var rpc = _store.rpc;
if (!rpc) { return void cb('EFORBIDDEN'); }
rpc.send('COOKIE', '', function (err) {
cb(err);
});
}
// Wait for the RPC we need to be ready and then tyr to authenticate
if (_store.onRpcReadyEvt) {
_store.onRpcReadyEvt.reg(function () {
auth();
});
return;
}
// Fall back to the old system in case onRpcReadyEvt doesn't exist (shouldn't happen)
auth();
};
Store.joinPad = function (clientId, data) { Store.joinPad = function (clientId, data) {
if (data.versionHash) { if (data.versionHash) {
return void getVersionHash(clientId, data); return void getVersionHash(clientId, data);
@ -1701,37 +1746,7 @@ define([
}, },
onError: onError, onError: onError,
onChannelError: onError, onChannelError: onError,
onRejected: function (allowed, _cb) { onRejected: Store.onRejected,
var cb = Util.once(Util.mkAsync(_cb));
// There is an allow list: check if we can authenticate
if (!Array.isArray(allowed)) { return void cb('EINVAL'); }
if (!store.loggedIn || !store.proxy.edPublic) { return void cb('EFORBIDDEN'); }
onReadyEvt.reg(function () {
var rpc;
var teamModule = store.modules['team'];
var teams = (teamModule && teamModule.getTeams()) || [];
if (allowed.indexOf(store.proxy.edPublic) !== -1) {
// We are allowed: use our own rpc
rpc = store.rpc;
} else if (teams.some(function (teamId) {
// We're not allowed: check our teams
var ed = Util.find(store, ['proxy', 'teams', teamId, 'keys', 'drive', 'edPublic']);
if (allowed.indexOf(ed) === -1) { return false; }
// This team is allowed: use its rpc
var t = teamModule.getTeam(teamId);
rpc = t.rpc;
return true;
})) {}
if (!rpc) { return void cb('EFORBIDDEN'); }
rpc.send('COOKIE', '', function (err) {
cb(err);
});
});
},
onConnectionChange: function (info) { onConnectionChange: function (info) {
if (!info.state) { if (!info.state) {
channel.bcast("PAD_DISCONNECT"); channel.bcast("PAD_DISCONNECT");
@ -2589,7 +2604,7 @@ define([
progress: 100*obj.progress/obj.max progress: 100*obj.progress/obj.max
}; };
postMessage(clientId, 'LOADING_DRIVE', data); postMessage(clientId, 'LOADING_DRIVE', data);
}); }, true);
}).nThen(function (waitFor) { }).nThen(function (waitFor) {
loadUniversal(Team, 'team', waitFor, clientId); loadUniversal(Team, 'team', waitFor, clientId);
}).nThen(function () { }).nThen(function () {
@ -2782,6 +2797,7 @@ define([
var rt = window.rt = Listmap.create(listmapConfig); var rt = window.rt = Listmap.create(listmapConfig);
store.driveSecret = secret; store.driveSecret = secret;
store.proxy = rt.proxy; store.proxy = rt.proxy;
store.onRpcReadyEvt = Util.mkEvent(true);
store.loggedIn = typeof(data.userHash) !== "undefined"; store.loggedIn = typeof(data.userHash) !== "undefined";
var returned = {}; var returned = {};

@ -107,15 +107,12 @@ define([
// If we try to load an existing shared folder (isNew === false) but this folder // If we try to load an existing shared folder (isNew === false) but this folder
// doesn't exist in the database, abort and cb // doesn't exist in the database, abort and cb
nThen(function (waitFor) { nThen(function (waitFor) {
// XXX use a config.cache flag in the new branches // If we're in onCacheReady, make sure we have a cache for this shared folder
// If we don't have a network yet and we're pulling our own SF (no team id) if (config.cache) {
// Make sure we have a cache
if (!config.store.id && !config.store.network) {
Cache.getChannelCache(secret.channel, waitFor(function (err, res) { Cache.getChannelCache(secret.channel, waitFor(function (err, res) {
if (err === "EINVAL") { // Cache not found if (err === "EINVAL") { // Cache not found
waitFor.abort(); waitFor.abort();
store.manager.restrictedProxy(id, secret.channel); store.manager.restrictedProxy(id, secret.channel);
// XXX unrestrict when we connect?
return void cb(null); return void cb(null);
} }
})); }));
@ -178,16 +175,6 @@ define([
readOnly: !Boolean(secondaryKey) readOnly: !Boolean(secondaryKey)
}; };
// If there is an allow list and we're offline, try again when we're synced
var onRejected = function (allowed, _cb) {
var cb = Util.once(Util.mkAsync(_cb));
if (store.offline && config.Store) {
config.Store.onReadyEvt.reg(cb);
return;
}
cb('ERESTRICTED');
};
var owners = data.owners; var owners = data.owners;
var listmapConfig = { var listmapConfig = {
data: {}, data: {},
@ -204,7 +191,7 @@ define([
validateKey: secret.keys.validateKey || undefined, validateKey: secret.keys.validateKey || undefined,
owners: owners owners: owners
}, },
onRejected: onRejected // XXX not working onRejected: config.Store && config.Store.onRejected
}; };
var rt = sf.rt = Listmap.create(listmapConfig); var rt = sf.rt = Listmap.create(listmapConfig);
rt.proxy.on('cacheready', function () { rt.proxy.on('cacheready', function () {
@ -368,7 +355,7 @@ define([
- userObject: userObject associated to the main drive - userObject: userObject associated to the main drive
- handler: a function (sfid, rt) called for each shared folder loaded - handler: a function (sfid, rt) called for each shared folder loaded
*/ */
SF.loadSharedFolders = function (Store, network, store, userObject, waitFor, progress) { SF.loadSharedFolders = function (Store, network, store, userObject, waitFor, progress, cache) {
var shared = Util.find(store.proxy, ['drive', UserObject.SHARED_FOLDERS]) || {}; var shared = Util.find(store.proxy, ['drive', UserObject.SHARED_FOLDERS]) || {};
var steps = Object.keys(shared).length; var steps = Object.keys(shared).length;
var i = 1; var i = 1;
@ -381,6 +368,7 @@ define([
network: network, network: network,
store: store, store: store,
Store: Store, Store: Store,
cache: cache,
isNewChannel: Store.isNewChannel isNewChannel: Store.isNewChannel
}, id, sf, waitFor(function () { }, id, sf, waitFor(function () {
progress({ progress({

@ -184,6 +184,7 @@ define([
Pinpad.create(ctx.store.network, data, function (e, call) { Pinpad.create(ctx.store.network, data, function (e, call) {
if (e) { return void cb(e); } if (e) { return void cb(e); }
team.rpc = call; team.rpc = call;
team.onRpcReadyEvt.fire();
cb(); cb();
}); });
}); });
@ -202,6 +203,7 @@ define([
handleSharedFolder: function (sfId, rt) { handleSharedFolder(ctx, id, sfId, rt); }, handleSharedFolder: function (sfId, rt) { handleSharedFolder(ctx, id, sfId, rt); },
sharedFolders: {}, // equivalent of store.sharedFolders in async-store sharedFolders: {}, // equivalent of store.sharedFolders in async-store
roster: roster, roster: roster,
onRpcReadyEvt: Util.mkEvent(true),
offline: true offline: true
}; };
ctx.cache[id] = team; ctx.cache[id] = team;
@ -334,7 +336,7 @@ define([
ctx.updateProgress({ ctx.updateProgress({
progress: ctx.progress progress: ctx.progress
}); });
}); }, true);
}).nThen(function () { }).nThen(function () {
cb(); cb();
}); });

Loading…
Cancel
Save