budibase/packages/server/utilities/budibaseApi.js

85 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-06-07 15:18:10 +02:00
const crypto = require("../nodeCrypto");
2019-06-25 23:48:22 +02:00
const {getAppApis, getTemplateApi} = require("budibase-core");
2019-07-09 08:29:50 +02:00
const getDatastore = require("./datastore");
const { masterAppPackage } = require("../utilities/createAppPackage");
const getDatabaseManager = require("../utilities/databaseManager");
2019-06-25 23:48:22 +02:00
const constructHierarchy = (datastore, appDefinition) => {
appDefinition.hierarchy = getTemplateApi({datastore})
.constructHierarchy(appDefinition.hierarchy);
return appDefinition;
}
module.exports.getApisWithFullAccess = async (datastore, appPackage) => {
const appDefinition = constructHierarchy(
datastore,
appPackage.appDefinition);
2019-06-07 15:18:10 +02:00
const bb = await getAppApis(
datastore,
2019-06-25 23:48:22 +02:00
appPackage.behaviourSources,
null, //cleanupTransations
null, // getEpochTime
crypto,
appDefinition
2019-06-07 15:18:10 +02:00
);
bb.withFullAccess();
return bb;
};
2019-06-25 23:48:22 +02:00
module.exports.getApisForUser = async (datastore, appPackage, username, password) => {
2019-06-07 15:18:10 +02:00
const bb = await getAppApis(
datastore,
2019-06-25 23:48:22 +02:00
appPackage.behaviourSources,
null, //cleanupTransations
null, // getEpochTime
crypto,
constructHierarchy(
datastore,
appPackage.appDefinition)
2019-06-07 15:18:10 +02:00
);
await bb.authenticateAs(username, password);
return bb;
}
2019-06-25 23:48:22 +02:00
module.exports.getApisForSession = async (datastore, appPackage, session) => {
2019-06-14 11:05:46 +02:00
const user = JSON.parse(session.user_json);
const bb = await getAppApis(
datastore,
2019-06-25 23:48:22 +02:00
appPackage.behaviourSources,
null, //cleanupTransations
null, // getEpochTime
crypto,
constructHierarchy(
datastore,
appPackage.appDefinition)
2019-06-14 11:05:46 +02:00
);
bb.asUser(user);
return bb;
2019-07-09 08:29:50 +02:00
}
module.exports.getMasterApisWithFullAccess = async (context) => {
const { config } = context;
const datastoreModule = getDatastore(config);
const databaseManager = getDatabaseManager(
datastoreModule,
config.datastoreConfig);
const masterDatastore = datastoreModule.getDatastore(
databaseManager.masterDatastoreConfig);
return await module.exports.getApisWithFullAccess(
masterDatastore, masterAppPackage(context));
2019-06-14 11:05:46 +02:00
}