From 98127d6eb8e8685db50bbfe1f7a5e05ae56deb71 Mon Sep 17 00:00:00 2001 From: michael shanks Date: Thu, 11 Jul 2019 07:35:29 +0100 Subject: [PATCH] test app working tf --- packages/server/appPackages/master/main.js | 5 +- .../appPackages/testApp/appDefinition.json | 6 ++ .../appPackages/testApp/dist/package.tar.gz | Bin 1185 -> 1210 bytes .../access_levels.json | 0 .../appDefinition.json | 6 ++ .../plugins.js | 0 packages/server/tests/createNewApp.js | 2 +- .../utilities/injectedPlugins/createUser.js | 15 +++-- .../server/utilities/injectedPlugins/index.js | 8 +-- .../server/utilities/masterAppInternal.js | 59 ++++++++++-------- 10 files changed, 65 insertions(+), 36 deletions(-) rename packages/server/runtime_apps/testApp/{3-M8CjoLngYZw => 3-Ch-Pk0Sm6Xb}/access_levels.json (100%) rename packages/server/runtime_apps/testApp/{3-M8CjoLngYZw => 3-Ch-Pk0Sm6Xb}/appDefinition.json (89%) rename packages/server/runtime_apps/testApp/{3-M8CjoLngYZw => 3-Ch-Pk0Sm6Xb}/plugins.js (100%) diff --git a/packages/server/appPackages/master/main.js b/packages/server/appPackages/master/main.js index b53de8124..ac0ff8a73 100644 --- a/packages/server/appPackages/master/main.js +++ b/packages/server/appPackages/master/main.js @@ -52,6 +52,9 @@ module.exports = (context) => { }, createNewUser: async ({user, apis}) => { + + if(!user.createdByMaster) return; + const instance = await apis.recordApi.load(user.instance.key); const appKey = $(instance.key, [ @@ -81,8 +84,8 @@ module.exports = (context) => { const authUser = instanceApis.authApi.getNewUser(); authUser.name = user.name; authUser.accessLevels = [instance.version.defaultAccessLevel]; + await instanceApis.authApi.createUser(authUser); - } }); diff --git a/packages/server/appPackages/testApp/appDefinition.json b/packages/server/appPackages/testApp/appDefinition.json index 17b992b18..871524cb9 100644 --- a/packages/server/appPackages/testApp/appDefinition.json +++ b/packages/server/appPackages/testApp/appDefinition.json @@ -73,6 +73,12 @@ "eventName": "authApi:createUser:onComplete", "optionsCreator": "return { filename:'tempaccess' + context.user.name, content:context.result.tempCode };", "condition": "!context.password" + }, + { + "actionName": "output_to_file", + "eventName": "authApi:createTemporaryAccess:onComplete", + "optionsCreator": "return { filename:'tempaccess' + context.userName, content:context.result };", + "condition": "" } ] } \ 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 4f95910d929db345683972977f3a1369c153ec7e..cd657b26b0c3db14158dfcba3257b47d8e6f07e5 100644 GIT binary patch delta 1198 zcmV;f1X25;3Azc89Dgl8Hy0gHq$p5qO^2o3mmx?Q>1=bLNtLAXK%oD7%&PhJR6UL*e=ftoR`nG}U-rKyh1Tt#krpnw`6 z3IeIZg_u-@4B>aUvTUsY(;;V|bj$zwnuUqC;7l5~DG7nY#+AfTxOq=7TH%IOtecS* z6X%j)(`a>`E`N9rQo3$gJV@z!8GMk+3&qe)1G9)@ko7v%5NllI_nD``kceu1Vf|b~ zp|w^js;)@75#37$2zthWNJT=X-Q&IOPh*zqCi5F_qMmCIngM32V*9fP3808 zzh1A~b@MJ_$R%aFU2#J!Y?#%((i=12VFg}R+EPKf`)yPP^TuZs6O-EEPm!6U*otIgo89Ff zASPytqklf8wXwu;PI(M=vWPif9;Teji+2!ksv!K^@qQBAhIvf6hBALrHILIX;46Kb zfcaq8E#*#d3U$Ub=BAGI3vL#;&gWJXnTx#1;^m~6in7E;iQ_!V!130;*w!_48VZ@6?5nIGLHqxA3__E>WBk9D`w$p zW1&8tQWd_Zfsx8C+a=p_Nt){vOdfAWl1bu359i@6d>x{c-$i<6C1ahdk|U*@k)yk zPYaWA(b!DiMr@Rxb-p0(9!YU1@qC8o(SO1unOb#d*<6OPJ(^eva!ssw8>Z1Kds0eK z`z!Jj!f#>V(ljlYZ(4>%`bn5kHl#q~(rwy#hnOmoye_CjrqDq?tL=wuO64S%wVua} z!sqP3_tyEck$@cC5RV(e;1w$3cdO|0(|OS^VGj{qFYu|0yWq@qdH~ zd-K0J9k=Db+Z*Kn|LG5Un18?H_%~)#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nRcRKEif3H6*{{J%=_A&lJcYnA) z|2+xe)~^kjX!43E_>?dO$KFJHCohi { .getNew(`${newAppKey}/users`, "user"); user1_instance1.name = app.credentials.testApp.username; user1_instance1.createdByMaster = true; - + master.recordApi.setCustomId(user1_instance1, user1_instance1.name); /*const lookupResponse = await app.get(`/_master/api/lookup_field/${user1_instance1.key}?fields=instance`) .set("cookie", app.credentials._master.cookie) .expect(statusCodes.OK); diff --git a/packages/server/utilities/injectedPlugins/createUser.js b/packages/server/utilities/injectedPlugins/createUser.js index 8181cafc9..4d5a4e3fb 100644 --- a/packages/server/utilities/injectedPlugins/createUser.js +++ b/packages/server/utilities/injectedPlugins/createUser.js @@ -1,10 +1,16 @@ -module.exports = ({ masterAppInternal, instanceKey }) => async ({ user }) => { - const { bbMaster } = masterAppInternal; +module.exports = ({ masterAppInternal, instanceKey, app }) => async ({ user }) => { + const { bbMaster } = masterAppInternal; + const existingUser =await masterAppInternal.getUser( + app.id, user.name); + + if(existingUser) return; + const masterUser = bbMaster.recordApi - .getNew(`${newAppKey}/users`, "user"); + .getNew(`${app.key}/users`, "user"); masterUser.name = user.name; + bbMaster.recordApi.setCustomId(masterUser, masterUser.name); masterUser.createdByMaster = false; masterUser.instance = await bbMaster.recordApi .load(instanceKey); @@ -13,5 +19,6 @@ module.exports = ({ masterAppInternal, instanceKey }) => async ({ user }) => { await bbMaster.recordApi.save(masterUser); } - +const timeout = ms => + new Promise(resolve => setTimeout(resolve, ms)); diff --git a/packages/server/utilities/injectedPlugins/index.js b/packages/server/utilities/injectedPlugins/index.js index 74cdc47b9..80695699e 100644 --- a/packages/server/utilities/injectedPlugins/index.js +++ b/packages/server/utilities/injectedPlugins/index.js @@ -2,7 +2,7 @@ const createUser = require("./createUser"); const enableUser = require("./enableUser"); const disableUser = require("./disableUser"); -module.exports = async (appPackage, masterAppInternal, instanceKey, appName) => { +module.exports = async (appPackage, masterAppInternal, appName, instanceKey) => { const plugin = await constructPlugin( masterAppInternal, @@ -21,7 +21,7 @@ const createTriggers = (appPackage) => { appDef.triggers.push({ actionName: 'createUser', eventName: 'authApi:createUser:onComplete', - optionsCreator: 'return {user:context.user};', + optionsCreator: 'return {user:context.result};', condition: '' }); appDef.triggers.push({ @@ -46,13 +46,13 @@ const createActions = (appPackage) => { behaviourName: 'createUser', initialOptions: {} }; - appDef.actions.createUser = { + appDef.actions.enableUser = { name: "enableUser", behaviourSource: '_injected', behaviourName: 'enableUser', initialOptions: {} }; - appDef.actions.createUser = { + appDef.actions.disableUser = { name: "disableUser", behaviourSource: '_injected', behaviourName: 'disableUser', diff --git a/packages/server/utilities/masterAppInternal.js b/packages/server/utilities/masterAppInternal.js index 76eb70f14..18f0506a5 100644 --- a/packages/server/utilities/masterAppInternal.js +++ b/packages/server/utilities/masterAppInternal.js @@ -41,13 +41,21 @@ module.exports = async (context) => { : bb.recordApi.customId("session", sessionId); - const getApplication = async (name) => { - if(applications[name]) - return applications[name]; + 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; + } + + if(isRetry) return; await loadApplications(); - return applications[name]; + return await getApplication(nameOrKey, true); }; const getSession = async (sessionId, appname) => { @@ -90,11 +98,11 @@ module.exports = async (context) => { const app = await getApplication(appname); - const userInMaster = await getUser(bb, app.id, username); + const userInMaster = await getUser(app.id, username); if(!userInMaster) return null; const instance = await bb.recordApi.load( - userInMaster.instanceKey); + userInMaster.instance.key); const versionId = $(instance.version.key, [ splitKey, @@ -165,17 +173,15 @@ module.exports = async (context) => { } }; - 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 getUser = async (appId, username ) => { + const userId = bb.recordApi.customId("user", username); + try { + return await bb.recordApi.load( + `/applications/${appId}/users/${userId}`); + } catch(_) { + //empty + return; + } } const getFullAccessInstanceApiForUsername = async (appname, username) => { @@ -185,22 +191,22 @@ module.exports = async (context) => { } else { const app = await getApplication(appname); - const user = await getUser(bb, app.id, username); + const user = await getUser(app.id, username); if(!user) return null; - const dsConfig = JSON.parse(user.instanceDatastoreConfig); + const dsConfig = JSON.parse(user.instance.datastoreconfig); const instanceDatastore = getInstanceDatastore( dsConfig ); - const versionId = $((await bb.recordApi.load(user.instanceKey)).version.key, [ + const versionId = $((await bb.recordApi.load(user.instance.key)).version.key, [ splitKey, last ]); const appPackage = await applictionVersionPackage( - context, appname, versionId, user.instanceKey); + context, appname, versionId, user.instance.key); return await getApisWithFullAccess( instanceDatastore, @@ -229,8 +235,8 @@ module.exports = async (context) => { const sessions = await bb.indexApi.listItems( `/applications/${app.id}/sessions_by_user`, { - rangeStartParams:{name:username}, - rangeEndParams:{name:username}, + rangeStartParams:{username}, + rangeEndParams:{username}, searchPhrase:`username:${username}` } ); @@ -242,14 +248,14 @@ module.exports = async (context) => { } const disableUser = async (app, username) => { - await removeSessionsForUser(appName, username); - const userInMaster = await getUser(bb, app.id, 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(bb, app.id, username); + const userInMaster = await getUser(app.id, username); userInMaster.active = true; await bb.recordApi.save(userInMaster); } @@ -264,6 +270,7 @@ module.exports = async (context) => { removeSessionsForUser, disableUser, enableUser, + getUser, bbMaster:bb });