1
0
Fork 0
mirror of synced 2024-07-06 23:10:57 +12:00
budibase/packages/server/utilities/masterAppInternal.js

244 lines
8 KiB
JavaScript
Raw Normal View History

2019-06-14 21:05:46 +12:00
const {getApisWithFullAccess, getApisForSession} = require("./budibaseApi");
const getDatastore = require("./datastore");
const getDatabaseManager = require("./databaseManager");
2019-06-26 09:48:22 +12:00
const {$, splitKey} = require("budibase-core").common;
const { keyBy, last } = require("lodash/fp");
2019-06-14 21:05:46 +12:00
const {unauthorized} = require("./exceptions");
2019-06-26 09:48:22 +12:00
const { masterAppPackage, applictionVersionPackage } = require("../utilities/createAppPackage");
2019-06-14 21:05:46 +12:00
const isMaster = appname => appname === "_master";
module.exports = async (config) => {
2019-06-15 04:01:01 +12:00
const datastoreModule = getDatastore(config);
2019-06-14 21:05:46 +12:00
const databaseManager = getDatabaseManager(
2019-06-15 04:01:01 +12:00
datastoreModule,
2019-06-14 21:05:46 +12:00
config.datastoreConfig);
2019-06-15 04:01:01 +12:00
const masterDatastore = datastoreModule.getDatastore(
databaseManager.masterDatastoreConfig);
2019-06-26 09:48:22 +12:00
const bb = await getApisWithFullAccess(
masterDatastore, masterAppPackage(config));
2019-06-14 21:05:46 +12:00
let applications;
const loadApplications = async () =>
applications = $(await bb.indexApi.listItems("/all_applications"), [
keyBy("name")
]);
await loadApplications();
2019-06-15 04:01:01 +12:00
const getInstanceDatastore = (instanceDatastoreConfig) =>
datastoreModule.getDatastore(instanceDatastoreConfig);
2019-06-14 21:05:46 +12:00
const getCustomSessionId = (appname, sessionId) =>
isMaster(appname)
? bb.recordApi.customId("mastersession", sessionId)
: bb.recordApi.customId("session", sessionId);
const getApplication = async (name) => {
if(applications[name])
return applications[name];
await loadApplications();
return applications[name];
};
const getSession = async (sessionId, appname) => {
const customSessionId = getCustomSessionId(appname, sessionId);
if(isMaster(appname)) {
return await bb.recordApi.load(`/sessions/${customSessionId}`);
}
else {
const app = await getApplication(appname);
return await bb.recordApi.load(`/applications/${app.id}/sessions/${customSessionId}`);
}
};
const deleteSession = async (sessionId, appname) => {
const customSessionId = getCustomSessionId(appname, sessionId);
if(isMaster(appname)) {
return await bb.recordApi.delete(`/sessions/${customSessionId}`);
}
else {
const app = await getApplication(appname);
return await bb.recordApi.delete(`/applications/${app.id}/sessions/${customSessionId}`);
}
};
const authenticate = async (sessionId, appname, username, password, instanceName="default") => {
if(isMaster(appname)) {
const authUser = await bb.authApi.authenticate(username, password);
if(!authUser) {
return null;
}
const session = bb.recordApi.getNew("/sessions", "mastersession");
bb.recordApi.setCustomId(session, sessionId);
session.user_json = JSON.stringify(authUser);
2019-06-20 09:05:53 +12:00
session.username = username;
2019-06-14 21:05:46 +12:00
await bb.recordApi.save(session);
return session;
}
const app = await getApplication(appname);
const userInMaster = await getUser(bb, app.id, username);
2019-07-07 20:03:37 +12:00
if(!userInMaster) return null;
2019-06-14 21:05:46 +12:00
const instance = await bb.recordApi.load(
userInMaster.instanceKey);
2019-06-14 21:05:46 +12:00
2019-06-26 09:48:22 +12:00
const versionId = $(instance.version.key, [
splitKey,
last
]);
const dsConfig = JSON.parse(instance.datastoreconfig);
2019-06-14 21:05:46 +12:00
const bbInstance = await getApisWithFullAccess(
datastoreModule.getDatastore(dsConfig),
applictionVersionPackage(config, appname, versionId)
2019-06-26 09:48:22 +12:00
);
2019-06-14 21:05:46 +12:00
const authUser = await bbInstance.authApi.authenticate(username, password);
if(!authUser) {
return null;
}
const session = bb.recordApi.getNew(`/applications/${app.id}/sessions`, "session");
bb.recordApi.setCustomId(session, sessionId);
session.user_json = JSON.stringify(authUser);
session.instanceDatastoreConfig = instance.datastoreconfig;
2019-06-20 09:05:53 +12:00
session.username = username;
2019-07-07 20:03:37 +12:00
session.instanceVersion = instance.version.key;
2019-06-14 21:05:46 +12:00
await bb.recordApi.save(session);
return session;
};
const getInstanceApiForSession = async (appname, sessionId) => {
if(isMaster(appname)) {
2019-06-15 04:01:01 +12:00
const customId = bb.recordApi.customId("mastersession", sessionId);
2019-06-22 01:00:24 +12:00
try {
const session = await bb.recordApi.load(`/sessions/${customId}`);
2019-06-26 09:48:22 +12:00
return await getApisForSession(
masterDatastore,
masterAppPackage(config),
session);
2019-06-22 01:00:24 +12:00
} catch(_) {
return null;
}
2019-06-14 21:05:46 +12:00
}
else {
const app = await getApplication(appname);
const customId = bb.recordApi.customId("session", sessionId);
2019-06-22 01:00:24 +12:00
try {
const session = await bb.recordApi.load(`/applications/${app.id}/sessions/${customId}`);
const dsConfig = JSON.parse(session.instanceDatastoreConfig);
const instanceDatastore = getInstanceDatastore(dsConfig)
2019-07-07 20:03:37 +12:00
const versionId = $(session.instanceVersion,[
splitKey,
last
]);
2019-06-26 09:48:22 +12:00
return await getApisForSession(
instanceDatastore,
2019-07-07 20:03:37 +12:00
applictionVersionPackage(config, appname, versionId),
2019-06-26 09:48:22 +12:00
session);
2019-06-22 01:00:24 +12:00
} catch(_) {
return null;
}
2019-06-14 21:05:46 +12:00
}
};
const getUser = async (bb, appId, username ) => {
const matches = await bb.indexApi.listItems(
`/applications/${appId}/user_name_lookup`,
{
rangeStartParams:{name:username},
rangeEndParams:{name:username},
searchPhrase:`name:${username}`
}
);
if(matches.length !== 1) return;
return matches[0];
}
const getFullAccessInstanceApiForUsername = async (appname, username) => {
if(isMaster(appname)) {
2019-06-27 09:29:28 +12:00
return bb;
}
else {
const app = await getApplication(appname);
const user = await getUser(bb, app.id, username);
2019-07-07 20:03:37 +12:00
if(!user) return null;
const dsConfig = JSON.parse(user.instanceDatastoreConfig);
const instanceDatastore = getInstanceDatastore(
dsConfig
);
const versionId = $((await bb.recordApi.load(user.instanceKey)).version.key, [
splitKey,
last
]);
return await getApisWithFullAccess(
instanceDatastore,
applictionVersionPackage(config, appname, versionId));
}
};
2019-06-14 21:05:46 +12:00
2019-06-20 09:05:53 +12:00
const removeSessionsForUser = async (appname, username) => {
if(isMaster(appname)) {
const sessions = await bb.indexApi.listItems(
"/mastersessions_by_user",
{
2019-06-21 19:42:37 +12:00
rangeStartParams:{username},
2019-06-22 01:00:24 +12:00
rangeEndParams:{username},
searchPhrase:`username:${username}`
2019-06-20 09:05:53 +12:00
}
);
for(let session of sessions) {
await bb.recordApi.delete(session.key);
}
}
else {
const app = await getApplication(appname);
const sessions = await bb.indexApi.listItems(
`/applications/${app.id}/sessions_by_user`,
{
rangeStartParams:{name:username},
rangeEndParams:{name:username},
searchPhrase:`username:${username}`
}
);
for(let session of sessions) {
await bb.recordApi.delete(session.key);
}
}
}
2019-06-14 21:05:46 +12:00
return ({
getApplication,
getSession,
deleteSession,
authenticate,
getInstanceApiForSession,
getFullAccessInstanceApiForUsername,
2019-06-26 09:48:22 +12:00
removeSessionsForUser,
bbMaster:bb
2019-06-14 21:05:46 +12:00
});
}