Updating per review comments.

This commit is contained in:
mike12345567 2021-11-10 12:00:29 +00:00
parent 472af5d4e5
commit 51b6687262
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) => { exports.dbExists = async (CouchDB, dbName) => {
let exists = false let exists = false
try { try {

View File

@ -8,11 +8,7 @@ const { getGlobalUsers, getRawGlobalUser } = require("../../utilities/global")
const { getFullUser } = require("../../utilities/users") const { getFullUser } = require("../../utilities/users")
const { isEqual } = require("lodash") const { isEqual } = require("lodash")
const { BUILTIN_ROLE_IDS } = require("@budibase/auth/roles") const { BUILTIN_ROLE_IDS } = require("@budibase/auth/roles")
const { const { getDevelopmentAppID, getDeployedAppIDs } = require("@budibase/auth/db")
getDevelopmentAppID,
getAllApps,
isDevAppID,
} = require("@budibase/auth/db")
const { doesDatabaseExist } = require("../../utilities") const { doesDatabaseExist } = require("../../utilities")
const { UserStatus } = require("@budibase/auth/constants") const { UserStatus } = require("@budibase/auth/constants")
@ -78,8 +74,12 @@ exports.syncUser = async function (ctx) {
try { try {
user = await getRawGlobalUser(userId) user = await getRawGlobalUser(userId)
} catch (err) { } catch (err) {
if (err && err.status === 404) {
user = {} user = {}
deleting = true deleting = true
} else {
throw err
}
} }
const roles = user.roles const roles = user.roles
// remove props which aren't useful to metadata // remove props which aren't useful to metadata
@ -90,9 +90,7 @@ exports.syncUser = async function (ctx) {
let prodAppIds let prodAppIds
// if they are a builder then get all production app IDs // if they are a builder then get all production app IDs
if ((user.builder && user.builder.global) || deleting) { if ((user.builder && user.builder.global) || deleting) {
prodAppIds = (await getAllApps(CouchDB, { idsOnly: true })).filter( prodAppIds = await getDeployedAppIDs(CouchDB)
id => !isDevAppID(id)
)
} else { } else {
prodAppIds = Object.entries(roles) prodAppIds = Object.entries(roles)
.filter(entry => entry[1] !== BUILTIN_ROLE_IDS.PUBLIC) .filter(entry => entry[1] !== BUILTIN_ROLE_IDS.PUBLIC)

View File

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

View File

@ -4,7 +4,7 @@ const { getTenantId, isTenantIdSet } = require("@budibase/auth/tenancy")
const { checkSlashesInUrl } = require("../utilities") const { checkSlashesInUrl } = require("../utilities")
const env = require("../environment") const env = require("../environment")
exports.syncUserInApps = async userId => { async function makeAppRequest(url, method, body) {
if (env.isTest()) { if (env.isTest()) {
return return
} }
@ -13,12 +13,19 @@ exports.syncUserInApps = async userId => {
if (isTenantIdSet()) { if (isTenantIdSet()) {
request.headers[Headers.TENANT_ID] = getTenantId() request.headers[Headers.TENANT_ID] = getTenantId()
} }
if (body) {
request.headers["Content-Type"] = "application/json" request.headers["Content-Type"] = "application/json"
request.body = JSON.stringify({}) request.body = JSON.stringify(body)
request.method = "POST" }
const response = await fetch( request.method = method
checkSlashesInUrl(env.APPS_URL + `/api/users/sync/${userId}`), return fetch(checkSlashesInUrl(env.APPS_URL + url), request)
request }
exports.syncUserInApps = async userId => {
const response = await makeAppRequest(
`/api/users/metadata/sync/${userId}`,
"POST",
{}
) )
if (response.status !== 200) { if (response.status !== 200) {
throw "Unable to sync user." throw "Unable to sync user."