test app working tf

This commit is contained in:
michael shanks 2019-07-11 07:35:29 +01:00
parent 991bd69671
commit 98127d6eb8
10 changed files with 65 additions and 36 deletions

View File

@ -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);
}
});

View File

@ -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": ""
}
]
}

View File

@ -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": ""
}
]
}

View File

@ -87,7 +87,7 @@ module.exports = (app) => {
.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);

View File

@ -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));

View File

@ -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',

View File

@ -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
});