2019-07-09 08:29:50 +02:00
|
|
|
const {
|
2020-02-03 10:24:25 +01:00
|
|
|
getApisWithFullAccess,
|
|
|
|
getApisForSession,
|
|
|
|
getMasterApisWithFullAccess,
|
|
|
|
} = require("./budibaseApi")
|
|
|
|
const getDatastore = require("./datastore")
|
|
|
|
const getDatabaseManager = require("./databaseManager")
|
2020-02-25 16:21:23 +01:00
|
|
|
const { $ } = require("@budibase/core").common
|
2020-03-10 11:05:09 +01:00
|
|
|
const { keyBy, values, cloneDeep } = require("lodash/fp")
|
2020-02-03 10:24:25 +01:00
|
|
|
const {
|
|
|
|
masterAppPackage,
|
|
|
|
applictionVersionPackage,
|
|
|
|
applictionVersionPublicPaths,
|
2020-03-25 13:38:04 +01:00
|
|
|
deleteCachedPackage,
|
2020-02-03 10:24:25 +01:00
|
|
|
} = require("../utilities/createAppPackage")
|
2020-03-25 13:38:04 +01:00
|
|
|
const { determineVersionId, LATEST_VERSIONID } = require("./runtimePackages")
|
2020-02-03 10:24:25 +01:00
|
|
|
|
|
|
|
const isMaster = appname => appname === "_master"
|
|
|
|
|
|
|
|
module.exports = async context => {
|
|
|
|
const { config } = context
|
|
|
|
const datastoreModule = getDatastore(config)
|
|
|
|
|
|
|
|
const databaseManager = getDatabaseManager(
|
|
|
|
datastoreModule,
|
|
|
|
config.datastoreConfig
|
|
|
|
)
|
|
|
|
|
|
|
|
const masterDatastore = datastoreModule.getDatastore(
|
|
|
|
databaseManager.masterDatastoreConfig
|
|
|
|
)
|
|
|
|
|
|
|
|
const bb = await getMasterApisWithFullAccess(context)
|
|
|
|
|
|
|
|
let applications
|
|
|
|
const loadApplications = async () => {
|
|
|
|
const apps = await bb.indexApi.listItems("/all_applications")
|
|
|
|
applications = $(apps, [keyBy("name")])
|
|
|
|
}
|
|
|
|
await loadApplications()
|
|
|
|
|
|
|
|
const getInstanceDatastore = instanceDatastoreConfig =>
|
|
|
|
datastoreModule.getDatastore(instanceDatastoreConfig)
|
|
|
|
|
|
|
|
const getCustomSessionId = (appname, sessionId) =>
|
|
|
|
isMaster(appname)
|
|
|
|
? bb.recordApi.customId("mastersession", sessionId)
|
|
|
|
: bb.recordApi.customId("session", sessionId)
|
|
|
|
|
|
|
|
const getApplication = async (nameOrKey, isRetry = false) => {
|
|
|
|
if (applications[nameOrKey]) return applications[nameOrKey]
|
|
|
|
|
|
|
|
for (let name in applications) {
|
|
|
|
const a = applications[name]
|
|
|
|
if (a.key === nameOrKey) return a
|
|
|
|
if (a.id === nameOrKey) return a
|
2019-07-18 09:05:16 +02:00
|
|
|
}
|
2019-06-14 11:05:46 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
if (isRetry) return
|
2019-06-14 18:01:01 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
await loadApplications()
|
2019-06-14 11:05:46 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
return await getApplication(nameOrKey, true)
|
|
|
|
}
|
2019-07-11 08:35:29 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
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}`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2019-07-11 08:35:29 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const createAppUser = async (appname, instance, user, password) => {
|
|
|
|
if (isMaster(appname)) {
|
2020-02-12 13:45:24 +01:00
|
|
|
throw new Error("This method is for creating app users - not on master!")
|
2020-02-03 10:24:25 +01:00
|
|
|
}
|
2019-06-14 11:05:46 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const versionId = determineVersionId(instance.version)
|
|
|
|
const dsConfig = JSON.parse(instance.datastoreconfig)
|
|
|
|
const appPackage = await applictionVersionPackage(
|
|
|
|
context,
|
|
|
|
appname,
|
|
|
|
versionId,
|
|
|
|
instance.key
|
|
|
|
)
|
|
|
|
|
|
|
|
const bbInstance = await getApisWithFullAccess(
|
|
|
|
datastoreModule.getDatastore(dsConfig),
|
|
|
|
appPackage
|
|
|
|
)
|
|
|
|
|
|
|
|
await bbInstance.authApi.createUser(user, password)
|
|
|
|
}
|
|
|
|
|
2020-02-25 16:21:23 +01:00
|
|
|
const authenticate = async (sessionId, appname, username, password) => {
|
2020-02-03 10:24:25 +01:00
|
|
|
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)
|
|
|
|
session.username = username
|
|
|
|
await bb.recordApi.save(session)
|
|
|
|
return session
|
|
|
|
}
|
2019-06-14 11:05:46 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const app = await getApplication(appname)
|
2019-06-14 11:05:46 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const userInMaster = await getUser(app.id, username)
|
|
|
|
if (!userInMaster) return null
|
2019-06-14 11:05:46 +02:00
|
|
|
|
2020-03-10 11:05:09 +01:00
|
|
|
const { instance, bbInstance } = await getFullAccessApiForInstanceId(
|
2020-02-03 10:24:25 +01:00
|
|
|
appname,
|
2020-03-10 11:05:09 +01:00
|
|
|
userInMaster.instance.id,
|
|
|
|
app.id
|
2020-02-03 10:24:25 +01:00
|
|
|
)
|
2019-10-18 18:32:03 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const authUser = await bbInstance.authApi.authenticate(username, password)
|
2019-06-14 11:05:46 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
if (!authUser) {
|
|
|
|
return null
|
|
|
|
}
|
2019-06-14 11:05:46 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
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
|
|
|
|
session.instanceKey = instance.key
|
|
|
|
session.username = username
|
|
|
|
session.instanceVersion = instance.version.key
|
|
|
|
await bb.recordApi.save(session)
|
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
2020-03-10 11:05:09 +01:00
|
|
|
const getFullAccessApiForInstanceId = async (appname, instanceId, appId) => {
|
|
|
|
if (!appId) {
|
2020-03-11 17:42:19 +01:00
|
|
|
const app = await getApplication(appname)
|
|
|
|
appId = app.id
|
2020-03-10 11:05:09 +01:00
|
|
|
}
|
|
|
|
const instanceKey = `/applications/${appId}/instances/${instanceId}`
|
|
|
|
const instance = await bb.recordApi.load(instanceKey)
|
|
|
|
|
|
|
|
const versionId = determineVersionId(instance.version)
|
|
|
|
|
|
|
|
const dsConfig = JSON.parse(instance.datastoreconfig)
|
|
|
|
const appPackage = await applictionVersionPackage(
|
|
|
|
context,
|
|
|
|
appname,
|
|
|
|
versionId,
|
|
|
|
instance.key
|
|
|
|
)
|
|
|
|
return {
|
|
|
|
bbInstance: await getApisWithFullAccess(
|
2020-03-11 17:42:19 +01:00
|
|
|
getInstanceDatastore(dsConfig),
|
2020-03-10 11:05:09 +01:00
|
|
|
appPackage
|
|
|
|
),
|
|
|
|
instance,
|
2020-03-11 17:42:19 +01:00
|
|
|
publicPath: appPackage.mainUiPath,
|
|
|
|
sharedPath: appPackage.sharedPath,
|
2020-03-10 11:05:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-11 17:42:19 +01:00
|
|
|
const getFullAccessApiForMaster = async () => {
|
|
|
|
const masterPkg = masterAppPackage(context)
|
|
|
|
const instance = await getApisWithFullAccess(masterDatastore, masterPkg)
|
|
|
|
|
|
|
|
return {
|
|
|
|
instance,
|
|
|
|
publicPath: masterPkg.unauthenticatedUiPath,
|
|
|
|
sharedPath: masterPkg.sharedPath,
|
|
|
|
}
|
|
|
|
}
|
2020-03-10 11:05:09 +01:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const getInstanceApiForSession = async (appname, sessionId) => {
|
|
|
|
if (isMaster(appname)) {
|
|
|
|
const customId = bb.recordApi.customId("mastersession", sessionId)
|
|
|
|
const masterPkg = masterAppPackage(context)
|
|
|
|
try {
|
|
|
|
const session = await bb.recordApi.load(`/sessions/${customId}`)
|
|
|
|
return {
|
|
|
|
instance: await getApisForSession(
|
|
|
|
masterDatastore,
|
|
|
|
masterAppPackage(context),
|
|
|
|
session
|
|
|
|
),
|
|
|
|
publicPath: masterPkg.mainUiPath,
|
|
|
|
sharedPath: masterPkg.sharedPath,
|
2019-06-14 11:05:46 +02:00
|
|
|
}
|
2020-02-03 10:24:25 +01:00
|
|
|
} catch (_) {
|
|
|
|
return {
|
|
|
|
instance: null,
|
|
|
|
publicPath: masterPkg.unauthenticatedUiPath,
|
|
|
|
sharedPath: masterPkg.sharedPath,
|
2019-06-14 11:05:46 +02:00
|
|
|
}
|
2020-02-03 10:24:25 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const app = await getApplication(appname)
|
|
|
|
const customId = bb.recordApi.customId("session", sessionId)
|
|
|
|
try {
|
|
|
|
const session = await bb.recordApi.load(
|
|
|
|
`/applications/${app.id}/sessions/${customId}`
|
|
|
|
)
|
|
|
|
const dsConfig = JSON.parse(session.instanceDatastoreConfig)
|
|
|
|
const instanceDatastore = getInstanceDatastore(dsConfig)
|
|
|
|
|
|
|
|
const versionId = determineVersionId(session.instanceVersion)
|
2019-06-14 11:05:46 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const appPackage = await applictionVersionPackage(
|
|
|
|
context,
|
|
|
|
appname,
|
|
|
|
versionId,
|
|
|
|
session.instanceKey
|
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
|
|
|
instance: await getApisForSession(
|
|
|
|
instanceDatastore,
|
|
|
|
appPackage,
|
|
|
|
session
|
|
|
|
),
|
|
|
|
publicPath: appPackage.mainUiPath,
|
|
|
|
sharedPath: appPackage.sharedPath,
|
2019-06-14 11:05:46 +02:00
|
|
|
}
|
2020-02-03 10:24:25 +01:00
|
|
|
} catch (_) {
|
|
|
|
const versionId = determineVersionId(app.defaultVersion)
|
|
|
|
const appPublicPaths = applictionVersionPublicPaths(
|
|
|
|
context,
|
|
|
|
app.name,
|
|
|
|
versionId
|
|
|
|
)
|
|
|
|
return {
|
|
|
|
instance: null,
|
|
|
|
publicPath: appPublicPaths.unauthenticatedUiPath,
|
|
|
|
sharedPath: appPublicPaths.sharedPath,
|
2019-07-11 08:35:29 +02:00
|
|
|
}
|
2020-02-03 10:24:25 +01:00
|
|
|
}
|
2019-07-05 17:56:53 +02:00
|
|
|
}
|
2020-02-03 10:24:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const getUser = async (appId, username) => {
|
|
|
|
const userId = bb.recordApi.customId("user", username)
|
|
|
|
try {
|
|
|
|
return await bb.recordApi.load(`/applications/${appId}/users/${userId}`)
|
|
|
|
} catch (_) {
|
|
|
|
//empty
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-07-05 17:56:53 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const getFullAccessInstanceApiForUsername = async (appname, username) => {
|
|
|
|
if (isMaster(appname)) {
|
|
|
|
return bb
|
|
|
|
} else {
|
|
|
|
const app = await getApplication(appname)
|
|
|
|
const user = await getUser(app.id, username)
|
2019-06-16 23:17:22 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
if (!user) return null
|
2019-06-16 23:17:22 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const dsConfig = JSON.parse(user.instance.datastoreconfig)
|
|
|
|
const instanceDatastore = getInstanceDatastore(dsConfig)
|
2019-06-19 23:05:53 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const versionId = determineVersionId(
|
|
|
|
(await bb.recordApi.load(user.instance.key)).version
|
|
|
|
)
|
2019-07-09 08:29:50 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const appPackage = await applictionVersionPackage(
|
|
|
|
context,
|
|
|
|
appname,
|
|
|
|
versionId,
|
|
|
|
user.instance.key
|
|
|
|
)
|
|
|
|
|
|
|
|
return await getApisWithFullAccess(instanceDatastore, appPackage)
|
2019-07-09 08:29:50 +02:00
|
|
|
}
|
2020-02-03 10:24:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const removeSessionsForUser = async (appname, username) => {
|
|
|
|
if (isMaster(appname)) {
|
|
|
|
const sessions = await bb.indexApi.listItems("/mastersessions_by_user", {
|
|
|
|
rangeStartParams: { username },
|
|
|
|
rangeEndParams: { username },
|
|
|
|
searchPhrase: `username:${username}`,
|
|
|
|
})
|
|
|
|
|
|
|
|
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: { username },
|
|
|
|
rangeEndParams: { username },
|
|
|
|
searchPhrase: `username:${username}`,
|
|
|
|
}
|
|
|
|
)
|
2019-07-09 08:29:50 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
for (let session of sessions) {
|
|
|
|
await bb.recordApi.delete(session.key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-10 11:05:09 +01:00
|
|
|
const getApplicationWithInstances = async appname => {
|
|
|
|
const app = cloneDeep(await getApplication(appname))
|
|
|
|
app.instances = await bb.indexApi.listItems(
|
|
|
|
`/applications/${app.id}/allinstances`
|
|
|
|
)
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const disableUser = async (app, username) => {
|
|
|
|
await removeSessionsForUser(app.name, username)
|
|
|
|
const userInMaster = await getUser(app.id, username)
|
|
|
|
userInMaster.active = false
|
|
|
|
await bb.recordApi.save(userInMaster)
|
|
|
|
}
|
|
|
|
|
|
|
|
const enableUser = async (app, username) => {
|
|
|
|
const userInMaster = await getUser(app.id, username)
|
|
|
|
userInMaster.active = true
|
|
|
|
await bb.recordApi.save(userInMaster)
|
|
|
|
}
|
|
|
|
|
2020-03-25 13:38:04 +01:00
|
|
|
const deleteLatestPackageFromCache = (appname) => {
|
|
|
|
deleteCachedPackage(context, appname, LATEST_VERSIONID)
|
|
|
|
}
|
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const listApplications = () => values(applications)
|
|
|
|
|
|
|
|
return {
|
|
|
|
getApplication,
|
|
|
|
getSession,
|
|
|
|
deleteSession,
|
|
|
|
authenticate,
|
|
|
|
getInstanceApiForSession,
|
|
|
|
getFullAccessInstanceApiForUsername,
|
|
|
|
removeSessionsForUser,
|
|
|
|
disableUser,
|
|
|
|
enableUser,
|
|
|
|
getUser,
|
|
|
|
createAppUser,
|
|
|
|
bbMaster: bb,
|
|
|
|
listApplications,
|
2020-03-10 11:05:09 +01:00
|
|
|
getFullAccessApiForInstanceId,
|
|
|
|
getFullAccessApiForMaster,
|
|
|
|
getApplicationWithInstances,
|
2020-03-25 13:38:04 +01:00
|
|
|
deleteLatestPackageFromCache,
|
2020-02-03 10:24:25 +01:00
|
|
|
}
|
|
|
|
}
|