Updating per review comments.

This commit is contained in:
mike12345567 2021-11-10 12:00:29 +00:00
parent b3590537ba
commit 935fc06edc
4 changed files with 41 additions and 18 deletions

View File

@ -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 {

View File

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

View File

@ -35,7 +35,7 @@ router
controller.destroyMetadata
)
.post(
"/api/users/sync/:id",
"/api/users/metadata/sync/:id",
authorized(PermissionTypes.USER, PermissionLevels.WRITE),
controller.syncUser
)

View File

@ -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."