From 2781a9fabb743d4e5d03846f3a9af4a83b868d08 Mon Sep 17 00:00:00 2001 From: michael shanks Date: Tue, 2 Jul 2019 23:15:15 +0100 Subject: [PATCH] instance - create user started --- .../appPackages/master/appDefinition.json | 26 +- packages/server/appPackages/master/main.js | 43 +- .../appPackages/testApp/appDefinition.json | 446 +++++++++++++++++- .../server/appPackages/testApp/plugins.js | 9 + packages/server/tests/createNewApp.js | 7 +- 5 files changed, 525 insertions(+), 6 deletions(-) create mode 100644 packages/server/appPackages/testApp/plugins.js diff --git a/packages/server/appPackages/master/appDefinition.json b/packages/server/appPackages/master/appDefinition.json index 29f29fa89..f9a0d7b6d 100644 --- a/packages/server/appPackages/master/appDefinition.json +++ b/packages/server/appPackages/master/appDefinition.json @@ -186,6 +186,18 @@ "label": "Name", "getInitialValue": "default", "getUndefinedValue": "default" + }, + { + "name": "defaultAccessLevel", + "type": "string", + "typeOptions": { + "maxLength": 200, + "values": null, + "allowDeclaredValuesOnly": false + }, + "label": "Default Access Level", + "getInitialValue": "default", + "getUndefinedValue": "default" } ], "children": [], @@ -424,6 +436,12 @@ "behaviourSource": "main", "behaviourName": "initialiseInstance", "initialOptions": {} + }, + "create_user": { + "name": "create_user", + "behaviourSource": "main", + "behaviourName": "createNewUser", + "initialOptions": {} } }, "triggers": [ @@ -431,7 +449,13 @@ "actionName": "initialise_instance", "eventName": "recordApi:save:onRecordCreated", "optionsCreator": "return { instance:context.record, apis };", - "condition": "context.record.type === \"instance\"" + "condition": "record.type === \"instance\"" + }, + { + "actionName": "create_user", + "eventName": "recordApi:save:onRecordCreated", + "optionsCreator": "return ({ user:context.record, apis });", + "condition": "context.record.type === \"user\"" } ] } \ No newline at end of file diff --git a/packages/server/appPackages/master/main.js b/packages/server/appPackages/master/main.js index 2ac149734..d29bc6f44 100644 --- a/packages/server/appPackages/master/main.js +++ b/packages/server/appPackages/master/main.js @@ -8,8 +8,12 @@ const { getRuntimePackageDirectory } = require("../../utilities/runtimePackages" const { exists } = require("../../utilities/fsawait"); const createInstanceDb = require("../../initialise/createInstanceDb"); const { createWriteStream } = require("fs"); +const { applictionVersionPackage } = require("../../utilities/createAppPackage"); +const { getApisWithFullAccess } = require("../../utilities/budibaseApi"); -module.exports = (config) => ({ +module.exports = (config) => { + const datastoreModule = require(`../../../datastores/datastores/${config.datastore}`); + return ({ initialiseInstance : async ({ instance, apis }) => { const appKey = $(instance.key, [ splitKey, @@ -19,7 +23,6 @@ module.exports = (config) => ({ const application = await apis.recordApi.load(appKey); - const datastoreModule = require(`../../../datastores/datastores/${config.datastore}`); const dbConfig = await createInstanceDb( datastoreModule, config.datastoreConfig, @@ -43,8 +46,42 @@ module.exports = (config) => ({ instance.datastoreconfig = JSON.stringify(dbConfig); instance.isNew = false; await apis.recordApi.save(instance); + }, + + createNewUser: async ({user, apis}) => { + const instance = apis.recordApi.load(user.instance.key); + + const appKey = $(instance.key, [ + splitKey, + take(2), + joinKey + ]); + + const application = await apis.recordApi.load(appKey); + + const versionId = $(instance.version.key, [ + splitKey, + takeRight(1), + joinKey + ]); + + const appPackage = applictionVersionPackage( + application.name, + versionId); + + const instanceApis = getApisWithFullAccess( + datastoreModule.getDatastore(instance.datastoreconfig), + appPackage); + + const authUser = instanceApis.authApi.getNewUser(); + authUser.name = user.name; + authUser.accessLevels = [instance.version.defaultAccessLevel]; + await instanceApis.authApi.createUser(authUser); + } -}); + + }); +} const downloadAppPackage = async (apis, instance, appName, versionId) => { const inputStream = await apis.recordApi.downloadFile(instance.version.key, "package.tar.gz"); diff --git a/packages/server/appPackages/testApp/appDefinition.json b/packages/server/appPackages/testApp/appDefinition.json index 61f0510f5..582ff5b39 100644 --- a/packages/server/appPackages/testApp/appDefinition.json +++ b/packages/server/appPackages/testApp/appDefinition.json @@ -1 +1,445 @@ -{"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.name, instanceDatastoreConfig:instance.datastoreconfig};","filter":"","indexType":"ancestor","getShardName":"return record.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":{},"triggers":[]} \ No newline at end of file +{ + "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.name, instanceDatastoreConfig:instance.datastoreconfig};", + "filter": "", + "indexType": "ancestor", + "getShardName": "return record.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: filename:'tempaccess' + context.user.name, content:context.result.tempCode };", + "condition": "" + } + ] +} \ No newline at end of file diff --git a/packages/server/appPackages/testApp/plugins.js b/packages/server/appPackages/testApp/plugins.js new file mode 100644 index 000000000..838a10a7f --- /dev/null +++ b/packages/server/appPackages/testApp/plugins.js @@ -0,0 +1,9 @@ +const fs = require("fs"); + +module.exports = (config) => ({ + main: { + outputToFile : ({filename, content}) => { + fs.writeFile(`./tests/.data/${filename}`, content, {encoding:"utf8"}); + } + } +}) \ No newline at end of file diff --git a/packages/server/tests/createNewApp.js b/packages/server/tests/createNewApp.js index 07449fe8b..48fb98bb1 100644 --- a/packages/server/tests/createNewApp.js +++ b/packages/server/tests/createNewApp.js @@ -64,13 +64,18 @@ module.exports = (app) => { .getNew(`${newAppKey}/instances`, "instance"); instance1.name = "instance 1"; instance1.active = true; - instance1.version = {key:version1Key, name:"v1"}; + instance1.version = {key:version1Key, name:"v1", defaultAccessLevel:"owner"}; instance1Key = instance1.key; await app.post(`/_master/api/record/${instance1.key}`, instance1) .set("cookie", app.masterAuth.cookie) .expect(statusCodes.OK); + }); + + it("should be able to create new user on instance", async () => { + + });