From 6e10c1fa7348ef8446243fe9062034c7607c46fa Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Thu, 30 Sep 2021 13:08:07 +0100 Subject: [PATCH] Remove global info users on user deletion --- packages/auth/src/tenancy/deprovision.js | 16 ++++++++++++++++ .../worker/src/api/controllers/global/users.js | 2 ++ 2 files changed, 18 insertions(+) diff --git a/packages/auth/src/tenancy/deprovision.js b/packages/auth/src/tenancy/deprovision.js index b8e5bc82cf..608ca1b84a 100644 --- a/packages/auth/src/tenancy/deprovision.js +++ b/packages/auth/src/tenancy/deprovision.js @@ -19,6 +19,22 @@ const removeTenantFromInfoDB = async tenantId => { } } +exports.removeUserFromInfoDB = async dbUser => { + const infoDb = getDB(PLATFORM_INFO_DB) + const keys = [dbUser._id, dbUser.email] + const userDocs = await infoDb.allDocs({ + keys, + include_docs: true, + }) + const toDelete = userDocs.rows.map(row => { + return { + ...row.doc, + _deleted: true, + } + }) + await infoDb.bulkDocs(toDelete) +} + const removeUsersFromInfoDB = async tenantId => { try { const globalDb = getGlobalDB(tenantId) diff --git a/packages/worker/src/api/controllers/global/users.js b/packages/worker/src/api/controllers/global/users.js index 8d4313fc12..ed5fd56ee0 100644 --- a/packages/worker/src/api/controllers/global/users.js +++ b/packages/worker/src/api/controllers/global/users.js @@ -19,6 +19,7 @@ const { tryAddTenant, updateTenantId, } = require("@budibase/auth/tenancy") +const { removeUserFromInfoDB } = require("@budibase/auth/deprovision") const env = require("../../../environment") const PLATFORM_INFO_DB = StaticDatabases.PLATFORM_INFO.name @@ -193,6 +194,7 @@ exports.adminUser = async ctx => { exports.destroy = async ctx => { const db = getGlobalDB() const dbUser = await db.get(ctx.params.id) + await removeUserFromInfoDB(dbUser) await db.remove(dbUser._id, dbUser._rev) await userCache.invalidateUser(dbUser._id) await invalidateSessions(dbUser._id)