From 1590b584d43718a0cfd56ea4850f4e4366593d4a Mon Sep 17 00:00:00 2001 From: michael shanks Date: Sun, 7 Jul 2019 09:03:37 +0100 Subject: [PATCH] work on app instances --- packages/core | 2 +- .../appPackages/master/appDefinition.json | 12 + packages/server/appPackages/master/main.js | 16 +- .../appPackages/testApp/access_levels.json | 76 ++- .../appPackages/testApp/appDefinition.json | 395 +--------------- .../appPackages/testApp/dist/package.tar.gz | Bin 2176 -> 1185 bytes .../server/initialise/createInstanceDb.js | 32 +- packages/server/middleware/routers.js | 10 + .../testApp/3-7HHy_x966Th/access_levels.json | 1 - .../testApp/3-7HHy_x966Th/appDefinition.json | 445 ------------------ .../testApp/3-Y5vsgF-ZEjh/access_levels.json | 75 +++ .../testApp/3-Y5vsgF-ZEjh/appDefinition.json | 78 +++ .../plugins.js | 0 packages/server/tests/all.spec.js | 3 +- packages/server/tests/authenticate.js | 63 +-- packages/server/tests/createNewApp.js | 29 +- packages/server/tests/testApp.js | 16 +- packages/server/utilities/createAppPackage.js | 7 +- .../server/utilities/masterAppInternal.js | 11 +- 19 files changed, 376 insertions(+), 895 deletions(-) delete mode 100644 packages/server/runtime_apps/testApp/3-7HHy_x966Th/access_levels.json delete mode 100644 packages/server/runtime_apps/testApp/3-7HHy_x966Th/appDefinition.json create mode 100644 packages/server/runtime_apps/testApp/3-Y5vsgF-ZEjh/access_levels.json create mode 100644 packages/server/runtime_apps/testApp/3-Y5vsgF-ZEjh/appDefinition.json rename packages/server/runtime_apps/testApp/{3-7HHy_x966Th => 3-Y5vsgF-ZEjh}/plugins.js (100%) diff --git a/packages/core b/packages/core index 804483d3a..ab14ab9db 160000 --- a/packages/core +++ b/packages/core @@ -1 +1 @@ -Subproject commit 804483d3a3210936743f3b57a032783296eab06c +Subproject commit ab14ab9db1698a82b1f98b692ecfa6b71c17752a diff --git a/packages/server/appPackages/master/appDefinition.json b/packages/server/appPackages/master/appDefinition.json index 7487ae21b..02296255c 100644 --- a/packages/server/appPackages/master/appDefinition.json +++ b/packages/server/appPackages/master/appDefinition.json @@ -273,6 +273,18 @@ "getInitialValue": "default", "getUndefinedValue": "default" }, + { + "name": "instanceVersion", + "type": "string", + "typeOptions": { + "maxLength": null, + "values": null, + "allowDeclaredValuesOnly": false + }, + "label": "Instance Version", + "getInitialValue": "default", + "getUndefinedValue": "default" + }, { "name": "username", "type": "string", diff --git a/packages/server/appPackages/master/main.js b/packages/server/appPackages/master/main.js index bc6f1b36d..849b90c72 100644 --- a/packages/server/appPackages/master/main.js +++ b/packages/server/appPackages/master/main.js @@ -23,13 +23,6 @@ module.exports = (config) => { const application = await apis.recordApi.load(appKey); - const dbConfig = await createInstanceDb( - datastoreModule, - config.datastoreConfig, - application.id, - instance.id - ); - const versionId = $(instance.version.key, [ splitKey, last @@ -41,6 +34,15 @@ module.exports = (config) => { if(!await exists(runtimeDir)) await downloadAppPackage(apis, instance, application.name, versionId); + + const dbConfig = await createInstanceDb( + config, + datastoreModule, + config.datastoreConfig, + application, + instance + ); + instance.datastoreconfig = JSON.stringify(dbConfig); instance.isNew = false; diff --git a/packages/server/appPackages/testApp/access_levels.json b/packages/server/appPackages/testApp/access_levels.json index ebb7ca11a..4e01e4e9a 100644 --- a/packages/server/appPackages/testApp/access_levels.json +++ b/packages/server/appPackages/testApp/access_levels.json @@ -1 +1,75 @@ -{"levels":[{"name":"owner","permissions":[{"type":"create record","nodeKey":"/applications/1-{id}"},{"type":"update record","nodeKey":"/applications/1-{id}"},{"type":"delete record","nodeKey":"/applications/1-{id}"},{"type":"read record","nodeKey":"/applications/1-{id}"},{"type":"create record","nodeKey":"/applications/1-{id}/users/8-{id}"},{"type":"update record","nodeKey":"/applications/1-{id}/users/8-{id}"},{"type":"delete record","nodeKey":"/applications/1-{id}/users/8-{id}"},{"type":"read record","nodeKey":"/applications/1-{id}/users/8-{id}"},{"type":"create record","nodeKey":"/applications/1-{id}/instances/2-{id}"},{"type":"update record","nodeKey":"/applications/1-{id}/instances/2-{id}"},{"type":"delete record","nodeKey":"/applications/1-{id}/instances/2-{id}"},{"type":"read record","nodeKey":"/applications/1-{id}/instances/2-{id}"},{"type":"create record","nodeKey":"/applications/1-{id}/versions/3-{id}"},{"type":"update record","nodeKey":"/applications/1-{id}/versions/3-{id}"},{"type":"delete record","nodeKey":"/applications/1-{id}/versions/3-{id}"},{"type":"read record","nodeKey":"/applications/1-{id}/versions/3-{id}"},{"type":"create record","nodeKey":"/applications/1-{id}/sessions/16-{id}"},{"type":"update record","nodeKey":"/applications/1-{id}/sessions/16-{id}"},{"type":"delete record","nodeKey":"/applications/1-{id}/sessions/16-{id}"},{"type":"read record","nodeKey":"/applications/1-{id}/sessions/16-{id}"},{"type":"create record","nodeKey":"/sessions/17-{id}"},{"type":"update record","nodeKey":"/sessions/17-{id}"},{"type":"delete record","nodeKey":"/sessions/17-{id}"},{"type":"read record","nodeKey":"/sessions/17-{id}"},{"type":"read index","nodeKey":"/mastersessions_by_user"},{"type":"read index","nodeKey":"/all_applications"},{"type":"read index","nodeKey":"/applications/1-{id}/allinstances"},{"type":"read index","nodeKey":"/applications/1-{id}/sessions_by_user"},{"type":"read index","nodeKey":"/applications/1-{id}/user_name_lookup"},{"type":"read index","nodeKey":"/applications/1-{id}/all_versions"},{"type":"read index","nodeKey":"/applications/1-{id}/instances/2-{id}/users_on_this_instance"},{"type":"read index","nodeKey":"/applications/1-{id}/versions/3-{id}/instances_for_this_version"},{"type":"read index","nodeKey":"/applications/1-{id}/versions/3-{id}/instances_on_this_version"},{"type":"write templates"},{"type":"create user"},{"type":"set password"},{"type":"create temporary access"},{"type":"enable or disable user"},{"type":"write access levels"},{"type":"list users"},{"type":"list access levels"},{"type":"manage index"},{"type":"manage collection"},{"type":"set user access levels"}]}],"version":0} \ No newline at end of file +{ + "levels": [ + { + "name": "owner", + "permissions": [ + { + "type": "create record", + "nodeKey": "/customers/1-{id}" + }, + { + "type": "delete record", + "nodeKey": "/customers/1-{id}" + }, + { + "type": "update record", + "nodeKey": "/customers/1-{id}" + }, + { + "type": "read record", + "nodeKey": "/customers/1-{id}" + }, + { + "type": "create record", + "nodeKey": "/customers/1-{id}/invoices/2-{id}" + }, + { + "type": "update record", + "nodeKey": "/customers/1-{id}/invoices/2-{id}" + }, + { + "type": "delete record", + "nodeKey": "/customers/1-{id}/invoices/2-{id}" + }, + { + "type": "read record", + "nodeKey": "/customers/1-{id}/invoices/2-{id}" + }, + { + "type": "write templates" + }, + { + "type": "create user" + }, + { + "type": "set password" + }, + { + "type": "create temporary access" + }, + { + "type": "enable or disable user" + }, + { + "type": "write access levels" + }, + { + "type": "list users" + }, + { + "type": "list access levels" + }, + { + "type": "manage index" + }, + { + "type": "set user access levels" + }, + { + "type": "manage collection" + } + ] + } + ], + "version": 0 +} \ No newline at end of file diff --git a/packages/server/appPackages/testApp/appDefinition.json b/packages/server/appPackages/testApp/appDefinition.json index 6747e91e3..17b992b18 100644 --- a/packages/server/appPackages/testApp/appDefinition.json +++ b/packages/server/appPackages/testApp/appDefinition.json @@ -4,426 +4,59 @@ "type": "root", "children": [ { - "name": "application", + "name": "customer", "type": "record", "fields": [ { "name": "name", "type": "string", "typeOptions": { - "maxLength": 500, + "maxLength": 1000, "values": null, "allowDeclaredValuesOnly": false }, - "label": "Name", - "getInitialValue": "default", - "getUndefinedValue": "default" - }, - { - "name": "domain", - "type": "string", - "typeOptions": { - "maxLength": 500, - "values": null, - "allowDeclaredValuesOnly": false - }, - "label": "domain", - "getInitialValue": "default", - "getUndefinedValue": "default" - }, - { - "name": "application_resolve_strategy", - "type": "string", - "typeOptions": { - "maxLength": 100, - "values": [ - "domain", - "path" - ], - "allowDeclaredValuesOnly": true - }, - "label": "Resolve Application By", + "label": "name", "getInitialValue": "default", "getUndefinedValue": "default" } ], "children": [ { - "name": "user", - "type": "record", - "fields": [ - { - "name": "unique_name", - "type": "string", - "typeOptions": { - "maxLength": 200, - "values": null, - "allowDeclaredValuesOnly": false - }, - "label": "Name (unique)", - "getInitialValue": "default", - "getUndefinedValue": "default" - }, - { - "name": "active", - "type": "bool", - "typeOptions": { - "allowNulls": false - }, - "label": "Is Active", - "getInitialValue": "default", - "getUndefinedValue": "default" - }, - { - "name": "instance", - "type": "reference", - "typeOptions": { - "indexNodeKey": "/applications/1-{id}/allinstances", - "reverseIndexNodeKeys": [ - "/applications/1-{id}/instances/2-{id}/users_on_this_instance" - ], - "displayValue": "name" - }, - "label": "Instance", - "getInitialValue": "default", - "getUndefinedValue": "default" - } - ], - "children": [], - "validationRules": [], - "nodeId": 8, - "indexes": [], - "allidsShardFactor": "64", - "collectionName": "users", - "isSingle": false - }, - { - "name": "instance", - "type": "record", - "fields": [ - { - "name": "name", - "type": "string", - "typeOptions": { - "maxLength": 1000, - "values": null, - "allowDeclaredValuesOnly": false - }, - "label": "Name", - "getInitialValue": "default", - "getUndefinedValue": "default" - }, - { - "name": "active", - "type": "bool", - "typeOptions": { - "allowNulls": false - }, - "label": "Is Active", - "getInitialValue": "default", - "getUndefinedValue": "default" - }, - { - "name": "version", - "type": "reference", - "typeOptions": { - "indexNodeKey": "/applications/1-{id}/all_versions", - "reverseIndexNodeKeys": [ - "/applications/1-{id}/versions/3-{id}/instances_on_this_version" - ], - "displayValue": "name" - }, - "label": "Version", - "getInitialValue": "default", - "getUndefinedValue": "default" - }, - { - "name": "datastoreconfig", - "type": "string", - "typeOptions": { - "maxLength": 1000, - "values": null, - "allowDeclaredValuesOnly": false - }, - "label": "Datastore Config", - "getInitialValue": "default", - "getUndefinedValue": "default" - } - ], - "children": [], - "validationRules": [], - "nodeId": 2, - "indexes": [ - { - "name": "users_on_this_instance", - "type": "index", - "map": "return {...record};", - "filter": "", - "indexType": "reference", - "getShardName": "", - "getSortKey": "record.id", - "aggregateGroups": [], - "allowedRecordNodeIds": [], - "nodeId": 15 - } - ], - "allidsShardFactor": 1, - "collectionName": "instances", - "isSingle": false - }, - { - "name": "version", - "type": "record", - "fields": [ - { - "name": "name", - "type": "string", - "typeOptions": { - "maxLength": 200, - "values": null, - "allowDeclaredValuesOnly": false - }, - "label": "Name", - "getInitialValue": "default", - "getUndefinedValue": "default" - }, - { - "name": "package", - "type": "file", - "typeOptions": {}, - "label": "Package", - "getInitialValue": "default", - "getUndefinedValue": "default" - } - ], - "children": [], - "validationRules": [], - "nodeId": 3, - "indexes": [ - { - "name": "instances_for_this_version", - "type": "index", - "map": "return {name:record.name};", - "filter": "", - "indexType": "ancestor", - "getShardName": "", - "getSortKey": "record.id", - "aggregateGroups": [], - "allowedRecordNodeIds": [], - "nodeId": 9 - }, - { - "name": "instances_on_this_version", - "type": "index", - "map": "return {...record};", - "filter": "", - "indexType": "reference", - "getShardName": "", - "getSortKey": "record.id", - "aggregateGroups": [], - "allowedRecordNodeIds": [], - "nodeId": 10 - } - ], - "allidsShardFactor": 1, - "collectionName": "versions", - "isSingle": false - }, - { - "name": "session", + "name": "invoice", "type": "record", "fields": [ { - "name": "created", + "name": "amount", "type": "number", "typeOptions": { - "minValue": 0, - "maxValue": 99999999999999, - "decimalPlaces": 0 + "minValue": 99999999999, + "maxValue": 99999999999, + "decimalPlaces": 2 }, - "label": "Created", - "getInitialValue": "default", - "getUndefinedValue": "default" - }, - { - "name": "user_json", - "type": "string", - "typeOptions": { - "maxLength": null, - "values": null, - "allowDeclaredValuesOnly": false - }, - "label": "User Json", - "getInitialValue": "default", - "getUndefinedValue": "default" - }, - { - "name": "instanceDatastoreConfig", - "type": "string", - "typeOptions": { - "maxLength": null, - "values": null, - "allowDeclaredValuesOnly": false - }, - "label": "Instance Datastore Config", - "getInitialValue": "default", - "getUndefinedValue": "default" - }, - { - "name": "username", - "type": "string", - "typeOptions": { - "maxLength": null, - "values": null, - "allowDeclaredValuesOnly": false - }, - "label": "User", + "label": "amount", "getInitialValue": "default", "getUndefinedValue": "default" } ], "children": [], "validationRules": [], - "nodeId": 16, + "nodeId": 2, "indexes": [], "allidsShardFactor": 1, - "collectionName": "sessions", + "collectionName": "invoices", "isSingle": false } ], "validationRules": [], "nodeId": 1, - "indexes": [ - { - "name": "allinstances", - "type": "index", - "map": "return {...record};", - "filter": "", - "indexType": "ancestor", - "getShardName": "", - "getSortKey": "record.id", - "aggregateGroups": [], - "allowedRecordNodeIds": [ - 2 - ], - "nodeId": 23 - }, - { - "name": "sessions_by_user", - "type": "index", - "map": "return {username:record.username};", - "filter": "", - "indexType": "ancestor", - "getShardName": "return record.username.substring(0,2)", - "getSortKey": "record.id", - "aggregateGroups": [], - "allowedRecordNodeIds": [ - 16 - ], - "nodeId": 24 - }, - { - "name": "user_name_lookup", - "type": "index", - "map": "return {name:record.unique_name, instanceDatastoreConfig:instance.datastoreconfig};", - "filter": "", - "indexType": "ancestor", - "getShardName": "return record.unique_name.substring(0,2)", - "getSortKey": "record.id", - "aggregateGroups": [], - "allowedRecordNodeIds": [ - 8 - ], - "nodeId": 25 - }, - { - "name": "all_versions", - "type": "index", - "map": "return {...record};", - "filter": "", - "indexType": "ancestor", - "getShardName": "", - "getSortKey": "record.id", - "aggregateGroups": [], - "allowedRecordNodeIds": [ - 3 - ], - "nodeId": 26 - } - ], - "allidsShardFactor": 64, - "collectionName": "applications", - "isSingle": false - }, - { - "name": "mastersession", - "type": "record", - "fields": [ - { - "name": "user_json", - "type": "string", - "typeOptions": { - "maxLength": 10000, - "values": null, - "allowDeclaredValuesOnly": false - }, - "label": "User Json", - "getInitialValue": "default", - "getUndefinedValue": "default" - }, - { - "name": "username", - "type": "string", - "typeOptions": { - "maxLength": null, - "values": null, - "allowDeclaredValuesOnly": false - }, - "label": "User", - "getInitialValue": "default", - "getUndefinedValue": "default" - } - ], - "children": [], - "validationRules": [], - "nodeId": 17, "indexes": [], "allidsShardFactor": 64, - "collectionName": "sessions", + "collectionName": "customers", "isSingle": false } ], "pathMaps": [], - "indexes": [ - { - "name": "all_applications", - "type": "index", - "map": "return {...record};", - "filter": "", - "indexType": "ancestor", - "getShardName": "", - "getSortKey": "record.id", - "aggregateGroups": [], - "allowedRecordNodeIds": [ - 1 - ], - "nodeId": 22 - }, - { - "name": "mastersessions_by_user", - "type": "index", - "map": "return {username:record.username};", - "filter": "", - "indexType": "ancestor", - "getShardName": "return record.username.substring(0,2)", - "getSortKey": "record.id", - "aggregateGroups": [], - "allowedRecordNodeIds": [ - 17 - ], - "nodeId": 27 - } - ], + "indexes": [], "nodeId": 0 }, "actions": { @@ -439,7 +72,7 @@ "actionName": "output_to_file", "eventName": "authApi:createUser:onComplete", "optionsCreator": "return { filename:'tempaccess' + context.user.name, content:context.result.tempCode };", - "condition": "" + "condition": "!context.password" } ] } \ No newline at end of file diff --git a/packages/server/appPackages/testApp/dist/package.tar.gz b/packages/server/appPackages/testApp/dist/package.tar.gz index bd99d0227d58940ead17548080cedfd79c109a6e..ee34a92d9fe435c2cb12d46ae02d7edaff05ea0c 100644 GIT binary patch literal 1185 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 CwKlB) diff --git a/packages/server/initialise/createInstanceDb.js b/packages/server/initialise/createInstanceDb.js index d0996c0a7..e5273d0f3 100644 --- a/packages/server/initialise/createInstanceDb.js +++ b/packages/server/initialise/createInstanceDb.js @@ -1,28 +1,44 @@ const { initialiseData, - setupDatastore + setupDatastore, + common } = require("budibase-core"); const constructHierarchy = require("../utilities/constructHierarchy"); const getDatabaseManager = require("../utilities/databaseManager"); const masterDbAppDefinition = require("../appPackages/master/appDefinition.json"); +const { applictionVersionPackage } = require("../utilities/createAppPackage"); +const { last } = require("lodash/fp"); +const {$,splitKey} = common; -module.exports = async (datastoreModule, rootDatastoreConfig, appId, instanceId) => { + +module.exports = async (config, datastoreModule, rootDatastoreConfig, app, instance) => { try { const databaseManager = getDatabaseManager( datastoreModule, rootDatastoreConfig); await databaseManager.createEmptyInstanceDb( - appId, instanceId); + app.id, instance.id); const dbConfig = databaseManager.getInstanceDatastoreConfig( - appId, instanceId); + app.id, instance.id); const datastore = setupDatastore( - datastoreModule.getDatastore(dbConfig)); - - await initialiseData(datastore, - constructHierarchy(masterDbAppDefinition)); + datastoreModule.getDatastore(dbConfig)); + + const versionId = $(instance.version.key, [ + splitKey, + last + ]); + + const appPackage = applictionVersionPackage( + config, app.name, versionId + ); + + await initialiseData( + datastore, + appPackage.appDefinition, + appPackage.accessLevels); return dbConfig; diff --git a/packages/server/middleware/routers.js b/packages/server/middleware/routers.js index 8498203dc..5f75b6905 100644 --- a/packages/server/middleware/routers.js +++ b/packages/server/middleware/routers.js @@ -42,6 +42,11 @@ module.exports = (config, app) => { ctx.request.body.username ); + if(!instanceApi) { + ctx.request.status = StatusCodes.OK; + return; + } + await instanceApi.authApi.setPasswordFromTemporaryCode( ctx.request.body.tempCode, ctx.request.body.newPassword); @@ -54,6 +59,11 @@ module.exports = (config, app) => { ctx.request.body.username ); + if(!instanceApi) { + ctx.request.status = StatusCodes.OK; + return; + } + await instanceApi.authApi.createTemporaryAccess( ctx.request.body.username); diff --git a/packages/server/runtime_apps/testApp/3-7HHy_x966Th/access_levels.json b/packages/server/runtime_apps/testApp/3-7HHy_x966Th/access_levels.json deleted file mode 100644 index ebb7ca11a..000000000 --- a/packages/server/runtime_apps/testApp/3-7HHy_x966Th/access_levels.json +++ /dev/null @@ -1 +0,0 @@ -{"levels":[{"name":"owner","permissions":[{"type":"create record","nodeKey":"/applications/1-{id}"},{"type":"update record","nodeKey":"/applications/1-{id}"},{"type":"delete record","nodeKey":"/applications/1-{id}"},{"type":"read record","nodeKey":"/applications/1-{id}"},{"type":"create record","nodeKey":"/applications/1-{id}/users/8-{id}"},{"type":"update record","nodeKey":"/applications/1-{id}/users/8-{id}"},{"type":"delete record","nodeKey":"/applications/1-{id}/users/8-{id}"},{"type":"read record","nodeKey":"/applications/1-{id}/users/8-{id}"},{"type":"create record","nodeKey":"/applications/1-{id}/instances/2-{id}"},{"type":"update record","nodeKey":"/applications/1-{id}/instances/2-{id}"},{"type":"delete record","nodeKey":"/applications/1-{id}/instances/2-{id}"},{"type":"read record","nodeKey":"/applications/1-{id}/instances/2-{id}"},{"type":"create record","nodeKey":"/applications/1-{id}/versions/3-{id}"},{"type":"update record","nodeKey":"/applications/1-{id}/versions/3-{id}"},{"type":"delete record","nodeKey":"/applications/1-{id}/versions/3-{id}"},{"type":"read record","nodeKey":"/applications/1-{id}/versions/3-{id}"},{"type":"create record","nodeKey":"/applications/1-{id}/sessions/16-{id}"},{"type":"update record","nodeKey":"/applications/1-{id}/sessions/16-{id}"},{"type":"delete record","nodeKey":"/applications/1-{id}/sessions/16-{id}"},{"type":"read record","nodeKey":"/applications/1-{id}/sessions/16-{id}"},{"type":"create record","nodeKey":"/sessions/17-{id}"},{"type":"update record","nodeKey":"/sessions/17-{id}"},{"type":"delete record","nodeKey":"/sessions/17-{id}"},{"type":"read record","nodeKey":"/sessions/17-{id}"},{"type":"read index","nodeKey":"/mastersessions_by_user"},{"type":"read index","nodeKey":"/all_applications"},{"type":"read index","nodeKey":"/applications/1-{id}/allinstances"},{"type":"read index","nodeKey":"/applications/1-{id}/sessions_by_user"},{"type":"read index","nodeKey":"/applications/1-{id}/user_name_lookup"},{"type":"read index","nodeKey":"/applications/1-{id}/all_versions"},{"type":"read index","nodeKey":"/applications/1-{id}/instances/2-{id}/users_on_this_instance"},{"type":"read index","nodeKey":"/applications/1-{id}/versions/3-{id}/instances_for_this_version"},{"type":"read index","nodeKey":"/applications/1-{id}/versions/3-{id}/instances_on_this_version"},{"type":"write templates"},{"type":"create user"},{"type":"set password"},{"type":"create temporary access"},{"type":"enable or disable user"},{"type":"write access levels"},{"type":"list users"},{"type":"list access levels"},{"type":"manage index"},{"type":"manage collection"},{"type":"set user access levels"}]}],"version":0} \ No newline at end of file diff --git a/packages/server/runtime_apps/testApp/3-7HHy_x966Th/appDefinition.json b/packages/server/runtime_apps/testApp/3-7HHy_x966Th/appDefinition.json deleted file mode 100644 index 6747e91e3..000000000 --- a/packages/server/runtime_apps/testApp/3-7HHy_x966Th/appDefinition.json +++ /dev/null @@ -1,445 +0,0 @@ -{ - "hierarchy": { - "name": "root", - "type": "root", - "children": [ - { - "name": "application", - "type": "record", - "fields": [ - { - "name": "name", - "type": "string", - "typeOptions": { - "maxLength": 500, - "values": null, - "allowDeclaredValuesOnly": false - }, - "label": "Name", - "getInitialValue": "default", - "getUndefinedValue": "default" - }, - { - "name": "domain", - "type": "string", - "typeOptions": { - "maxLength": 500, - "values": null, - "allowDeclaredValuesOnly": false - }, - "label": "domain", - "getInitialValue": "default", - "getUndefinedValue": "default" - }, - { - "name": "application_resolve_strategy", - "type": "string", - "typeOptions": { - "maxLength": 100, - "values": [ - "domain", - "path" - ], - "allowDeclaredValuesOnly": true - }, - "label": "Resolve Application By", - "getInitialValue": "default", - "getUndefinedValue": "default" - } - ], - "children": [ - { - "name": "user", - "type": "record", - "fields": [ - { - "name": "unique_name", - "type": "string", - "typeOptions": { - "maxLength": 200, - "values": null, - "allowDeclaredValuesOnly": false - }, - "label": "Name (unique)", - "getInitialValue": "default", - "getUndefinedValue": "default" - }, - { - "name": "active", - "type": "bool", - "typeOptions": { - "allowNulls": false - }, - "label": "Is Active", - "getInitialValue": "default", - "getUndefinedValue": "default" - }, - { - "name": "instance", - "type": "reference", - "typeOptions": { - "indexNodeKey": "/applications/1-{id}/allinstances", - "reverseIndexNodeKeys": [ - "/applications/1-{id}/instances/2-{id}/users_on_this_instance" - ], - "displayValue": "name" - }, - "label": "Instance", - "getInitialValue": "default", - "getUndefinedValue": "default" - } - ], - "children": [], - "validationRules": [], - "nodeId": 8, - "indexes": [], - "allidsShardFactor": "64", - "collectionName": "users", - "isSingle": false - }, - { - "name": "instance", - "type": "record", - "fields": [ - { - "name": "name", - "type": "string", - "typeOptions": { - "maxLength": 1000, - "values": null, - "allowDeclaredValuesOnly": false - }, - "label": "Name", - "getInitialValue": "default", - "getUndefinedValue": "default" - }, - { - "name": "active", - "type": "bool", - "typeOptions": { - "allowNulls": false - }, - "label": "Is Active", - "getInitialValue": "default", - "getUndefinedValue": "default" - }, - { - "name": "version", - "type": "reference", - "typeOptions": { - "indexNodeKey": "/applications/1-{id}/all_versions", - "reverseIndexNodeKeys": [ - "/applications/1-{id}/versions/3-{id}/instances_on_this_version" - ], - "displayValue": "name" - }, - "label": "Version", - "getInitialValue": "default", - "getUndefinedValue": "default" - }, - { - "name": "datastoreconfig", - "type": "string", - "typeOptions": { - "maxLength": 1000, - "values": null, - "allowDeclaredValuesOnly": false - }, - "label": "Datastore Config", - "getInitialValue": "default", - "getUndefinedValue": "default" - } - ], - "children": [], - "validationRules": [], - "nodeId": 2, - "indexes": [ - { - "name": "users_on_this_instance", - "type": "index", - "map": "return {...record};", - "filter": "", - "indexType": "reference", - "getShardName": "", - "getSortKey": "record.id", - "aggregateGroups": [], - "allowedRecordNodeIds": [], - "nodeId": 15 - } - ], - "allidsShardFactor": 1, - "collectionName": "instances", - "isSingle": false - }, - { - "name": "version", - "type": "record", - "fields": [ - { - "name": "name", - "type": "string", - "typeOptions": { - "maxLength": 200, - "values": null, - "allowDeclaredValuesOnly": false - }, - "label": "Name", - "getInitialValue": "default", - "getUndefinedValue": "default" - }, - { - "name": "package", - "type": "file", - "typeOptions": {}, - "label": "Package", - "getInitialValue": "default", - "getUndefinedValue": "default" - } - ], - "children": [], - "validationRules": [], - "nodeId": 3, - "indexes": [ - { - "name": "instances_for_this_version", - "type": "index", - "map": "return {name:record.name};", - "filter": "", - "indexType": "ancestor", - "getShardName": "", - "getSortKey": "record.id", - "aggregateGroups": [], - "allowedRecordNodeIds": [], - "nodeId": 9 - }, - { - "name": "instances_on_this_version", - "type": "index", - "map": "return {...record};", - "filter": "", - "indexType": "reference", - "getShardName": "", - "getSortKey": "record.id", - "aggregateGroups": [], - "allowedRecordNodeIds": [], - "nodeId": 10 - } - ], - "allidsShardFactor": 1, - "collectionName": "versions", - "isSingle": false - }, - { - "name": "session", - "type": "record", - "fields": [ - { - "name": "created", - "type": "number", - "typeOptions": { - "minValue": 0, - "maxValue": 99999999999999, - "decimalPlaces": 0 - }, - "label": "Created", - "getInitialValue": "default", - "getUndefinedValue": "default" - }, - { - "name": "user_json", - "type": "string", - "typeOptions": { - "maxLength": null, - "values": null, - "allowDeclaredValuesOnly": false - }, - "label": "User Json", - "getInitialValue": "default", - "getUndefinedValue": "default" - }, - { - "name": "instanceDatastoreConfig", - "type": "string", - "typeOptions": { - "maxLength": null, - "values": null, - "allowDeclaredValuesOnly": false - }, - "label": "Instance Datastore Config", - "getInitialValue": "default", - "getUndefinedValue": "default" - }, - { - "name": "username", - "type": "string", - "typeOptions": { - "maxLength": null, - "values": null, - "allowDeclaredValuesOnly": false - }, - "label": "User", - "getInitialValue": "default", - "getUndefinedValue": "default" - } - ], - "children": [], - "validationRules": [], - "nodeId": 16, - "indexes": [], - "allidsShardFactor": 1, - "collectionName": "sessions", - "isSingle": false - } - ], - "validationRules": [], - "nodeId": 1, - "indexes": [ - { - "name": "allinstances", - "type": "index", - "map": "return {...record};", - "filter": "", - "indexType": "ancestor", - "getShardName": "", - "getSortKey": "record.id", - "aggregateGroups": [], - "allowedRecordNodeIds": [ - 2 - ], - "nodeId": 23 - }, - { - "name": "sessions_by_user", - "type": "index", - "map": "return {username:record.username};", - "filter": "", - "indexType": "ancestor", - "getShardName": "return record.username.substring(0,2)", - "getSortKey": "record.id", - "aggregateGroups": [], - "allowedRecordNodeIds": [ - 16 - ], - "nodeId": 24 - }, - { - "name": "user_name_lookup", - "type": "index", - "map": "return {name:record.unique_name, instanceDatastoreConfig:instance.datastoreconfig};", - "filter": "", - "indexType": "ancestor", - "getShardName": "return record.unique_name.substring(0,2)", - "getSortKey": "record.id", - "aggregateGroups": [], - "allowedRecordNodeIds": [ - 8 - ], - "nodeId": 25 - }, - { - "name": "all_versions", - "type": "index", - "map": "return {...record};", - "filter": "", - "indexType": "ancestor", - "getShardName": "", - "getSortKey": "record.id", - "aggregateGroups": [], - "allowedRecordNodeIds": [ - 3 - ], - "nodeId": 26 - } - ], - "allidsShardFactor": 64, - "collectionName": "applications", - "isSingle": false - }, - { - "name": "mastersession", - "type": "record", - "fields": [ - { - "name": "user_json", - "type": "string", - "typeOptions": { - "maxLength": 10000, - "values": null, - "allowDeclaredValuesOnly": false - }, - "label": "User Json", - "getInitialValue": "default", - "getUndefinedValue": "default" - }, - { - "name": "username", - "type": "string", - "typeOptions": { - "maxLength": null, - "values": null, - "allowDeclaredValuesOnly": false - }, - "label": "User", - "getInitialValue": "default", - "getUndefinedValue": "default" - } - ], - "children": [], - "validationRules": [], - "nodeId": 17, - "indexes": [], - "allidsShardFactor": 64, - "collectionName": "sessions", - "isSingle": false - } - ], - "pathMaps": [], - "indexes": [ - { - "name": "all_applications", - "type": "index", - "map": "return {...record};", - "filter": "", - "indexType": "ancestor", - "getShardName": "", - "getSortKey": "record.id", - "aggregateGroups": [], - "allowedRecordNodeIds": [ - 1 - ], - "nodeId": 22 - }, - { - "name": "mastersessions_by_user", - "type": "index", - "map": "return {username:record.username};", - "filter": "", - "indexType": "ancestor", - "getShardName": "return record.username.substring(0,2)", - "getSortKey": "record.id", - "aggregateGroups": [], - "allowedRecordNodeIds": [ - 17 - ], - "nodeId": 27 - } - ], - "nodeId": 0 - }, - "actions": { - "output_to_file": { - "name": "output_to_file", - "behaviourSource": "main", - "behaviourName": "outputToFile", - "initialOptions": {} - } - }, - "triggers": [ - { - "actionName": "output_to_file", - "eventName": "authApi:createUser:onComplete", - "optionsCreator": "return { filename:'tempaccess' + context.user.name, content:context.result.tempCode };", - "condition": "" - } - ] -} \ No newline at end of file diff --git a/packages/server/runtime_apps/testApp/3-Y5vsgF-ZEjh/access_levels.json b/packages/server/runtime_apps/testApp/3-Y5vsgF-ZEjh/access_levels.json new file mode 100644 index 000000000..4e01e4e9a --- /dev/null +++ b/packages/server/runtime_apps/testApp/3-Y5vsgF-ZEjh/access_levels.json @@ -0,0 +1,75 @@ +{ + "levels": [ + { + "name": "owner", + "permissions": [ + { + "type": "create record", + "nodeKey": "/customers/1-{id}" + }, + { + "type": "delete record", + "nodeKey": "/customers/1-{id}" + }, + { + "type": "update record", + "nodeKey": "/customers/1-{id}" + }, + { + "type": "read record", + "nodeKey": "/customers/1-{id}" + }, + { + "type": "create record", + "nodeKey": "/customers/1-{id}/invoices/2-{id}" + }, + { + "type": "update record", + "nodeKey": "/customers/1-{id}/invoices/2-{id}" + }, + { + "type": "delete record", + "nodeKey": "/customers/1-{id}/invoices/2-{id}" + }, + { + "type": "read record", + "nodeKey": "/customers/1-{id}/invoices/2-{id}" + }, + { + "type": "write templates" + }, + { + "type": "create user" + }, + { + "type": "set password" + }, + { + "type": "create temporary access" + }, + { + "type": "enable or disable user" + }, + { + "type": "write access levels" + }, + { + "type": "list users" + }, + { + "type": "list access levels" + }, + { + "type": "manage index" + }, + { + "type": "set user access levels" + }, + { + "type": "manage collection" + } + ] + } + ], + "version": 0 +} \ No newline at end of file diff --git a/packages/server/runtime_apps/testApp/3-Y5vsgF-ZEjh/appDefinition.json b/packages/server/runtime_apps/testApp/3-Y5vsgF-ZEjh/appDefinition.json new file mode 100644 index 000000000..17b992b18 --- /dev/null +++ b/packages/server/runtime_apps/testApp/3-Y5vsgF-ZEjh/appDefinition.json @@ -0,0 +1,78 @@ +{ + "hierarchy": { + "name": "root", + "type": "root", + "children": [ + { + "name": "customer", + "type": "record", + "fields": [ + { + "name": "name", + "type": "string", + "typeOptions": { + "maxLength": 1000, + "values": null, + "allowDeclaredValuesOnly": false + }, + "label": "name", + "getInitialValue": "default", + "getUndefinedValue": "default" + } + ], + "children": [ + { + "name": "invoice", + "type": "record", + "fields": [ + { + "name": "amount", + "type": "number", + "typeOptions": { + "minValue": 99999999999, + "maxValue": 99999999999, + "decimalPlaces": 2 + }, + "label": "amount", + "getInitialValue": "default", + "getUndefinedValue": "default" + } + ], + "children": [], + "validationRules": [], + "nodeId": 2, + "indexes": [], + "allidsShardFactor": 1, + "collectionName": "invoices", + "isSingle": false + } + ], + "validationRules": [], + "nodeId": 1, + "indexes": [], + "allidsShardFactor": 64, + "collectionName": "customers", + "isSingle": false + } + ], + "pathMaps": [], + "indexes": [], + "nodeId": 0 + }, + "actions": { + "output_to_file": { + "name": "output_to_file", + "behaviourSource": "main", + "behaviourName": "outputToFile", + "initialOptions": {} + } + }, + "triggers": [ + { + "actionName": "output_to_file", + "eventName": "authApi:createUser:onComplete", + "optionsCreator": "return { filename:'tempaccess' + context.user.name, content:context.result.tempCode };", + "condition": "!context.password" + } + ] +} \ No newline at end of file diff --git a/packages/server/runtime_apps/testApp/3-7HHy_x966Th/plugins.js b/packages/server/runtime_apps/testApp/3-Y5vsgF-ZEjh/plugins.js similarity index 100% rename from packages/server/runtime_apps/testApp/3-7HHy_x966Th/plugins.js rename to packages/server/runtime_apps/testApp/3-Y5vsgF-ZEjh/plugins.js diff --git a/packages/server/tests/all.spec.js b/packages/server/tests/all.spec.js index bdee95396..63968a922 100644 --- a/packages/server/tests/all.spec.js +++ b/packages/server/tests/all.spec.js @@ -6,7 +6,8 @@ beforeAll(async () => await app.start()) afterAll(async () => await app.destroy()) -describe("authenticateMaster", () => authenticateMaster(app)); +describe("authenticateMaster", () => authenticateMaster(app, "_master", () => app.masterAuth)); describe("createNewApp", () => createNewApp(app)); +describe("authenticateTestApp", () => authenticateMaster(app, "testApp", () => app.user1_instance1)); diff --git a/packages/server/tests/authenticate.js b/packages/server/tests/authenticate.js index 9937de9e1..d5bf37d75 100644 --- a/packages/server/tests/authenticate.js +++ b/packages/server/tests/authenticate.js @@ -2,27 +2,29 @@ const statusCodes = require("../utilities/statusCodes"); const { readFile } = require("../utilities/fsawait"); const { timeout } = require("./helpers"); -module.exports = (app) => { +module.exports = (app, appName) => { + + const credentials = app.credentials[appName]; it("should return unauthorized if username is incorrect", async () => { - await app.post("/_master/api/authenticate", { + await app.post(`/${appName}/api/authenticate`, { username: "unknownuser", - password: app.masterAuth.password + password: credentials.password }) .expect(statusCodes.UNAUTHORIZED); }) it("should return unauthorized if password is incorrect", async () => { - await app.post("/_master/api/authenticate", { - username: app.masterAuth.username, + await app.post(`/${appName}/api/authenticate`, { + username: credentials.username, password: "incorrect_password" }) .expect(statusCodes.UNAUTHORIZED); }) it("should not get cookie when unauthorized", async () => { - const response = await app.post("/_master/api/authenticate", { - username: app.masterAuth.username, + const response = await app.post(`/${appName}/api/authenticate`, { + username: credentials.username, password: "incorrect_password" }); @@ -32,20 +34,20 @@ module.exports = (app) => { it("should return ok correct username and password supplied", async () => { - const response = await app.post("/_master/api/authenticate", { - username: app.masterAuth.username, - password: app.masterAuth.password + const response = await app.post(`/${appName}/api/authenticate`, { + username: credentials.username, + password: credentials.password }) .expect(statusCodes.OK); - app.masterAuth.cookie = response.header['set-cookie']; + credentials.cookie = response.header['set-cookie']; }); - const testUserName = "test_user"; + const testUserName = appName + "_test_user"; let testPassword = "test_user_password"; it("should be able to create new user with authenticated cookie", async () => { - await app.post("/_master/api/createUser", { + await app.post(`/${appName}/api/createUser`, { user: { name: testUserName, accessLevels:["owner"], @@ -54,7 +56,7 @@ module.exports = (app) => { }, password: testPassword }) - .set("cookie", app.masterAuth.cookie) + .set("cookie", credentials.cookie) .expect(statusCodes.OK); @@ -63,7 +65,7 @@ module.exports = (app) => { let newUserCookie; it("should be able to authenticate with new user", async () => { - const responseNewUser = await app.post("/_master/api/authenticate", { + const responseNewUser = await app.post(`/${appName}/api/authenticate`, { username: testUserName, password: testPassword }) @@ -72,7 +74,7 @@ module.exports = (app) => { newUserCookie = responseNewUser.header['set-cookie']; expect(newUserCookie).toBeDefined(); - expect(newUserCookie).not.toEqual(app.masterAuth.cookie); + expect(newUserCookie).not.toEqual(credentials.cookie); app.get("/_master/api/users/") .set("cookie", newUserCookie) @@ -81,17 +83,18 @@ module.exports = (app) => { it("should not be able to perform requests when user is disabled", async () => { - await app.post("/_master/api/disableUser", { + //HERE + await app.post(`/${appName}/api/disableUser`, { username: testUserName }) - .set("cookie", app.masterAuth.cookie) + .set("cookie", credentials.cookie) .expect(statusCodes.OK); - await app.get("/_master/api/users/") + await app.get(`/${appName}/api/users`) .set("cookie", newUserCookie) .expect(statusCodes.UNAUTHORIZED); - await app.post("/_master/api/authenticate", { + await app.post(`/${appName}/api/authenticate`, { username: testUserName, password: testPassword }) @@ -100,7 +103,7 @@ module.exports = (app) => { }); it("should not be able to re-authenticate when user is disabled", async () => { - await app.post("/_master/api/authenticate", { + await app.post(`/${appName}/api/authenticate`, { username: testUserName, password: testPassword }) @@ -109,13 +112,13 @@ module.exports = (app) => { it("should be able with re-authenticate when user is enabled again", async () => { - await app.post("/_master/api/enableUser", { + await app.post(`/${appName}/api/enableUser`, { username: testUserName }) - .set("cookie", app.masterAuth.cookie) + .set("cookie", credentials.cookie) .expect(statusCodes.OK); - await app.post("/_master/api/authenticate", { + await app.post(`/${appName}/api/authenticate`, { username: testUserName, password: testPassword }) @@ -125,7 +128,7 @@ module.exports = (app) => { let testUserTempCode; it("should be able to reset password with temporary access", async () => { - await app.post("/_master/api/createTemporaryAccess", { + await app.post(`/${appName}/api/createTemporaryAccess`, { username: testUserName }) .expect(statusCodes.OK); @@ -138,14 +141,14 @@ module.exports = (app) => { const testUserTempCode = await readFile(`./tests/.data/tempaccess${testUserName}`, "utf8"); - await app.post("/_master/api/setPasswordFromTemporaryCode", { + await app.post(`/${appName}/api/setPasswordFromTemporaryCode`, { username: testUserName, tempCode:testUserTempCode, newPassword:testPassword }) .expect(statusCodes.OK); - await app.post("/_master/api/authenticate", { + await app.post(`/${appName}/api/authenticate`, { username: testUserName, password: testPassword }) @@ -157,20 +160,20 @@ module.exports = (app) => { it("should not be able to set password with used temp code", async () => { - await app.post("/_master/api/setPasswordFromTemporaryCode", { + await app.post(`/${appName}/api/setPasswordFromTemporaryCode`, { username: testUserName, tempCode:testUserTempCode, newPassword:"whatever" }) .expect(statusCodes.OK); - await app.post("/_master/api/authenticate", { + await app.post(`/${appName}/api/authenticate`, { username: testUserName, password: "whatever" }) .expect(statusCodes.UNAUTHORIZED); - await app.post("/_master/api/authenticate", { + await app.post(`/${appName}/api/authenticate`, { username: testUserName, password: testPassword }) diff --git a/packages/server/tests/createNewApp.js b/packages/server/tests/createNewApp.js index e29630127..18dad0caf 100644 --- a/packages/server/tests/createNewApp.js +++ b/packages/server/tests/createNewApp.js @@ -25,11 +25,11 @@ module.exports = (app) => { newAppKey = newApp.key; await app.post(`/_master/api/record/${newApp.key}`, newApp) - .set("cookie", app.masterAuth.cookie) + .set("cookie", app.credentials._master.cookie) .expect(statusCodes.OK); const response = await app.get(`/_master/api/record/${newApp.key}`) - .set("cookie", app.masterAuth.cookie) + .set("cookie", app.credentials._master.cookie) .expect(statusCodes.OK); expect(response.body.name).toBe(newApp.name); @@ -49,12 +49,12 @@ module.exports = (app) => { version1.package = { relativePath: "package.tar.gz", size}; await app.post(`/_master/api/record/${version1.key}`, version1) - .set("cookie", app.masterAuth.cookie) + .set("cookie", app.credentials._master.cookie) .expect(statusCodes.OK); await app.post(`/_master/api/files/${version1.key}`) .attach("file", path, "package.tar.gz") - .set("cookie", app.masterAuth.cookie) + .set("cookie", app.credentials._master.cookie) .expect(statusCodes.OK); }); @@ -69,11 +69,11 @@ module.exports = (app) => { instance1.version = {key:version1Key, name:"v1", defaultAccessLevel:"owner"}; await app.post(`/_master/api/record/${instance1.key}`, instance1) - .set("cookie", app.masterAuth.cookie) + .set("cookie", app.credentials._master.cookie) .expect(statusCodes.OK); const loadInstanceResponse = await app.get(`/_master/api/record/${instance1.key}`) - .set("cookie", app.masterAuth.cookie) + .set("cookie", app.credentials._master.cookie) .expect(statusCodes.OK); instance1 = loadInstanceResponse.body; @@ -85,36 +85,37 @@ module.exports = (app) => { const master = await getmaster(); user1_instance1 = master.recordApi .getNew(`${newAppKey}/users`, "user"); - user1_instance1.name = "testAppUser1"; + user1_instance1.name = app.credentials.testApp.username; /*const lookupResponse = await app.get(`/_master/api/lookup_field/${user1_instance1.key}?fields=instance`) - .set("cookie", app.masterAuth.cookie) + .set("cookie", app.credentials._master.cookie) .expect(statusCodes.OK); */ user1_instance1.instance = instance1; user1_instance1.active = true; //await timeout(100); await app.post(`/_master/api/record/${user1_instance1.key}`, user1_instance1) - .set("cookie", app.masterAuth.cookie) + .set("cookie", app.credentials._master.cookie) .expect(statusCodes.OK); }); it("should be able to set password for new user using temporary code", async () => { const testUserTempCode = await readFile(`./tests/.data/tempaccess${user1_instance1.name}`, "utf8"); - user1_instance1.password = "user1_instance1_password"; + user1_instance1.password = app.credentials.testApp.password; await app.post("/testApp/api/setPasswordFromTemporaryCode", { - username: user1_instance1.name, + username: app.credentials.testApp.username, tempCode:testUserTempCode, - newPassword:user1_instance1.password + newPassword:app.credentials.testApp.password }) .expect(statusCodes.OK); await app.post("/testApp/api/authenticate", { - username: user1_instance1.name, - password: user1_instance1.password + username: app.credentials.testApp.username, + password: app.credentials.testApp.password }) .expect(statusCodes.OK); + }) } \ No newline at end of file diff --git a/packages/server/tests/testApp.js b/packages/server/tests/testApp.js index 8fffaba32..c221b2b17 100644 --- a/packages/server/tests/testApp.js +++ b/packages/server/tests/testApp.js @@ -68,11 +68,19 @@ module.exports = () => { server:() => server, post: (url, body) => postRequest(server,url,body), get: (url) => getRequest(server, url), - masterAuth: { - username: masterOwnerName, - password: masterOwnerPassword, - cookie: "" + credentials: { + _master: { + username: masterOwnerName, + password: masterOwnerPassword, + cookie: "" + }, + testApp: { + username: "testAppUser1", + password: "user1_instance1_password", + cookie: "" + } }, + testAppInfo: { name: "testApp" }, diff --git a/packages/server/utilities/createAppPackage.js b/packages/server/utilities/createAppPackage.js index bb0a5c304..1582647b7 100644 --- a/packages/server/utilities/createAppPackage.js +++ b/packages/server/utilities/createAppPackage.js @@ -9,11 +9,16 @@ const createAppPackage = (config, appPath) => { const pluginsModule = require( join(appPath, "plugins.js")); + + const accessLevels = require( + join(appPath, "access_levels.json") + ); return ({ appDefinition: appDefModule, behaviourSources: pluginsModule(config), - appPath + appPath, + accessLevels }) } diff --git a/packages/server/utilities/masterAppInternal.js b/packages/server/utilities/masterAppInternal.js index 74e3e42b4..e9d7e67c0 100644 --- a/packages/server/utilities/masterAppInternal.js +++ b/packages/server/utilities/masterAppInternal.js @@ -89,6 +89,7 @@ module.exports = async (config) => { const app = await getApplication(appname); const userInMaster = await getUser(bb, app.id, username); + if(!userInMaster) return null; const instance = await bb.recordApi.load( userInMaster.instanceKey); @@ -115,6 +116,7 @@ module.exports = async (config) => { session.user_json = JSON.stringify(authUser); session.instanceDatastoreConfig = instance.datastoreconfig; session.username = username; + session.instanceVersion = instance.version.key; await bb.recordApi.save(session); return session; }; @@ -140,9 +142,14 @@ module.exports = async (config) => { const session = await bb.recordApi.load(`/applications/${app.id}/sessions/${customId}`); const dsConfig = JSON.parse(session.instanceDatastoreConfig); const instanceDatastore = getInstanceDatastore(dsConfig) + const versionId = $(session.instanceVersion,[ + splitKey, + last + ]); + return await getApisForSession( instanceDatastore, - applictionVersionPackage(config, appname, session.instanceVersion), + applictionVersionPackage(config, appname, versionId), session); } catch(_) { return null; @@ -172,6 +179,8 @@ module.exports = async (config) => { const app = await getApplication(appname); const user = await getUser(bb, app.id, username); + if(!user) return null; + const dsConfig = JSON.parse(user.instanceDatastoreConfig); const instanceDatastore = getInstanceDatastore( dsConfig