From 946c79df53aa29c315ac6bebb4df5d16f8b8396d Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Sat, 28 Mar 2020 06:39:22 +0000 Subject: [PATCH 1/2] bugfix: clearing sessions on backend update --- packages/core/src/collectionApi/delete.js | 11 +++++++++-- packages/core/src/collectionApi/initialise.js | 2 +- packages/core/test/collectionApi.delete.spec.js | 10 +++++++--- .../server/middleware/routeHandlers/upgradeData.js | 1 + packages/server/utilities/masterAppInternal.js | 12 +++++++++++- 5 files changed, 29 insertions(+), 7 deletions(-) diff --git a/packages/core/src/collectionApi/delete.js b/packages/core/src/collectionApi/delete.js index b93b65b0a9..897c199c46 100644 --- a/packages/core/src/collectionApi/delete.js +++ b/packages/core/src/collectionApi/delete.js @@ -3,6 +3,8 @@ import { _deleteRecord } from "../recordApi/delete" import { getAllIdsIterator } from "../indexing/allIds" import { permission } from "../authApi/permissions" import { getCollectionDir } from "../recordApi/recordInfo" +import { ensureCollectionIsInitialised } from "./initialise" +import { getNodeForCollectionPath } from "../templateApi/hierarchy" export const deleteCollection = (app, disableCleanup = false) => async key => apiWrapper( @@ -25,14 +27,19 @@ export const _deleteCollection = async (app, key, disableCleanup) => { key = safeKey(key) const collectionDir = getCollectionDir(app.hierarchy, key) await deleteRecords(app, key) - await deleteCollectionFolder(app, collectionDir) + await deleteCollectionFolder(app, key, collectionDir) if (!disableCleanup) { await app.cleanupTransactions() } } -const deleteCollectionFolder = async (app, dir) => +const deleteCollectionFolder = async (app, key, dir) => { await app.datastore.deleteFolder(dir) + await ensureCollectionIsInitialised( + app.datastore, + getNodeForCollectionPath(app.hierarchy)(key), + dir) +} const deleteRecords = async (app, key) => { const iterate = await getAllIdsIterator(app)(key) diff --git a/packages/core/src/collectionApi/initialise.js b/packages/core/src/collectionApi/initialise.js index 8190c58db8..3aac1c2889 100644 --- a/packages/core/src/collectionApi/initialise.js +++ b/packages/core/src/collectionApi/initialise.js @@ -6,7 +6,7 @@ import { } from "../templateApi/hierarchy" import { $, allTrue, joinKey } from "../common" -const ensureCollectionIsInitialised = async (datastore, node, dir) => { +export const ensureCollectionIsInitialised = async (datastore, node, dir) => { if (!(await datastore.exists(dir))) { await datastore.createFolder(dir) await datastore.createFolder(joinKey(dir, node.nodeId)) diff --git a/packages/core/test/collectionApi.delete.spec.js b/packages/core/test/collectionApi.delete.spec.js index 09bddb8ef2..8841cbc051 100644 --- a/packages/core/test/collectionApi.delete.spec.js +++ b/packages/core/test/collectionApi.delete.spec.js @@ -8,7 +8,7 @@ import { permission } from "../src/authApi/permissions" describe("collectionApi > delete", () => { it("should remove every key in collection's path", async () => { - const { recordApi, collectionApi } = await setupApphierarchy( + const { recordApi, collectionApi, appHierarchy } = await setupApphierarchy( basicAppHierarchyCreator_WithFields ) const record1 = recordApi.getNew("/customers", "customer") @@ -31,7 +31,10 @@ describe("collectionApi > delete", () => { filter(k => splitKey(k)[0] === "customers"), ]) - expect(remainingKeys).toEqual([]) + expect(remainingKeys).toEqual([ + "/customers", + `/customers/${appHierarchy.customerRecord.nodeId}`, + ]) }) it("should not delete anything that is not in its path", async () => { @@ -51,7 +54,8 @@ describe("collectionApi > delete", () => { filter(k => splitKey(k)[0] === "customers"), ]) - const expectedRemainingKeys = allKeys.length - customerKeys.length + const expectedRemainingKeys = allKeys.length - customerKeys.length + 2 + // +2 because is should keep the collection folders: /customers & /customers/1 await collectionApi.delete("/customers") diff --git a/packages/server/middleware/routeHandlers/upgradeData.js b/packages/server/middleware/routeHandlers/upgradeData.js index 24026ace66..a9f2fcf349 100644 --- a/packages/server/middleware/routeHandlers/upgradeData.js +++ b/packages/server/middleware/routeHandlers/upgradeData.js @@ -6,5 +6,6 @@ module.exports = async ctx => { accessLevels.version = existingAccessLevels.version await ctx.instance.authApi.saveAccessLevels(accessLevels) await ctx.instance.templateApi.upgradeData(ctx.request.body.newHierarchy) + await ctx.master.clearAllSessions(ctx.params.appname) ctx.response.status = StatusCodes.OK } diff --git a/packages/server/utilities/masterAppInternal.js b/packages/server/utilities/masterAppInternal.js index e02862f61c..3cb358434e 100644 --- a/packages/server/utilities/masterAppInternal.js +++ b/packages/server/utilities/masterAppInternal.js @@ -325,6 +325,15 @@ module.exports = async context => { } } + const clearAllSessions = async appname => { + if (isMaster(appname)) { + await bb.collectionApi.delete("/mastersessions") + } else { + const app = await getApplication(appname) + await bb.collectionApi.delete(`/applications/${app.id}/sessions`) + } + } + const getApplicationWithInstances = async appname => { const app = cloneDeep(await getApplication(appname)) app.instances = await bb.indexApi.listItems( @@ -346,7 +355,7 @@ module.exports = async context => { await bb.recordApi.save(userInMaster) } - const deleteLatestPackageFromCache = (appname) => { + const deleteLatestPackageFromCache = appname => { deleteCachedPackage(context, appname, LATEST_VERSIONID) } @@ -370,5 +379,6 @@ module.exports = async context => { getFullAccessApiForMaster, getApplicationWithInstances, deleteLatestPackageFromCache, + clearAllSessions, } } From fbfca2370bd649c76f21ee5776b7d6219ffca46b Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Sat, 28 Mar 2020 06:40:01 +0000 Subject: [PATCH 2/2] bugfix: better logging on create user error --- packages/core/src/authApi/createUser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/authApi/createUser.js b/packages/core/src/authApi/createUser.js index 3693bd4347..fa953f9c9f 100644 --- a/packages/core/src/authApi/createUser.js +++ b/packages/core/src/authApi/createUser.js @@ -44,7 +44,7 @@ export const _createUser = async (app, user, password = null) => { const userErrors = validateUser(app)([...users, user], user) if (userErrors.length > 0) { - throw new BadRequestError(`User is invalid. ${join("; ")(userErrors)}`) + throw new BadRequestError(`User is invalid. ${join("; ")(userErrors.map(e => e.error))}`) } const { auth, tempCode, temporaryAccessId } = await getAccess(app, password)