From 51b6687262fd433199599f5fea0cb13264b56e51 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 10 Nov 2021 12:00:29 +0000 Subject: [PATCH] Updating per review comments. --- packages/auth/src/db/utils.js | 18 ++++++++++++++++++ packages/server/src/api/controllers/user.js | 18 ++++++++---------- packages/server/src/api/routes/user.js | 2 +- packages/worker/src/utilities/appService.js | 21 ++++++++++++++------- 4 files changed, 41 insertions(+), 18 deletions(-) diff --git a/packages/auth/src/db/utils.js b/packages/auth/src/db/utils.js index 03bd773922..b956089660 100644 --- a/packages/auth/src/db/utils.js +++ b/packages/auth/src/db/utils.js @@ -259,6 +259,24 @@ exports.getAllApps = async (CouchDB, { dev, all, idsOnly } = {}) => { } } +/** + * Utility function for getAllApps but filters to production apps only. + */ +exports.getDeployedAppIDs = async CouchDB => { + return (await exports.getAllApps(CouchDB, { idsOnly: true })).filter( + id => !exports.isDevAppID(id) + ) +} + +/** + * Utility function for the inverse of above. + */ +exports.getDevAppIDs = async CouchDB => { + return (await exports.getAllApps(CouchDB, { idsOnly: true })).filter(id => + exports.isDevAppID(id) + ) +} + exports.dbExists = async (CouchDB, dbName) => { let exists = false try { diff --git a/packages/server/src/api/controllers/user.js b/packages/server/src/api/controllers/user.js index 5faf821349..908018fe51 100644 --- a/packages/server/src/api/controllers/user.js +++ b/packages/server/src/api/controllers/user.js @@ -8,11 +8,7 @@ const { getGlobalUsers, getRawGlobalUser } = require("../../utilities/global") const { getFullUser } = require("../../utilities/users") const { isEqual } = require("lodash") const { BUILTIN_ROLE_IDS } = require("@budibase/auth/roles") -const { - getDevelopmentAppID, - getAllApps, - isDevAppID, -} = require("@budibase/auth/db") +const { getDevelopmentAppID, getDeployedAppIDs } = require("@budibase/auth/db") const { doesDatabaseExist } = require("../../utilities") const { UserStatus } = require("@budibase/auth/constants") @@ -78,8 +74,12 @@ exports.syncUser = async function (ctx) { try { user = await getRawGlobalUser(userId) } catch (err) { - user = {} - deleting = true + if (err && err.status === 404) { + user = {} + deleting = true + } else { + throw err + } } const roles = user.roles // remove props which aren't useful to metadata @@ -90,9 +90,7 @@ exports.syncUser = async function (ctx) { let prodAppIds // if they are a builder then get all production app IDs if ((user.builder && user.builder.global) || deleting) { - prodAppIds = (await getAllApps(CouchDB, { idsOnly: true })).filter( - id => !isDevAppID(id) - ) + prodAppIds = await getDeployedAppIDs(CouchDB) } else { prodAppIds = Object.entries(roles) .filter(entry => entry[1] !== BUILTIN_ROLE_IDS.PUBLIC) diff --git a/packages/server/src/api/routes/user.js b/packages/server/src/api/routes/user.js index 43c08a7f33..a3043b5af1 100644 --- a/packages/server/src/api/routes/user.js +++ b/packages/server/src/api/routes/user.js @@ -35,7 +35,7 @@ router controller.destroyMetadata ) .post( - "/api/users/sync/:id", + "/api/users/metadata/sync/:id", authorized(PermissionTypes.USER, PermissionLevels.WRITE), controller.syncUser ) diff --git a/packages/worker/src/utilities/appService.js b/packages/worker/src/utilities/appService.js index 166a82f78b..92809c9046 100644 --- a/packages/worker/src/utilities/appService.js +++ b/packages/worker/src/utilities/appService.js @@ -4,7 +4,7 @@ const { getTenantId, isTenantIdSet } = require("@budibase/auth/tenancy") const { checkSlashesInUrl } = require("../utilities") const env = require("../environment") -exports.syncUserInApps = async userId => { +async function makeAppRequest(url, method, body) { if (env.isTest()) { return } @@ -13,12 +13,19 @@ exports.syncUserInApps = async userId => { if (isTenantIdSet()) { request.headers[Headers.TENANT_ID] = getTenantId() } - request.headers["Content-Type"] = "application/json" - request.body = JSON.stringify({}) - request.method = "POST" - const response = await fetch( - checkSlashesInUrl(env.APPS_URL + `/api/users/sync/${userId}`), - request + if (body) { + request.headers["Content-Type"] = "application/json" + request.body = JSON.stringify(body) + } + request.method = method + return fetch(checkSlashesInUrl(env.APPS_URL + url), request) +} + +exports.syncUserInApps = async userId => { + const response = await makeAppRequest( + `/api/users/metadata/sync/${userId}`, + "POST", + {} ) if (response.status !== 200) { throw "Unable to sync user."